Extracting data using cut command in Linux

cut command allows you to extract specific portion of a line in a file.

Examples of cut command.

  1. cat /etc/passwd | cut -d: -f1 – This command will display first column from the file /etc/passwd. Here f stands for field. d stands for delimiter.
  2. cat /etc/passwd | cut -d: -f1,2 – This command will display first 2 columns from the file /etc/passwd.
  3. cut -c2 <filename> : This command will extract and show second character from each line of a file
  4. cut -c1-4 <filename> : This command will extract and show first 4 characters from each line of a file
  5. cut -c4- <filename> : This command will extract and show all characters starting from 4th character  from each line of a file
  6. cut -d’:’ -s -f1 <filename> : This command will not display line of there is no delimiter found in the line. “-s” stands for suppress.
  7. cut -d’:’ –complement -s -f1 <filename> : This command will complement the output. That means it will display all fields from the lines except field 1.
  8. cut -d’:’ -s -f1,2 –output-delimiter=’@’  <filename> : This command will display different delimiter in output.

You may also like...