Home Linux Commandline Tips How to Join Multiple Lines into One in a File in Linux

How to Join Multiple Lines into One in a File in Linux

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
Create File in Linux
Create File in Linux

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:

  1. Without delimiters: I GenuinelyLoveLinuxShellTipsTutorials!
  2. With a single character delimiter: I Genuinely,Love,Linux.Shell,Tips,Tutorials!
  3. 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 
Join Multiple Lines into One Line
Join Multiple Lines into One Line

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
Join Multiple Lines Using Awk Command
Join Multiple Lines Using Awk Command

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
Join Multiple Lines Using Paste Command
Join Multiple Lines Using Paste Command

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/'
Join Multiple Lines Using tr Command
Join Multiple Lines Using tr Command

We have successfully learned how to join multiple lines into one single line in a file in Linux.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.