Updated May 31, 2023
Introduction to Paste Command in Unix
Paste Command in Unix is among the most common and useful commands in Unix or Linux Shell Scripting. The main use of the paste command is to join the files horizontally by separating the lines from each file with a tab as a delimiter to get the required output. Paste command can be used to merge a single file or set of files aGenerally, the paste command is used both for single file handling and multiple file handling.
Syntax:
The basic syntax of the paste command in Unix Shell Scripting is given below:
paste [option].. [files]..
How does Paste Command work in Unix?
The main use of the paste command is to join the files horizontally by separating the lines from each file with a tab as a delimiter to get the required output.
Options that are available in paste commands are:
- -d, delimiter: Specifies the delimiter mentioned in the input parameter to display the output in a specified manner.
- – – help: This option helps to display the help message and options available in the paste command.
- – – version: Displays the version of the paste command that is running in your system and exit.
- – s, -serial: Helps display the file’s contents in a horizontal format.
Examples of Paste Command in Unix
Given below are the examples of Paste Command in Unix:
a. Paste Command for Single File Handling.
Let us consider a file ‘testing.txt’ that has the below contents. We can list the contents in the file by using the ‘cat’ command as shown below:
Code:
cat testing.txt
Output:
Paste command with a single file can be similar to a cat command in Unix. You can see below that the paste command works functionally like the cat command in the example below.
Code:
paste testing.txt
Output:
Example #1: Option -s
Option’ s’ joins the contents in lines to a horizontal format. As no delimiter is specified, ‘tab’ is taken as a delimiter for the column separator.
Syntax:
paste -s file_name.txt
Code:
paste -s testing.txt
Output:
Example #2: Option -d
Paste command when used with option ‘d’, is used to mention the delimiter. When it is used with a combination of -d and -s, then the lines in the files are merged into one single line with the delimiter specified.
Syntax:
paste -d, -s file_name.txt
Code:
paste -d, -s testing.txt
Output:
Example #3: Merge the contents in a column
The option ‘-‘ reads a line from the input file given. The number of ‘-‘s passed in the input will give the number of lines to be split side by side.
Syntax:
paste - - < file_name.txt
Code:
paste - - < testing.txt
Output:
Example #4: Merge the contents with a delimiter
We can split the lines into columns with the specified delimiter by specifying the delimiter.
Syntax:
paste -d'-' - - < file_name.txt
Code:
paste -d ' - ' - - < testing.txt
Output:
Example #5: Merge the data into different delimiters
We can also merge the lines in the file with different delimiters by passing two different delimiters in the input parameters.
Syntax:
paste -d',:' - - - < file_name.txt
Code:
paste -d ' , : ' - - - < testing.txt
Output:
b. Paste Command Options for Multiple File Handling.
Let us consider a file ‘testing2.txt’ with the following lines:
Code:
cat testing2.txt
Output:
Example #1: Option paste
When we try to paste both files, the paste command merges both files mentioned in the input side by side.
Syntax:
paste file1.txt file2.txt
Code:
paste testing.txt testing2.txt
Output:
Example #2: Option -d
Option ‘d’ helps to merge two files side by side with a delimiter specified in the input parameters.
Syntax:
paste -d, file1.txt file2.txt
Code:
paste -d, testing.txt testing2.txt
Output:
Example #3: Option ‘\n’
The paste command in Unix actively allows reading lines from both files alternately. Each line is displayed alternatively one by one and is given as an output.
Syntax:
paste -d'\n' file1.txt file2.txt
Code:
paste -d ' \n ' testing.txt testing2.txt
Output:
Example #4: Option –version
This option helps us know the version of the paste command used in our system.
Syntax:
paste –version
Code:
paste - - version
Output:
Example #5: Option –help
To display the options that can be used with the paste command in Unix Shell Scripting, actively use the ‘–help’ option.
Syntax:
paste –help
Code:
paste - -help
Output:
Conclusion
From the above article, we are now clear on using the paste command in Unix Shell Scripting with different options as per the required output. The paste command actively merges one or more lines in the input files. As the article above demonstrates, it can handle single or multiple files in the paste options.
Recommended Articles
We hope that this EDUCBA information on “Paste Command in Unix” was beneficial to you. You can view EDUCBA’s recommended articles for more information.