anudeep
anudeep

Reputation: 413

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?

Upvotes: 1

Views: 501

Answers (2)

aruizcon
aruizcon

Reputation: 74

awk/gawk solution:

$ gawk 'BEGIN{ OFS="\t"} { COL1=$1; getline; COL2=$1; print(COL1,COL2)}' file
hello   1223
man     2332
xyz     abc

Bash solution (no paste command):

$ echo $(cat file) | while read col1 col2; do printf "%s\t%s\n" $col1 $col2; done
hello   1223
man     2332
xyz     abc

Upvotes: 1

anubhava
anubhava

Reputation: 785611

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

Upvotes: 2

Related Questions