Given a text file file.txt, transpose its content.
For example, if file.txt has the following content:
name age
alice 21
ryan 30
Output the following:
name alice ryan
age 21 30
Given a text file file.txt, transpose its content.
For example, if file.txt has the following content:
name age
alice 21
ryan 30
Output the following:
name alice ryan
age 21 30
With shell utils cut and paste:
for f in 1 2 ; do cut -d ' ' -f $f file.txt ; done | paste -d ' ' - - -
Outputs:
name alice ryan
age 21 30
How it works. The field separator in file.txt is a space, so both cut and paste (which have tab as a default field separator) must use the -d ' ' option. We know in advance there are two columns in file.txt, the for loop therefore requires two passes. Pass #1, cut selects column 1 from file.txt, pass #2, column 2. When the for loop ends, what's fed to the pipe '|' looks like:
name alice ryan age 21 30
Then paste outputs that three at a time (hence the three hyphens).