Table of content

Cut Command in Linux

The cut is a command-line utility that enables us to cut parts of lines from specified files or piped data and print the result to shared output. It can cut parts of a line by a delimiter, byte position, and character.

Along with the cut command, we have to pass the argument(option) i.e the criteria to filter out the data.

To cut a specific section, it is necessary to specify the delimiter. Delimiters can be a space (' '), a hyphen (-), a slash (/), or anything else.

Options:

The following command-line options are used by the cut command to make it more specific:

  • -b, --bytes=LIST : It is used to cut a specific section by bytes.
  • -c, --characters=LIST : It is used to select the specified characters.
  • -d, --delimiter=DELIM : It is used to cut a specific section by a delimiter.
  • -f, --fields=LIST : It is used to select the specific fields. It also prints any line that does not contain any delimiter character, unless the -s option is specified. After '-f' option, the column number is mentioned.
  • -n : It is used to ignore any option.
  • --complement : It is used to complement the set of selected bytes, characters or fields
  • -s, --only-delimited : It is used to not print lines that do not have delimiters.
  • --output-delimiter=STRING : This option is specified to use a STRING as an output delimiter; The default is to use "input delimiter".
  • -z, --zero-terminated : It is used if the line delimiter is NUL, not newline.
  • --help : It is used to display the help manual.
  • --version : It is used to display the version information.

The cut --version command checks the version.

linux-fundamental32

The cut -c1 filename command gives the list of the first characters of each line in the file.

linux-fundamental33

The cut -c2,4 filename command gives the list of second and fourth characters of each line in the file.

linux-fundamental34

The cut -c1-3 filename command gives the range of characters from the first to the third position of each line in the file.

linux-fundamental35

The cut -c1-3,5-7 filename command gives the list of the range of characters from the first to the third position and fifth to seventh of each line in the file.

The cut -b1-3 filename command gives the list of characters by byte size.

linux-fundamental36

The cut -d: -f 6 /etc/passwd command gives the list of the sixth column separated by delimiter : from the passwd file.

linux-fundamental37

The cut command not only works on a file system, but it can also act on an output of a command. The command ls -l filename | cut -c2-4 shall obtain the range of characters from the second to the fourth position from the output of the ls -l command.

About Author :

Myself Debomita Bhattacharjee, an IT employee with 6+ years of experience in Software industry. My area of interest is Automation testing and Front End Development.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions