how to change the column A with the column B in Text file? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tyyi
Posts: 84
Joined: 23 Dec 2015, 08:51

how to change the column A with the column B in Text file?

03 Feb 2017, 21:01

Dear All,

I have a text file that is updated everyday. ( eg. test.txt )

The text file has just two columns A and B
Actually, I hope the data in the column A should be in the column B vice versa. ( column A data to column B vice versa column B data to column A )

column A | column B

0 | aaaaa
0 | bbbbb
0 | ccccc
1 | ddddd
1 | eeeee
0 | ffffff

----->

aaaaa | 0
bbbbb | 0
ccccc | 0
ddddd | 1
eeeee | 1
fffff | 0

I really appreciate you all who help me :D
User avatar
boiler
Posts: 16951
Joined: 21 Dec 2014, 02:44

Re: how to change the column A with the column B in Text file?  Topic is solved

03 Feb 2017, 22:02

Read the lines into a variable. Parse the lines in an outer loop and split each line in an inner loop using the pipe character (probably need to removes spaces first), the write each line with the parts swapped to a new file as you go.
tyyi
Posts: 84
Joined: 23 Dec 2015, 08:51

Re: how to change the column A with the column B in Text file?

04 Feb 2017, 07:03

boiler wrote:Read the lines into a variable. Parse the lines in an outer loop and split each line in an inner loop using the pipe character (probably need to removes spaces first), the write each line with the parts swapped to a new file as you go.
Dear Boiler,

Thank you for your answer.
I could resolve this problem based on your idea.
Really thank you for your idea again.! :bravo:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to change the column A with the column B in Text file?

04 Feb 2017, 16:29

Classic problem.
Here's some example code (*assuming 2 columns only* of course):

Code: Select all

q::
vText = ;continuation section
(Join`r`n
0 | aaaaa
0 | bbbbb
0 | ccccc
1 | ddddd
1 | eeeee
0 | fffff
)

vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2)
vText := StrReplace(vText, "`r`n", "`n")
Loop, Parse, vText, `n
{
	vTemp := A_LoopField
	vTemp := StrReplace(vTemp, " | ", "|")

	;method 1
	oTemp := StrSplit(A_LoopField, "|")
	vOutput .= oTemp.2 " | " oTemp.1 "`r`n"

	;method 2
	;StringSplit, vTemp, vTemp, |
	;vOutput .= vTemp2 " | " vTemp1 "`r`n"

	;method 3
	;Loop, Parse, vTemp, |
		;vTemp%A_Index% := A_LoopField
	;vOutput .= vTemp2 " | " vTemp1 "`r`n"
}

oTemp := ""
vOutput := SubStr(vOutput, 1, -2)
Clipboard := vOutput
MsgBox, % "done"
return
Last edited by jeeswg on 11 Jun 2017, 19:21, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: how to change the column A with the column B in Text file?

04 Feb 2017, 17:21

Here is another way with 2d array. Any better idea ? :problem:

Code: Select all

text_in=
(
column A | column B

0 | aaaaa
0 | bbbbb
0 | ccccc
1 | ddddd
1 | eeeee
0 | ffffff
)

msgbox, % "TEXT IN" . "`n" . text_in

rows:=StrSplit(text_in,"`n")
Array:=[]
for k, row in rows
if row contains |
loop, parse, row, "|"
{
  if a_index=1
     a:=a_loopfield

  if a_index=2
    {
    b:=a_loopfield
    row:= % b . " | " . a
    }
Array[k]:=StrSplit(row, "|")

}
else
Array[k]:=StrSplit(row, "|")

;msgbox, % st_printarr(array)

; recombine array into string
for l, rows in Array
{
	for m, col in rows
		str.= col " |"
	str.="`n"

}

TEXT_OUT:=str
TEXT_OUT:=RegExReplace(TEXT_OUT, "\|`n", "`n")

MsgBox, % "TEXT OUT `n" . TEXT_OUT

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jaka1, mikeyww, ReyAHK, Rohwedder and 290 guests