How to merge lines of files in the Linux system?

LinuxOperating SystemOpen Source

To merge lines of files, we use the paste command in the Linux system. 

The paste command is used to combine files horizontally by outputting lines consisting of the sequentially corresponding lines from each FILE, separated by TABs to the standard output. When it has completed its operating for the last file, paste will output newline character and move on to the next line.

Syntax

The general syntax of the paste command is as follows −

paste [OPTION]... [FILE]...

Note – with no FILE the paste command read input from standard input.

Brief description of options available in the paste command.

Sr.No.Option & Description
1-d, --delimiters=LIST
Reuse characters from LIST instead of TABs
2-s, --serial
Paste one file at a time instead of in parallel
3-z, --zero-terminated
Line delimiter is NULL, not newline
4--help
Displays a help message and then exits.
5--version
It gives info about the version and then exits.

To merge the files parallelly, we use the paste command as shown below.

First, we need to create two files to merge simultaneously.

$ cat >text1.txt EMP_ID EMP_NAME
001 GAURAV
002 SID
$ cat >text2.txt
EMP_AGE
22
23
$ paste text1.txt text2.txt
EMP_ID EMP_NAME EMP_AGE
001 GAURAV 22
002 SID 23

Here, we will use the above files and save output in another file instead of standard output using the paste command in the Linux system as shown below.

$ paste text1.txt text2.txt >text.txt

To merge the files parallelly with delimiter, we use the -d option with the paste command as shown below.

$ paste -d ‘|’ text1.txt text2.txt
EMP_ID EMP_NAME |EMP_AGE
001 GAURAV |22
002 SID |23

To merges the files in a sequential manner, we use the -s option with the paste command as shown below.

$ paste -s text1.txt text2.txt

To check more information about the paste command, we use the --help option with the paste command in the Linux operating system as shown below.

$ paste --help

To check version information of the paste command, we use the --version option with the paste command in the Linux operating system as shown below.

$ paste --version
raja
Published on 01-Jul-2021 14:38:19
Advertisements