lostandconfused Posted February 7, 2016 Share Posted February 7, 2016 Hello, I have a text file with over 500 lines. I need to take the first word of each line and move it to the end of the same line. Did my best to find any leads, but don't know where to start. Can anyone point me in the right direction? Thank you in advance Link to comment Share on other sites More sharing options...
AutoBert Posted February 7, 2016 Share Posted February 7, 2016 You need _FileReadToArray, For...Next, StringSplit, _FileWriteFromArray and basic understanding of arrays. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 7, 2016 Moderators Share Posted February 7, 2016 (edited) lostandconfused, This seems to work: #include <StringConstants.au3> #include <Array.au3> $sText = "start1 blah end1" & @CRLF & _ "start2 blah end2" & @CRLF & _ "start3 blah end3" & @CRLF & _ "start4 blah end4" & @CRLF & _ "start5 blah end5" & @CRLF & _ "start6 blah end6" ; Simulate reading the file into an array with _FileReadToArray $aLines = StringSplit($sText, @CRLF, $STR_ENTIRESPLIT) ; Loop through the array and alter each line For $i = 1 To $aLines[0] $aLines[$i] = StringRegExpReplace($aLines[$i], "(?U)^(.*)(\s.*\s)(.*)$", "$3$2$1") Next ; Here is the result - now use _FileWriteFromAray to rewrite it _ArrayDisplay($aLines, "", Default, 8) RegEx decode: (?U) - Not greedy , look for smallest match ^ - Start of string (.*) - Capture any characters up to (\s.*\s) - the first space, which starts this captured group until the final space (.*) - when we capture the final characters until $ - the end of the string $3$2$1 - Rewrite the captured groups in the reverse order I will see if I can now develop a single pass RegEx - although a guru will probably beat me to it! M23 Edit: Easier than I thought: #include <MsgBoxConstants.au3> ; Simulate reading the file into a variable with FileRead $sText = "start1 blah end1" & @CRLF & _ "start2 blah end2" & @CRLF & _ "start3 blah end3" & @CRLF & _ "start4 blah end4" & @CRLF & _ "start5 blah end5" & @CRLF & _ "start6 blah end6" $sNewText = StringRegExpReplace($sText, "(?Um)^(.*)(\s.*\s)(.*)$", "$3$2$1") ; And here is the result MsgBox($MB_SYSTEMMODAL, "New", $sNewText) ; Now rewrite the file with FileWrite Similar RegEx decode - but with the (?m) multiline option added to treat each line as a separate string. Edited February 7, 2016 by Melba23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
kylomas Posted February 7, 2016 Share Posted February 7, 2016 Thus spake M23 the Wise.. "The thing about regular expressions is knowing when to use them." Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 7, 2016 Moderators Share Posted February 7, 2016 kylomas, And this was certainly one of them! M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
mikell Posted February 7, 2016 Share Posted February 7, 2016 This could be done without using an array $txt = "1 number of this line" & @CRLF & _ "2 number of this line" & @CRLF & @CRLF & _ "3 number of this line" & @CRLF & _ "4 number of this line" ; $txt = FileRead("1.txt") Msgbox(0,"", $txt) $txt = StringRegExpReplace($txt, '(?m)^(\S+)\h+(.*)$', "$2 $1") Msgbox(0,"", $txt) Link to comment Share on other sites More sharing options...
Gianni Posted February 7, 2016 Share Posted February 7, 2016 a 'regex-free' way... Local $sString = 'one two three four five six seven eight nine ten' ConsoleWrite(_StringSwapFirstLast($sString) & @CRLF) Func _StringSwapFirstLast($sMyString) Local $iFirstSpacePos = StringInStr($sString, " ") Local $iLastSpacePos = StringInStr($sString, " ", 0, -1) Local $FirstWord = StringLeft($sString, $iFirstSpacePos - 1) Local $LastWord = StringRight($sString, StringLen($sString) - $iLastSpacePos) Return $LastWord & StringMid($sString, $iFirstSpacePos, $iLastSpacePos - $iFirstSpacePos + 1) & $FirstWord EndFunc ;==>_StringSwapFirstLast Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 7, 2016 Moderators Share Posted February 7, 2016 mikell, Quote This could be done without using an array Beat you for once! M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
mikell Posted February 7, 2016 Share Posted February 7, 2016 Melba, Absolutely .... sorry I didn't see your edit Link to comment Share on other sites More sharing options...
lostandconfused Posted February 7, 2016 Author Share Posted February 7, 2016 41 minutes ago, Melba23 said: lostandconfused, This seems to work: #include <StringConstants.au3> #include <Array.au3> $sText = "start1 blah end1" & @CRLF & _ "start2 blah end2" & @CRLF & _ "start3 blah end3" & @CRLF & _ "start4 blah end4" & @CRLF & _ "start5 blah end5" & @CRLF & _ "start6 blah end6" ; Simulate reading the file into an array with _FileReadToArray $aLines = StringSplit($sText, @CRLF, $STR_ENTIRESPLIT) ; Loop through the array and alter each line For $i = 1 To $aLines[0] $aLines[$i] = StringRegExpReplace($aLines[$i], "(?U)^(.*)(\s.*\s)(.*)$", "$3$2$1") Next ; Here is the result - now use _FileWriteFromAray to rewrite it _ArrayDisplay($aLines, "", Default, 8) RegEx decode: (?U) - Not greedy , look for smallest match ^ - Start of string (.*) - Capture any characters up to (\s.*\s) - the first space, which starts this captured group until the final space (.*) - when we capture the final characters until $ - the end of the string $3$2$1 - Rewrite the captured groups in the reverse order I will see if I can now develop a single pass RegEx - although a guru will probably beat me to it! M23 Edit: Easier than I thought: #include <MsgBoxConstants.au3> ; Simulate reading the file into a variable with FileRead $sText = "start1 blah end1" & @CRLF & _ "start2 blah end2" & @CRLF & _ "start3 blah end3" & @CRLF & _ "start4 blah end4" & @CRLF & _ "start5 blah end5" & @CRLF & _ "start6 blah end6" $sNewText = StringRegExpReplace($sText, "(?Um)^(.*)(\s.*\s)(.*)$", "$3$2$1") ; And here is the result MsgBox($MB_SYSTEMMODAL, "New", $sNewText) ; Now rewrite the file with FileWrite Similar RegEx decode - but with the (?m) multiline option added to treat each line as a separate string. Thank you all. The code above does great, but it moves the first two words to the back on the line. Playing around with the settings now... Link to comment Share on other sites More sharing options...
iamtheky Posted February 7, 2016 Share Posted February 7, 2016 Can yall tell me why the extra stuff in your expressions make them optimal over something like: $sString = "start1 blah end1" & @CRLF & _ "start2 blah end2" & @CRLF & _ "start3 blah end3" & @CRLF & _ "start4 blah end4" & @CRLF & _ "start5 blah end5" & @CRLF & _ "start6 blah end6" msgbox(0, '' , stringregexpreplace($sString , "(.+?\b)(.*)" , "$2 $1")) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
mikell Posted February 7, 2016 Share Posted February 7, 2016 (edited) This stuff is here to avoid a leading space in the resulting lines Edited February 7, 2016 by mikell Link to comment Share on other sites More sharing options...
iamtheky Posted February 7, 2016 Share Posted February 7, 2016 Ah, i was thinking the multiline in yours had other uses, I had not even considered formatting. ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 7, 2016 Moderators Share Posted February 7, 2016 lostandconfused, Ooops! Try this instead: #include <MsgBoxConstants.au3> ; Simulate reading the file into a variable with FileRead $sText = "start1 blah fred end1" & @CRLF & _ "start2 blah fred end2" & @CRLF & _ "start3 blah fred end3" & @CRLF & _ "start4 blah fred end4" & @CRLF & _ "start5 blah fred end5" & @CRLF & _ "start6 blah fred end6" $sNewText = StringRegExpReplace($sText, "(?Um)^(\S*)\s(.*)\s(\S*)$", "$3 $2 $1") ; And here is the result MsgBox($MB_SYSTEMMODAL, "New", $sNewText) ; Now rewrite the file with FileWrite Decode: (?Um) - Non-greedy and multiline ^ - Start of line (\S*) - Caprture any non-spaces until \s - first space (.*) - Capture all characters until \s - last space (\S*) - Capture all nonspace characters until $ - end of line $3 $2 $1 - Reassmeble inserting spaces M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
mikell Posted February 7, 2016 Share Posted February 7, 2016 (edited) lostandconfused, In Melba's code change to this line : $sNewText = StringRegExpReplace($sText, "(?Um)^(.*)(\s+)(.*)$", "$3$2$1") Edit Or use mine Edited February 7, 2016 by mikell Link to comment Share on other sites More sharing options...
lostandconfused Posted February 7, 2016 Author Share Posted February 7, 2016 3 minutes ago, Melba23 said: lostandconfused, Ooops! Try this instead: #include <MsgBoxConstants.au3> ; Simulate reading the file into a variable with FileRead $sText = "start1 blah fred end1" & @CRLF & _ "start2 blah fred end2" & @CRLF & _ "start3 blah fred end3" & @CRLF & _ "start4 blah fred end4" & @CRLF & _ "start5 blah fred end5" & @CRLF & _ "start6 blah fred end6" $sNewText = StringRegExpReplace($sText, "(?Um)^(\S*)\s(.*)\s(\S*)$", "$3 $2 $1") ; And here is the result MsgBox($MB_SYSTEMMODAL, "New", $sNewText) ; Now rewrite the file with FileWrite Decode: (?Um) - Non-greedy and multiline ^ - Start of line (\S*) - Caprture any non-spaces until \s - first space (.*) - Capture all characters until \s - last space (\S*) - Capture all nonspace characters until $ - end of line $3 $2 $1 - Reassmeble inserting spaces M23 M23, thank you for the details explanation. This helps me learn as I go along so that soon I may be able to help others, just like you. 3 minutes ago, mikell said: lostandconfused, In Melba's code change to this line : $sNewText = StringRegExpReplace($sText, "(?Um)^(.*)(\s)(.*)$", "$3$2$1") This works. Thank you! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 7, 2016 Moderators Share Posted February 7, 2016 lostandconfused, When you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - we know what we wrote and it just pads the thread unnecessarily. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now