How to get fields in reverse order?

i am having lines like below seperated by "|" (pipe)

abc|xyz
123|567
i have to get the above in reverse order

xyz|abc
567|123

Pls help

Hi suryanarayana,

Try with:

awk 'BEGIN{FS=OFS="|"}{print $2,$1}' inputfile
xyz|abc
567|123

Regards

1 Like

If there are only two fields, this will work.

awk -F"|" '{print $2"|"$1}' filename
1 Like

This works for any number of fields:

 perl -ane 'chomp;print join("\|", reverse(split /\|/) )."\n" ' infile

awk, many fields:

awk -F\| '{s=$NF;for(i=NF-1;i>=1;i--)s=s FS $i;print s}' infile

also easy with sed

 sed -r 's/(.*)\|(.*)/\2|\1/g'