User Tools

Site Tools


bash:commands:cut:cut_using_fields

BASH - Commands - cut - Cut using Fields

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).


Cut the First Section of Input Stream

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 the First Section of a File

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.


Cut Specific Sections of the Input Stream

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 Specific Sections of a File

cut -d ':' -f 1,2,3 test.txt

This command will also provide the same sort of output as the previous command.


Cut Range of Fields from the Input Stream

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 Range of Fields from a File

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 Each Entry from a Specific Field and List them Alphabetically

cut -d ':' -f 1 test.txt | awk '{print $1}' | sort

List the result alphabetically sorted.


Cut Each Entry from a Field and List them in Alphabetically Reverse Order

cut -d ':' -f 1 test.txt | awk '{print $1}' | sort -r

Sorts the entries in a reverse manner.


Cut from a Fixed Field to the End of the Input Stream

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 from a Fixed Field to the End of a File

cut -d ':' -f 2- test.txt

Start cutting from the specified field and go till the end of each line.


Cut a Specified Number of Columns Starting from the First One

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 Some Specified Columns of a File Starting from the First One

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 Multiple Fields of CSV Files

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 Specific Fields of CSV Files and Sort them in Reverse Order

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.


* * * *


Field Cuts

Cut fields from a string, using a delimiter

cut -f 2- -d ':'
23:34:45:56

NOTE: -d specifies delimiter

returns:

34:45:56

Cut fields from a string, without specifying a delimiter

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 fields from a string, without specifying a delimiter; and prevent printing when delimiter not found

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 fields from a file, using a delimiter

cut -d: -f1 /etc/passwd >users.txt
bash/commands/cut/cut_using_fields.txt · Last modified: 2021/01/26 16:06 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki