Hack 25. Cut Command Examples

Cut command can be used to display only specific columns from a text file or other command outputs.

Following are some of the examples.

Display the 1st field (employee name) from a colon delimited file

$ cut -d: -f 1 names.txt

Emma Thomas
Alex Jason
Madison Randy
Sanjay Gupta
Nisha Singh

Display 1st and 3rd field from a colon delimited file

$ cut -d: -f 1,3 names.txt

Emma Thomas:Marketing
Alex Jason:Sales
Madison Randy:Product Development
Sanjay Gupta:Support
Nisha Singh:Sales

Display only the first 8 characters of every line in a file

$ cut -c 1-8 names.txt

Emma Tho
Alex Jas
Sanjay G
Nisha Si

Misc Cut command examples

Displays the unix login names for all the users in the system.

$ cut -d: -f1 /etc/passwd 

Displays the total memory available on the system.

$ free | tr -s ' ' | sed '/^Mem/!d' | cut -d" " -f2 

Comments on this entry are closed.

  • Ashwinee February 5, 2012, 11:40 pm

    good examples…

  • anon January 7, 2013, 1:11 pm

    Display only from the 3rd character on to the end of the line:

    cut -c 3-$NF names.txt

  • Jahaber Sadhik April 24, 2013, 6:59 am

    Good Examples.. And give me more on -b and -s.

  • Hasan May 30, 2013, 7:18 am

    Thanks. Great job.

  • Héctor July 12, 2013, 7:27 am

    Can you use “abc” as a delimiter?
    Thanks for the help.

  • Gengis Khan July 15, 2013, 7:16 pm

    Try the argument -d”abc” in the cut command.

  • ChetaN KS September 14, 2013, 8:07 am

    Simple and clear.. Thanks!

  • Danny December 7, 2013, 10:20 am

    I have a line in my source code:
    #define VERSION “0.1”
    and wanted to insert it into the name of the tarball, I added:
    `grep VERSION main.c | cut -d\” -f2`
    into the Makefile. The backslash allows you to use the double quote as a delimiter. The back quote turns the results of the command into an insertable string.