Monthly Archives: March 2021

Convert multiple lines into one tab-separated line using paste

I generally have a lot of text files with such content:

1
Play track
Turn Around
Unlove this track
Panic Attack
Avinash Meetoo
110 scrobbles
2
Play track
Playing the Angel
Unlove this track
Precious
Depeche Mode
101
3
Play track
The Singles 86>98
Unlove this track
Personal Jesus
Depeche Mode
98

These are the three tracks I have listened to most since I started using Last.fm way back in 2005.

What I want to achieve is to easily convert these 21 lines (3 x 7) into 3 tab-separated lines which can then be easily imported into, say, Libreoffice Calc:

1<TAB>Play track<TAB>Turn Around<TAB>Unlove this track<TAB>Panic Attack<TAB>Avinash Meetoo<TAB>110 scrobbles
2<TAB>Play track<TAB>Playing the Angel<TAB>Unlove this track<TAB>Precious<TAB>Depeche Mode<TAB>101
3<TAB>Play track<TAB>The Singles 86>98<TAB>Unlove this track<TAB>Personal Jesus<TAB>Depeche Mode<TAB>98

For many years, each time I had to do that, I wrote a small Awk script. But, today, thanks to RudiC on the Unix Stack Exchange, I have the perfect recipe using paste:

paste -s -d "\t\t\t\t\t\t\n" file-containing-all-the-lines.txt

Naturally, you can adjust the number of “\t” if you have fewer or more fields.

Enjoy :-)