How to reverse column order of a file in Linux fro

2020-03-11 02:01发布

I have a file named ip-list with two columns:

IP1  <TAB>  Server1
IP2  <TAB>  Server2

And I want to produce:

Server1  <TAB>  IP1
Server2  <TAB>  IP2

What's the most elegant, shortest Linux command line tool to do it?

4条回答
虎瘦雄心在
2楼-- · 2020-03-11 02:23

The simplest solution is:

awk '{print $2 "\t" $1}'

However, there are some issues. If there may be white space in either of the fields, you need to do one of: (depending on if your awk supports -v)

awk -v FS='\t' '{print $2 "\t" $1}'
awk 'BEGIN{ FS="\t" } {print $2 "\t" $1}'

Alternatively, you can do one of:

awk -v OFS='\t' '{print $2,$1}'
awk 'BEGIN{ OFS="\t" } {print $2,$1}'
awk -v FS='\t' -v OFS='\t' '{print $2,$1}' # if allowing spaces in fields

One of the comments asks, 'where does the filename go'? awk is used as a filter, so it would typically appear as:

$ some-cmd | awk ... | other-cmd

with no filename given. Or, a filename can be given as an argument after all commands:

$ awk ... filename
查看更多
Fickle 薄情
3楼-- · 2020-03-11 02:28

More than two columns

printf '1 2 3 4\n5 6 7 8\n' | awk '{for(i=NF;i>0;i--)printf "%s ",$i;print ""}'

Output:

4 3 2 1
8 7 6 5

See also: https://unix.stackexchange.com/questions/46275/swapping-an-unlimited-number-of-columns

Tested in GNU Awk 4.0.1.

查看更多
来,给爷笑一个
4楼-- · 2020-03-11 02:30

Use awk:

awk '{print $2,$1}' ip-list

That should give you what you want.

查看更多
相关推荐>>
5楼-- · 2020-03-11 02:32

perl -pi -e 's/^([^\t]+)\t([^\t]+)$/\2\t\1/' yourfile.csv

perl -pi -e 'split("\t"); print "$_[1]\t$_[0]"'

The first one probably works on sed, too.

查看更多
登录 后发表回答