1.

Solve : Swap text file colomn?

Answer»

Dear EXPERTS,
In Dos batch scripting,
How to swap text file colums,
I am having text file as follows,

abc.txt
11 26 2009
10 23 2008
05 15 2009

and required output as follows WITHIN text file,
2009 26 11
2008 23 10
2009 15 05

basically wanted to replace COLUMN no 3 with coloumn no 1 and vice-versa.
please advise how to go about it.
Thanx in advance.Code: [Select]for /F "tokens=1,2,3" %%i in (abc.txt) do @echo %%k %%j %%i >> new-abc.txt

RUN from a cmd prompt:

for /?


for complete information on the 'for' command.Best to have this line to start

if exist new-abc.txt del new-abc.txt

otherwise, each time you run it (because of using the >> [append] redirection operator) new-abc.txt will grow in size

Yogesh, you manged to spell "column" 4 different ways!


C:\batextra>type tokswap.bat
Code: [Select]@echo off

for /f "tokens=1,2,3 delims= " %%a in (abc.txt) do (echo %%c %%b %%a)
Input:
C:\batextra>type abc.txt
11 26 2009
10 23 2008
05 15 2009

Output:
C:\batextra>tokswap.bat
2009 26 11
2008 23 10
2009 15 05

C:\batextra>if you can use gawk for windows
Code: [Select]c:\test> gawk "{print $3,$2,$1}" file
C:\batextra>type swapawk.bat

Code: [Select]c:\BIN\awk "{print $3,$2,$1}" abc.txt
OUTPUT:

C:\batextra> swapawk.bat

C:\batextra>c:\bin\awk "{print $3,$2,$1}" abc.txt

2009 26 11
2008 23 10
2009 15 05

INPUT:

C:\batextra>type abc.txt
11 26 2009
10 23 2008
05 15 2009

__________________________________
C:\batextra>cd ..

C:\>cd bin

C:\bin> Usage: awk [-f programfile] [-Fc] [program] [var=value ...] [file ...]

C:\bin>

MKS Toolkit Commands – vi, sed, grep, awk, tar, gnu binutils, sh ...
Familiar environment enables UNIX/Linux developers to be equally productive on Windows with tools like vi, sed, grep, awk, tar, gnu binutils, sh, ksh, csh, ...
www.mkssoftware.com/products/tk/commands.asp?product.



Discussion

No Comment Found