The cut command allows users to cut sections of a text very easily.
For this, we need to use both the d and the f flag of cut.
The d flag stands for delimiters and f for fields.
Delimiters are special characters that separate section of a text from others.
Common examples include ‘-‘, ‘:’, and ” ” (space).
echo "Let's cut this input stream section by section" | cut -d ' ' -f 1
The above cut command will cut the first section of text (“Let’s” in this case) from the input stream.
Note that the value to the delimiter flag -d is a single space.
cut -d ':' -f 1 test.txt
Returns the first columns of each row inside the file.
The value provided to the delimiter flag was a colon because that’s how our file separates the columns.
echo "Let's cut this input stream section by section" | cut -d ' ' -f 1,2,3
Displays only the first three field of the given input string.
It is done using a comma-separated array of field positions.
The output of this command will be ‘Let’s cut this‘.
cut -d ':' -f 1,2,3 test.txt
This command will also provide the same sort of output as the previous command.
echo "Let's cut this input stream section by section" | cut -d ' ' -f 1-5
Cuts the first five fields of the string and displays it in the terminal.
The apostrophes are required when space is used as the delimiter between multiple fields.
cut -d ':' -f 1-3 test.txt
Cuts each of the first three columns of our text file and show it as the output.
The apostrophes aren’t mandatory for characters like – or :.
cut -d ':' -f 1 test.txt | awk '{print $1}' | sort
List the result alphabetically sorted.
cut -d ':' -f 1 test.txt | awk '{print $1}' | sort -r
Sorts the entries in a reverse manner.
echo "Let's cut this input stream section by section" | cut -d ' ' -f 2-
This cut command will cut starting from the second field to the end of the string.
It can be beneficial when you need to cut from a specified position until the end of the input.
You can change the value of -f while keeping the trailing – on for cutting from different fields.
cut -d ':' -f 2- test.txt
Start cutting from the specified field and go till the end of each line.
echo "Let's cut this input stream section by section" | cut -d ' ' -f -5
Cuts the first five fields of the given input.
You need to add the preceding hyphen(-) else the output will not match your expectation.
cut -d ':' -f -2 test.txt
This will start cutting the file file test.txt from the first column and terminate after it has finished cutting the second command.
cut -d ',' -f 1,2 file.csv
The cut command will often prove to be a viable tool when you’re working with massive CSV documents.
The above command, for example, will cut the first two columns of a comma-separated CSV file called file.csv.
cut -d ',' -f 1,3,5 file.csv | sort -r
The above command will cut the first, third, and fifth columns of a comma-separated CSV file named file.csv and display the output in the reverse order.
* * * *
cut -f 2- -d ':' 23:34:45:56
NOTE: -d specifies delimiter
returns:
34:45:56
cut -f 2 er rt fg wd ji
returns:
er rt fg wd ji
NOTE: cut didn't find the delimiter (default is tab) so returns whole line.
cut -f 2 -s er rt fg wd ji
NOTE: cut wont print as -s flag is used to prevent printing when delimiter not found.
cut -d: -f1 /etc/passwd >users.txt