How to join line on Linux

When working with text on the command line, it is sometimes useful to join multiple lines together. Rather than going through text files and manually shifting lines around to be on the same line, our Linux system provides us with multiple tools to simplify this task for us. Log files are a good example of text that is usually split up onto multiple lines, and sometimes it is easier to visualize the data when some lines are joined together. In this tutorial, we will show you several ways to join lines of a file together on a Linux system.

In this tutorial you will learn:

  • How to join lines of a file with various Linux tools
How to join line on Linux
How to join line on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell, paste, sed, awk, tr
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to join line in Linux – various methods




As an example, we will use a file that has the following text. This is just to illustrate how we can combine the three lines into a single cohesive line by using the various methods shown below.

$ cat myfile.txt
Linux is
the best
operating system
NOTE
If you have data spread out across multiple files, you can use the join command to combine all of the text. While the join command provides a similar function, this is slightly different than the scenario we will be working with below, since all of our lines are contained within the same file already.
  1. The paste command is arguably the easiest tool for this job, so we will cover it first. It is normally installed on all major Linux distros by default. We will use the -s and -d options to merge the data by row, and set a custom delimiter, respectively. Then, we specify a space character as our delimiter with ' ', and finally enter the path to our input file.
    $ paste -sd ' ' myfile.txt
    Linux is the best operating system
    

    Depending on your use case, you may want to set a different delimiting character, such as a comma:

    $ paste -sd ',' myfile.txt
    Linux is,the best,operating system
    
  2. The next method we can use is the readarray function built into the Bash shell. The advantage of this method is that we do not need any extra tools to do the job.

    This method works by loading each line of the input file into an array (named myarray in this example). Then, we set the IFS variable, which indicates – to several Bash tools – what the separator needs to be, to a space (you can change this to any character to fit your needs). Finally, we echo all values of the array.

    $ readarray -t myarray < myfile.txt; IFS=' '; echo "${myarray[*]}"
    Linux is the best operating system
    

    Note that the -t option of readarray will delete any newline characters in the file, before loading the values into an array.

  3. awk is a very versatile utility that can also be used to combine the data from each line.


    $ awk 'BEGIN{ORS=" "}1' myfile.txt 
    Linux is the best operating system
    

    In this case, we set the ORS value to a space character, in order to have our text output as a cohesive sentence. However, in other cases, you could separate the lines by any value you want, such as a comma:

    $ awk 'BEGIN{ORS=","}1' myfile.txt 
    Linux is,the best,operating system
    
  4. The tr command is also a tool that can do the job of joining lines very simply. Here we are specifying that we want to combine all the data on each new line with '\n', and then specifying a space character ' ' as the delimiter.
    $ tr '\n' ' ' < myfile.txt
    Linux is the best operating system
    

    Now let’s try joining each line with commas:

    $ tr '\n' ',' < myfile.txt
    Linux is,the best,operating system
    
  5. Have you ever seen a text manipulation task that was not able to be tackled by sed? Neither have we. In this example, we will use the -z option to treat the input file as a single line, and then we replace each newline character (indicated by \n) with a space character.
    $ sed -z 's/\n/ /g' myfile.txt 
    Linux is the best operating system
    

    Or if you want to join the lines and separate with some other character, like commas:

    $ sed -z 's/\n/,/g' myfile.txt 
    Linux is,the best,operating system
    

    Since the -z option is only available in more recent versions of sed, here is another option that relies on xargs to combine all lines into a single line, before handing off the processing to sed:

    $ xargs < myfile.txt | sed -e 's/ / /g'
    Linux is the best operating system
    
Using various methods to join lines in a file in Linux
Using various methods to join lines in a file in Linux

Closing Thoughts




In this tutorial, we saw how to join multiple lines of a file into a single line on a Linux system. As is the case with most things in Linux, there are many tools for the job and many ways to accomplish the task. For the most part, they all work about the same, and you can use whichever one you feel is most appropriate for your situation. We also saw how we can use a variety of different field separators and delimiting characters to adapt the commands to different scenarios.