KandZ – Tuts

We like to help…!

Linux CLI 43 🐧 cut paste and join commands

a - cut command
cut command is used to extract sections from each line of input
Extract is based on delimiters and field positions.
It is mainly used for processing text files
common options: -f fieldName → specifies the field to extract
-d delimiterChar → specifies the delimiter character -c charNumber → specifies specific characters from each line
cut -d ' ' -f 1 data.txt → ' ' delimiter and extract the first column
cut -d ' ' -f 1,3 data.txt → ' ' delimiter and extract the first and third column
cut -c 1-3 example.txt → extraxt the first 3 characters of each line

b - paste command
paste command is used to merge lines of files side by side
It reads data from each file and writes it to the standard output
Each line is separated by a TAB character
common options: -d 'delimiter'→ specifies an other delimiter than TAB
-s → merges files in series -u → also removes duplicate lines
paste data.txt example.txt → merges the line of two files
paste -d ',' data.txt example.txt → specify ',' as the dilimiter and merges the lines of two lines
paste data.txt example.txt csvfile.csv → merges the lines of two files and adds them to new file

c - join command
join command combines lines from two files on the basis of one or more fields
By default it assumes the first files is the one be joined
Files needs to be sorted and it uses the common fields
common options: -a fileNumber → displays lines from file index specified with no match
-t delimiter → specifies an alternative delimiter -i → ignore case differences
join file1.txt file2.txt → join lines of two files based on first field
join -v 1 file1.txt file2.txt → join lines and include lines with no match from both files
join -a 1 file1.txt file2.txt → join lines and include lines with no match from first file

Leave a Reply