Sed and Paste (scroll to bottom to see the PASTE primer)can be used to join lines. Paste in linux isnt your “copy paste clipboard” command, instead paste is used to paste lines together from a text in any way that you want.

example:

1
2
3
4

Can turn into:

1 2
3 4

 

NOTE: using the paste command is the easiest. However you can also do magic with “awk” and “sed”. With sed its easy-ish to join 2 items per line (see below). with awk its easy-ish to join X number of items per line (see this article). However its by far the eaiset to use paste command.

JOIN EVERY OTHER LINE
######################

NOTE: assume command has needed output

command | paste -sd" \n" -
command | paste -s -d " \n"
command | sed -rn 'N;s/\n/ /;p'
command | awk 'NR%2{printf $0" ";next;}1' 
command | awk 'NR%2{printf("%s ", $0); next}1'

Example on 10 numbers

seq 11 > num.txt

Output of reading num.txt (cat num.txt ):

1
2
3
4
5
6
7
8
9
10
11

Now run a join:

cat num.txt | paste -s -d " \n" -

Output:

1 2
3 4
5 6
7 8
9 10
11

Now run another join:

cat num.txt | paste -s -d " \n" | paste -s -d " \n"

Output:

1 2 3 4
5 6 7 8
9 10 11

UPDATE:

Getting any kind of delimitation with Paste command
##############################################

Using PASTE you can combine X number of items per line.
How? Its all on how you format the -d argument (dont forget the -s as well)
* If you just want 1 item per line you put “\n”: paste -s -d “\n”
* If you want 2 items per line you add a single space before the \n, so its ” \n”: paste -s -d ” \n”
* If you want 3 items per line you add 2 spaces before the \n, so its ”  \n”: paste -s -d ” \n”
* If you want X number of items per line you add X-1 spaces before the \n “<X – 1 spaces>\n”: paste -s -d “\n”
* ex: 5 items per line. X is 5. So X-1 is 4. So I need 4 spaces before the N. So its ”    \n”: paste -s -d “<X – 1 spaces>\n”

# cat num.txt | paste -s -d "\n"
1
2
3
4
5
6
7
8
9
10
11
# cat num.txt | paste -s -d " \n"
1 2
3 4
5 6
7 8
9 10
11

Notice how extra entries at the end, are still put in, they just dont have the full number of items per row (this is intended behaviour)

# cat num.txt | paste -s -d " \n"
1 2 3
4 5 6
7 8 9
10 11
# cat num.txt | paste -s -d " \n"
1 2 3 4
5 6 7 8
9 10 11
# cat num.txt | paste -s -d " \n"
1 2 3 4 5
6 7 8 9 10
11

Advanced (how Paste works): If you want to switch the behaviour, so that the items that are left out are in the beginning. You switch the -d to have the \n first.

# cat num.txt | paste -s -d "\n    "
1
2 3 4 5 6
7 8 9 10 11

How this works: Think of each space as “join the current and next line with a space in between” and the \n as the instruction to put a new line. You can even do wacky delimitations like this ” \n “. Also you dont need to use a space to join items. you can use other chars like colon or dot. Like this

# cat num.txt | paste -s -d "\n...."
1
2.3.4.5.6
7.8.9.10.11

So here we see take the item and join it to next item with a new line. Thats why 1 is by itself. Then the next five items are joined by 4 dots. Then a new line. Then 4 dots.

# cat num.txt | paste -s -d ".+\n:-"
1.2+3
4:5-6.7+8
9:10-11

Here we can get an idea of how it works. Take first and second item and join with dot. The 2nd and 3rd item with a +. THe 4th and 5th item with a new line (so thats where the line break is). and so on..

The end

Leave a Reply

Your email address will not be published. Required fields are marked *