deeps deeps - 3 months ago 8
Linux Question

print a file content side by side bash

I have a file with below contents. I need to print each line side by side

hello
1223
man
2332
xyz
abc


Output desired:

hello 1223
man 2332
xyz abc


Is there any other alternative than paste command?

Answer

You can use this awk:

awk '{ORS = (NR%2 ? FS : RS)} 1' file

hello 1223
man 2332
xyz abc

This sets ORS (output record separator) equal to input field separator (FS) for odd numbered lines, for even numbered lines it will be set to input record separator (RS).

To get tabular data use column -t:

awk '{ORS = (NR%2 ? FS : RS)} 1' file | column -t

hello  1223
man    2332
xyz    abc