26. Bash Shell - Text Processing: cut, paste, join
Contents
We can use the cut, paste, join commands provided by linux to retrieve, duplicate, or merge text file contents.
Print Selected Parts With cut
Let’s prepare some test data for the cut command:
|
|

Prints multiple lines of test data to cut_file
Retrieve the Specified Amount of Bytes
We can add the -b or –bytes parameter to specify the length of bytes to output:
|
|

Prints ten bytes from the beginning of the lines
Retrieve the Specified Amount of Characters
We can add the -c or –characters parameter to specify the length of characters to output:
|
|

Prints ten characters from the beginning of the lines
Retrieve the Specified Column
By default, the -f or –fields parameter distinguishes the contents of each column by TAB.
|
|

Retrieves the first column of the cut_file
So we need to add an extra -d or –delimiter parameter to allow the -f or –fields parameter to distinguish between each column according to the specified delimiter:
|
|

Retrieves the first column of the cut_file
Merge Lines of Files With paste
Let’s prepare some test data for the paste command:
|
|

Prints multiple lines of test data to two files
Retrieve Lines Sequentially
We can use the paste command to output the contents of the two files in sequence:
|
|

Retrieves two files' contents sequentially
Duplicate Lines
The paste command supports repeating the contents of the same file:
|
|

Retrieves three files' contents sequentially
Intermix Lines From Standard Input
By using - symbols, we can distribute the contents of the file in the input stream to each of the - symbols:
|
|

Distributes standard input stream's file contents to each - symbols
We can also do this:
|
|

Distributes standard input stream's file contents to each - symbols
Join Lines of Two Files With join
Let’s prepare some test data for the paste command:
|
|

Prints multiple lines of test data to two files
We can use join to export the overlapped contents in both files:
|
|

Retrieves two files overlapped contents
References 8.1 cut: Print selected parts of lines, 8.2 paste: Merge lines of files, 8.3 join: Join lines on a common field
Author Dong Chen
LastMod Wed Feb 27 2019