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:
|
|
Retrieve the Specified Amount of Bytes
We can add the -b or –bytes parameter to specify the length of bytes to output:
|
|
Retrieve the Specified Amount of Characters
We can add the -c or –characters parameter to specify the length of characters to output:
|
|
Retrieve the Specified Column
By default, the -f or –fields parameter distinguishes the contents of each column by TAB.
|
|
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:
|
|
Merge Lines of Files With paste
Let’s prepare some test data for the paste command:
|
|
Retrieve Lines Sequentially
We can use the paste command to output the contents of the two files in sequence:
|
|
Duplicate Lines
The paste command supports repeating the contents of the same file:
|
|
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:
|
|
We can also do this:
|
|
Join Lines of Two Files With join
Let’s prepare some test data for the paste command:
|
|
We can use join to export the overlapped contents in both files:
|
|
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