Hack #16 -> Cut Command

Cut command can be used to display only specific columns from a text

file or other command outputs.

The 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

Madison

Sanjay G

Nisha Si

Misc Cut command examples

  • cut -d: -f1 /etc/passwd Displays the unix login names for all the

users in the system.

  • free | tr -s ‘ ‘ | sed ‘/^Mem/!d’ | cut -d” ” -f2 Displays the

total memory available on the system.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s