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:

1
2
3
printf '%s\n' 001:andy:30:root 002:mary:26:user 003:anna:22:user 004:john:24:user 005:jeff:28:user >> cut_file
printf '%s\t%s\t%s\t%s\n' '006' 'andy' '30' 'root' '007' 'mary' '26' 'user' '008' 'anna' '22' 'user' '009' 'john' '24' 'user' '010' 'jeff' '28' 'user' >> cut_file
cat cut_file
img

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:

1
2
cut -b 1-10 cut_file
cut --bytes 1-10 cut_file
img

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:

1
2
cut -c 1-10 cut_file
cut --characters 1-10 cut_file
img

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.

1
2
cut -f 1 -s cut_file
cut --fields 1 --only-delimited cut_file
img

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:

1
2
cut -f 1 -d ':' -s cut_file
cut --fields 1 --delimiter ':' --only-delimited cut_file
img

Retrieves the first column of the cut_file

Merge Lines of Files With paste

Let’s prepare some test data for the paste command:

1
2
3
4
printf '%s\n' file1a file1b file1c file1d file1e file1f > paste_file1
printf '%s\n' file2a file2b file2c file2d file2e file2f > paste_file2
cat paste_file1
cat paste_file2
img

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:

1
paste paste_file1 paste_file2
img

Retrieves two files' contents sequentially

Duplicate Lines

The paste command supports repeating the contents of the same file:

1
paste paste_file1 paste_file2 paste_file1
img

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:

1
paste - - - < paste_file1
img

Distributes standard input stream's file contents to each - symbols

We can also do this:

1
paste - paste_file1 - paste_file2 - < paste_file1
img

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:

1
2
3
4
printf '%s\n' 'ID1 User1' 'ID2 User2' 'ID3 User3' 'ID5 User5' > join_file1
printf '%s\n' 'ID1 User1' 'ID3 User3' 'ID4 User4' > join_file2
cat join_file1
cat join_file2
img

Prints multiple lines of test data to two files

We can use join to export the overlapped contents from both files:

1
join join_file1 join_file2
img

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

Buy me a coffeeBuy me a coffee