Sometimes a very simple command can accomplish amazing tasks.
Recently I had a list with alternating entries on every other line with about 200 lines, and lots of uneven text. But I wanted to have 2 columns instead.
A quick online search let me find a new way of using a familiar command called paste
that I had used before. It is amazing what a simple dash -
can do!
Dash -
represents output, in the case below 2 dashes will mean alternate lines.
This is much easier with an example. Consider the list of data with alternating name and numbers contained in file s.txt
:
Jo:
1543
Meg:
685
Beth:
465
Amy:
645
In a Text-Based Terminal (Mac/Linux) I can send the content of the file to the paste
utility and add 2 dashes and get 2 columns.
paste - - < s.txt
Jo: 1543
Meg: 685
Beth: 465
Amy: 645
Using 4 dashes would yield l reorganize in 4 columns, 6 dashes in 6 columns…
paste - - - - < s.txt Jo: 1543 Meg: 685 Beth: 465 Amy: 645
While this is nice, the separation of the 2 columns is by blank spaces. To get tab-delimited data there is an example at the end of the manual pages specifying delimiters tab (\t
) and newline (\n
)
paste -s -d '\t\n' - < s.txt
Jo: 1543
Meg: 685
Beth: 465
Amy: 645
Now, imagine that the file has 2000 lines… that would not be so exciting to do this by hand! And what is there are a few dozens of such files…
See paste
manual pages for more.