In today’s tutorial on Linux file management, we will be looking at valid approaches to joining multiple lines within a file into a single line. By the end of this article, you will have added some computing milestones to your Linux file management experience.
[ You might also like: How to Join Two Text Files in Linux ]
The possibilities/opportunities for users exploiting the Linux operating system via the command line interface continue to be endless. One such possibility is manipulating a multi-line file into a single-line file via a series of Linux commands. The goal for merging such lines can be as simple as the need to add custom delimiters to the targeted file.
Problem Statement
Since this tutorial will be focusing on merging multiple lines to a single line on a targeted file, we need to create such kind of file for reference purposes and populate it with some multi-line data.
$ sudo nano sample_file.txt
As per the screen capture above, we are dealing with a file (sample_file.txt) with six lines, and there are three approaches to joining these six lines:
- Without delimiters: I GenuinelyLoveLinuxShellTipsTutorials!
- With a single character delimiter: I Genuinely,Love,Linux.Shell,Tips,Tutorials!
- With multiple characters delimiters: I Genuinely;Love;Linux;Shell;Tips;Tutorials!
How to Join Multiple Lines into One Single Line in File
Let’s take a look at these three approaches to joining multiple lines within a file into a single line in Linux.
1. Using the sed Command
The power of the sed text-processing command-line utility will enable us to join the six lines in our sample text file by addressing the above three discussed approaches in reference to almost the same code syntax.
Without Delimiters:
$ sed ':a; N; $!ba; s/\n//g' sample_file.txt
With Spacing:
$ sed ':a; N; $!ba; s/\n/ /g' sample_file.txt
With a Single Character Delimiter e.g. a comma:
$ sed ':a; N; $!ba; s/\n/,/g' sample_file.txt
With Multiple Characters Delimiter e.g. semicolon and white space:
$ sed ':a; N; $!ba; s/\n/; /g' sample_file.txt
2. Using the awk Command
The Linux awk command is another reputable text-processing command-line tool. Its approach to solving our multi-line to the single-line problem can be analyzed and implemented in the following manner:
Without Delimiters:
$ awk -v d="" '{s=(NR==1?s:s d)$0}END{print s}' sample_file.txt
With Spacing:
$ awk -v d=" " '{s=(NR==1?s:s d)$0}END{print s}' sample_file.txt
With a Single Character Delimiter e.g. a comma:
$ awk -v d="," '{s=(NR==1?s:s d)$0}END{print s}' sample_file.txt
With Multiple Characters Delimiter e.g. semicolon and white space:
$ awk -v d="; " '{s=(NR==1?s:s d)$0}END{print s}' sample_file.txt
3. Using the paste Command
The paste command is only ideal for joining multiple lines without delimiters, with spacing, and with a single character delimiter. This utility resides under the GNU Coreutils package hence its availability on all Linux OS distributions.
Without Delimiters:
$ paste -sd '' sample_file.txt
With Spacing:
$ paste -sd ' ' sample_file.txt
With a Single Character Delimiter e.g. an underscore:
$ paste -sd '_' sample_file.txt
The trailing underscore (_)
can be removed by piping the paste command to a sed command:
$ paste -sd '_' sample_file.txt | sed 's/_$/\n/'
This approach is not ideal with multiple characters delimiter e.g a semicolon and a white space because of irregular outputs like the following:
$ paste -sd '; ' sample_file.txt
4. Using the tr Command
The tr command is also ideal for joining multiple lines without delimiters, with spacing, and with a single character delimiter.
Without Delimiters:
$ tr -d '\n' < sample_file.txt
With Spacing:
$ tr '\n' ' ' < sample_file.txt
With a Single Character Delimiter e.g. a comma:
$ tr '\n' ',' < sample_file.txt
Pipe the output to a sed command to get rid of the extra commas.
$ tr '\n' ',' < sample_file.txt | sed 's/,,$/\n/'
We have successfully learned how to join multiple lines into one single line in a file in Linux.