Merge multiple consecutive lines

At times there is a need to merge multiple consecutive lines to one. paste command makes it very easy to merge lines.

Syntax: paste -d’,’ – – < input_file

See the example below,

robin@Home-XPS:~$ echo -e '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12'
1
2
3
4
5
6
7
8
9
10
11
12
robin@Home-XPS:~$ echo -e '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12' > test.txt
robin@Home-XPS:~$ cat test.txt
1
2
3
4
5
6
7
8
9
10
11
12
robin@Home-XPS:~$ paste -d' ' - - < test.txt
1 2
3 4
5 6
7 8
9 10
11 12
robin@Home-XPS:~$ paste -d' ' - - - < test.txt
1 2 3
4 5 6
7 8 9
10 11 12
robin@Home-XPS:~$

You may also like...

Leave a Reply

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