Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk question - Suppose I have a file called Test1 with the ....

Status
Not open for further replies.

New2awk

MIS
Aug 8, 2005
12
AT
.....following data:
field1^Vfield2^V^field3^Vfield4

I need to take the data from test1 and output it into test2 but where i can specify the order that the fields come out.
I have tried to cut the fields using the following command
grep "f" test1 | cut f 4,3,2,1 -d"^V" >> test2. However this merely outputs the fields in test2 in their original order.

Is there an awk routine that I can run to do this (bearing in mind that I do not know any awk!)?

Thankyou very much in advance.

 
You are right, cut always display the fields in the original order.

for awk:
Code:
awk -F"^V" '/f/{print $4,$3,$2,$1;}' test1 > test2

The /f/ part replace your grep. The {print ...} part replace your cut.

If you want to have the same separator (^V) in the output:
Code:
awk 'BEGIN {IFS=OFS="^V"}; /f/{print $4,$3,$2,$1;}' test1 > test2
This set the InputFieldSeparator and the OutputFieldSeparator before any line is read.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top