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?
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?
The simplest solution is:
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)
Alternatively, you can do one of:
One of the comments asks, 'where does the filename go'? awk is used as a filter, so it would typically appear as:
with no filename given. Or, a filename can be given as an argument after all commands:
More than two columns
Output:
See also: https://unix.stackexchange.com/questions/46275/swapping-an-unlimited-number-of-columns
Tested in GNU Awk 4.0.1.
Use awk:
That should give you what you want.
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.