Convert lines to columns

Let's say I have a bunch of lines of text, some of which are related, and I'd like to convert related lines to columns.

In this example, each logical dataset is four lines long, so I'd like to grab four lines at a time and convert each newline character to a column separator. I could write a script to do that, but there's a handy utility called 'paste', that will do all the heavy lifting for me.

Here's my example dataset:

data=$(
  for n in {1..5}; do
    echo $n
    for w in foo bar baz; do
      echo "$(shuf -n 1 -e ${w}{5..200})"
    done
  done
)
echo "$data"
1
foo24
bar171
baz56
2
foo49
bar105
baz61
3
foo156
bar94
baz63
4
foo132
bar7
baz187
5
foo130
bar30
baz81

The 'paste' command syntax to convert four lines at a time to columnar output would simply be:

paste -d'|' - - - -

For example:

echo "$data" | paste -d'|' - - - -
1|foo24|bar171|baz56
2|foo49|bar105|baz61
3|foo156|bar94|baz63
4|foo132|bar7|baz187
5|foo130|bar30|baz81

Nice! But let's say I also have a header:

header=$(
  for n in {1..4}; do
    echo "Col $n"
  done | paste -sd'|'
)
echo $header
Col 1|Col 2|Col 3|Col 4

And I want to combine the data with the header, then pass to another super handy utility called 'column' to line my columns up nicely:

(echo $header; echo "$data" | paste -d'|' - - - -) | column -t -s'|'
Col 1  Col 2   Col 3   Col 4
1      foo24   bar171  baz56
2      foo49   bar105  baz61
3      foo156  bar94   baz63
4      foo132  bar7    baz187
5      foo130  bar30   baz81

So easy. There are some many useful programs like this. I love it. 🙂

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).