User Tools

Site Tools


bash:commands:cut

BASH - Commands - cut

cut allows cutting of sections based on byte positions, characters, or fields separated by a delimiter like the ‘-‘ or ‘:’ characters.

cut can remove sections from each line of a file.

cut can return sections based on number of bytes (-b), characters (-c), or fields (-f) when fields are separated by a delimiter(-d).

Default delimiter is tab.


Cut using Bytes

Cut using Characters

Cut using Fields


Miscellaneous

Inspect The passwd File Using Cut Command

cut -d ':' -f1 /etc/passwd

The passwd file stored inside /etc in most systems contain very sensitive information about the system and its users.

You can inspect this file quickly using the cut command.

Delimiter ‘:’ is used as the columns of this file are separated using it.

Change the value of -f to monitor different fields.


Cut Specific Fields and Show Only the Unique Entries

cut -d ':' -f 3 test.txt | uniq -u

Cut the third column of the file test.txt and only show the unique entries.


Cut All Bytes of Input Stream Except the Specified Ones

echo "Let's cut this input stream section by section" | cut -b 1,3,5,7 --complement

Cut all the characters of the given input string except the ones supplied to -b.

So, byte positions first, third, fifth, and seventh will be omitted from the output.


Cut All Bytes of a File Except the Specified Ones

cut -b 2,4,6 test.txt --complement

Cut all the bytes of the file test.txt except the one mentioned in the command.

Thus, the output will not contain the second, fourth, and sixth bytes of each line.


Cut All Characters of Input Stream Except the Specified Ones

echo "Let's cut this input stream section by section" | cut -c 1,3,5,7 --complement

This command refrains from cutting the first, third, fifth, and seventh characters of the input string and instead cuts all other characters except these four.


Cut All Characters of a File Except the Specified Ones

cut -c 2,4,6 test.txt --complement

The output will contain all characters of the test.txt files except the ones mentioned.

So, characters second, fourth, and sixth will not be displayed.


Cut all Input Sections Except the Ones Specified

echo "Let's cut this input stream section by section" | cut -d ' ' -f 1,3,5 --complement

Output the string “cut input section by section“.

So, it will display all the input sections without the ones mentioned after the field flag.


Cut All Columns of a File Except the Specified Ones

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

Cut only the first and last columns of the file test.txt.

So, you can easily deselect some fields when processing large tabular documents using the complement flag.


Cut a Section of Input and Reverse them Characterwise

echo "Let's cut this input stream section by section" | rev | cut -d ' ' -f 1,3

Cut the first and third section of the input and reverse them characterwise.

Notice, how the output of one command is being fed as the input to other commands.


Cut Specific Columns in a File and Reverse them Characterwise

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

Cut the specified fields of the file test.txt and display the result in a characterwise reverse manner.


Modify the Output Delimiter of the Cut Command

echo "A,comma,separated,list,for,demonstration,purposes" | cut -d ',' -f 1- --output-delimiter=' '

Cut allows us to modify the output delimiter when displaying the result.

Cuts all sections of the comma-separated list but replaces the commas with spaces when showing the result.


Example of Cut+Sed Command with Tab Delimiter

sed 's/:/\t/g' test.txt | cut -f 1-4

Replace the colons in our file with tabs.

You can replace \t with some other characters like – or ; for changing to an output delimiter of your choice.

bash/commands/cut.txt · Last modified: 2021/01/26 16:06 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki