Thursday, April 29, 2021

Paste vs Cat

There are two commands that are handy in combining files in Unix. If you want to combine files vertically (think cake layers) - use cat and to combine horizontally (think columns) - use paste.
Use awk if you want to switch the order of the column after the files have been combined.

cat file1
a1 b1 c1
a2 b2 c2
 
cat file2
d1 e1
d2 e2 
cat file1 file2
a1 b1 c1
a2 b2 c2
d1 e1
d2 e2 
paste file1 file2 
a1 b1 c1 d1 e1
a2 b2 c2 d2 e2 
paste file1 file2 > file3
awk -F' ' '{print $5,$2,$3,$4,$1}' file3
e1 b1 c1 d1 a1
e2 b2 c2 d2 a2 
If you liked this or found it useful, leave me a comment. Thanks!

No comments:

Post a Comment