Hack 25. Cut Command Examples

by Ramesh

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

  1. $ cut -d: -f 1 names.txt
  2.  
  3. Emma Thomas
  4. Alex Jason
  5. Madison Randy
  6. Sanjay Gupta
  7. Nisha Singh

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

  1. $ cut -d: -f 1,3 names.txt
  2.  
  3. Emma Thomas:Marketing
  4. Alex Jason:Sales
  5. Madison Randy:Product Development
  6. Sanjay Gupta:Support
  7. Nisha Singh:Sales

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

  1. $ cut -c 1-8 names.txt
  2.  
  3. Emma Tho
  4. Alex Jas
  5. Madison
  6. Sanjay G
  7. Nisha Si

Misc Cut command examples

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

  1. $ cut -d: -f1 /etc/passwd

Displays the total memory available on the system.

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