Showing posts with label Paste Command. Show all posts
Showing posts with label Paste Command. Show all posts

Saturday, 23 March 2019

Paste command in Linux with examples

Paste Command, Linux Command, Linux Tutorial and Materials, Linux Learning

Paste command is one of the useful commands in Unix or Linux operating system. It is used to join files horizontally (parallel merging) by outputting lines consisting of lines from each file specified, separated by tab as delimiter, to the standard output. When no file is specified, or put dash (“-“) instead of file name, paste reads from standard input and gives output as it is until a interrupt command [Ctrl-c] is given.

Syntax:


paste [OPTION]... [FILES]...

Let us consider three files having name state, capital and number. state and capital file contains 5 names of the Indian states and capitals respectively. number file contains 5 numbers.

$ cat state
Arunachal Pradesh
Assam
Andhra Pradesh
Bihar
Chhattisgrah

$ cat capital
Itanagar
Dispur
Hyderabad
Patna
Raipur

Without any option paste merges the files in parallel. The paste command writes corresponding lines from the files with tab as a deliminator on the terminal.

$ paste number state capital
1       Arunachal Pradesh       Itanagar
2       Assam   Dispur
3       Andhra Pradesh  Hyderabad
4       Bihar   Patna
5       Chhattisgrah    Raipur

In the above command three files are merges by paste command.

Options:


1. -d (delimiter): Paste command uses the tab delimiter by default for merging the files. The delimiter can be changed to any other character by using the -d option. If more than one character is specified as delimiter then paste uses it in a circular fashion for each file line separation.

Only one character is specified
$ paste -d "|" number state capital
1|Arunachal Pradesh|Itanagar
2|Assam|Dispur
3|Andhra Pradesh|Hyderabad
4|Bihar|Patna
5|Chhattisgrah|Raipur

More than one character is specified
$ paste -d "|," number state capital
1|Arunachal Pradesh,Itanagar
2|Assam,Dispur
3|Andhra Pradesh,Hyderabad
4|Bihar,Patna
5|Chhattisgrah,Raipur

First and second file is separated by '|' and second and third is separated by ','.
After that list is exhausted and reused.

2. -s (serial): We can merge the files in sequentially manner using the -s option. It reads all the lines from a single file and merges all these lines into a single line with each line separated by tab. And these single lines are separated by newline.

$ paste -s number state capital
1       2       3       4       5
Arunachal Pradesh       Assam   Andhra Pradesh  Bihar   Chhattisgrah
Itanagar        Dispur  Hyderabad       Patna   Raipur

In the above command, first it reads data from number file and merge them into single line with each line separated by tab. After that newline character is introduced and reading from next file i.e. state starts and process repeats again till all files are read.

Combination of -d and -s: The following example shows how to specify a delimiter for sequential merging of files:

$ paste -s -d ":" number state capital
1:2:3:4:5
Arunachal Pradesh:Assam:Andhra Pradesh:Bihar:Chhattisgrah
Itanagar:Dispur:Hyderabad:Patna:Raipur

3. –version: This option is used to display the version of paste which is currently running on your system.

$ paste --version
paste (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Applications of Paste Command


1. Combining N consecutive lines: The paste command can also be used to merge N consecutive lines from a file into a single line. Here N can be specified by specifying number hyphens(-) after paste.

With 2 hyphens
$ cat capital | paste - -
Itanagar        Dispur
Hyderabad       Patna
Raipur

With 3 hyphens
$ paste - - - < capital
Itanagar        Dispur  Hyderabad
Patna   Raipur

2. Combination with other commands: Even though paste require at least two files for concatenating lines, but data from one file can be given from shell. Like in our example below, cut command is used with -f option for cutting out first field of state file and output is pipelined with paste command having one file name and instead of second file name hyphen is specified.

Note: If hyphen is not specified then input from shell is not pasted.

Without hypen
$ cut -d " " -f 1 state | paste number
1
2
3
4
5

With hypen
$ cut -d " " -f 1 state | paste number -
1       Arunachal
2       Assam
3       Andhra
4       Bihar
5       Chhattisgrah

Ordering of pasting can be changed by altering the location of hyphen:

$ cut -d " " -f 1 state | paste - number
Arunachal       1
Assam   2
Andhra  3
Bihar   4
Chhattisgrah    5

Saturday, 16 June 2018

Paste Command Examples in Unix / Linux

Unix Command, Linux Command, Paste Command, LPI Certifications

Paste command is one of the useful commands in unix or linux operating system. The paste command merges the lines from multiple files. The paste command sequentially writes the corresponding lines from each file separated by a TAB delimiter on the unix terminal.

The syntax of the paste command is

paste [options] files-list

The options of paste command are:

-d : Specify of a list of delimiters.
-s : Paste one file at a time instead of in parallel.
--version : version information
--help : Help about the paste command.

Paste Command Examples:


Create the following three files in your unix or linux servers to practice to practice the examples:

> cat file1
Unix
Linux
Windows

> cat file2
Dedicated server
Virtual server

> cat file3
Hosting
Machine
Operating system

1. Merging files in parallel


By default, the paste command merges the files in parallel. The paste command writes corresponding lines from the files as a tab delimited on the terminal.

> paste file1 file2
Unix    Dedicated server
Linux   Virtual server
Windows

> paste file2 file1
Dedicated server  Unix
Virtual server    Linux
                  Windows

2. Specifying the delimiter


Paste command uses the tab delimiter by default for merging the files. You can change the delimiter to any other character by using the -d option.

> paste -d"|" file1 file2
Unix|Dedicated server
Linux|Virtual server
Windows|

In the above example, pipe delimiter is specified

3. Merging files in sequentially.


You can merge the files in sequentially using the -s option. The paste command reads each file in sequentially. It reads all the lines from a single file and merges all these lines into a single line.

> paste -s file1 file2
Unix    Linux   Windows
Dedicated server        Virtual server

The following example shows how to specify a delimiter for sequential merging of files:

> paste -s -d"," file1 file2
Unix,Linux,Windows
Dedicated server,Virtual server

4. Specifying multiple delimiters.


Multiple delimiters come in handy when you want to merge more than two files with different delimiters. For example I want to merge file1, file2 with pipe delimiter and file2, file3 with comma delimiter. In this case multiple delimiters will be helpful.

> paste -d"|," file1 file2 file3
Unix|Dedicated server,Hosting
Linux|Virtual server,Machine
Windows|,Operating system

5. Combining N consecutive lines


The paste command can also be used to merge N consecutive lines from a file into a single line. The following example merges 2 consecutive lines into a single line

> cat file1 | paste - -
Unix    Linux
Windows