lostandconfused

Moving the first word of a string to the end

17 posts in this topic

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

Share this post


Link to post
Share on other sites



You need _FileReadToArray, For...Next, StringSplit, _FileWriteFromArray and basic understanding of arrays.

Share this post


Link to post
Share on other sites

#3 ·  Posted (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 by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

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)

 

Share this post


Link to post
Share on other sites

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

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Melba,
Absolutely  :D .... sorry I didn't see your edit  

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

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"))

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

This stuff is here to avoid a leading space in the resulting lines  :)

Edited by mikell

Share this post


Link to post
Share on other sites

Ah, i was thinking the multiline in yours had other uses, I had not even considered formatting.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

lostandconfused,
In Melba's code change to this line :

$sNewText = StringRegExpReplace($sText, "(?Um)^(.*)(\s+)(.*)$", "$3$2$1")

Edit
Or use mine  :D

Edited by mikell

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now