Split and Join Arbitrary Text

Last week I enumerated some strategies to split and join large files on the terminal; let’s see how to do more split and join, but this time for any text on the standard input, just like the creators of Unix wanted us to operate on data.

In the examples below, command stands for anything you can execute on the terminal that writes output to stdin.

Split

Use the sed command to split text using a separator:

$ command
one,two,three

$ command | sed 's/,/\n/g'
one
two
three

Join

Use the paste command to join lines with a separator character, like the comma below:

$ command
one
two
three

$ command | paste -d, -s
one,two,three

Cut

You can also use the cut command to select the nth item in a list defined with a particular separator:

$ command
one,two,three

$ command | cut -d, -f2
two

Reverse

Here’s a fun one; use the tac command (cat reversed, got it?) to turn a list upside down:

$ command
one two three

$ command | tac
three
two
one

Concrete Example

Here’s an example to get an idea of how to use these commands together:

$ ls ~
Desktop Documents Downloads Music Pictures Public Templates Videos

$ ls ~ | tac
Videos
Templates
Public
Pictures
Music
Downloads
Documents
Desktop

$ ls ~ | tac | paste -d, -s
Videos,Templates,Public,Pictures,Music,Downloads,Documents,Desktop

$ ls ~ | tac | paste -d, -s | sed 's/,/-/g'
Videos-Templates-Public-Pictures-Music-Downloads-Documents-Desktop

$ ls ~ | tac | paste -d, -s | sed 's/,/-/g' | cut -d- -f5
Music