Intro To 'paste' Command In Linux
2023-10-25 - By Robert Elder
I use the 'paste' command, to paste columns in a text document. The example below illustrates how the two files 'alphabetic.txt' and 'numeric.txt' can be combined using the 'paste' command:
cat alphabetic.txt
a
b
c
d
e
cat numeric.txt
1
2
3
4
5
paste alphabetic.txt numeric.txt
a 1
b 2
c 3
d 4
e 5
A Terminal Based Spreadsheet-like Paste Tool
The 'paste' command could be thought of as a primitive terminal based version of performing a 'paste' operation inside of a spreadsheet program:
A Practical Example
The paste command allows you paste columns of text at the end of lines in a file. For the next few examples, we'll make use of the files 'first_names.txt':
First
Tom
Bob
Scott
Alice
'last_names.txt':
Last
Miller
Kaiden
McKenna
Rene
'id_numbers.txt':
ID Number
1080621427
9168914220
3131331001
2732213184
'ages.txt':
Age
38
49
83
55
You can specify one or more files to the paste command, and the lines in these files will be appended one after another:
paste first_names.txt last_names.txt
First Last
Tom Miller
Bob Kaiden
Scott McKenna
Alice Rene
paste first_names.txt last_names.txt id_numbers.txt
First Last ID Number
Tom Miller 1080621427
Bob Kaiden 9168914220
Scott McKenna 3131331001
Alice Rene 2732213184
Specify A Custom Delimiter
By default, the columns will be delimited by tab characters, but you can use the '-d' flag to specify a different custom delimiter, like a comma:
paste -d ',' first_names.txt last_names.txt id_numbers.txt
First,Last,ID Number
Tom,Miller,1080621427
Bob,Kaiden,9168914220
Scott,McKenna,3131331001
Alice,Rene,2732213184
Pasting Columns In The Middle
The paste is not capable of directly inserting a column into the middle of existing lines since lines are simply appended in the same order that they are provided to the 'paste' command:
cat all.txt
First Last ID Number
Tom Miller 1080621427
Bob Kaiden 9168914220
Scott McKenna 3131331001
Alice Rene 2732213184
paste all.txt ages.txt
First Last ID Number Age
Tom Miller 1080621427 38
Bob Kaiden 9168914220 49
Scott McKenna 3131331001 83
Alice Rene 2732213184 55
However, you can achieve a similar result of 'pasting in the middle' by also leveraging the 'cut' command:
paste <(cut -f 1-2 all.txt) ages.txt <(cut -f 3- all.txt)
First Last Age ID Number
Tom Miller 38 1080621427
Bob Kaiden 49 9168914220
Scott McKenna 83 3131331001
Alice Rene 55 2732213184
In the above command, the cut command extracts only columns in the range from one to two, which is then followed by the age column, and then followed by the third until the last column.
And that's why the 'paste' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|