CUT & AWK commands

Akash C MAkash C M
3 min read

Understanding awk & cut commands

The cut command is a command-line utility that allows you to cut out sections of a specified file or piped data and print the result to standard output. The command cuts parts of a line by field, delimiter, byte position, and character.

  • Purpose: Primarily used for extracting specific fields or columns from lines of text.

  • Delimiter Specification: The delimiter is specified using the -d option.

  • Single Character Only: Supports a single character as the delimiter.


The awk command in Linux and Unix-like operating systems is a powerful text-processing tool that allows you to manipulate and analyze text data in files or through pipelines. It operates on a per-line basis, making it well-suited for tasks like extracting specific columns, filtering lines, and performing text transformations.

  • Purpose: A more versatile and general-purpose text-processing tool.

  • Delimiter Specification: The -F option is used to specify the field separator.

  • Multiple Characters: Supports multiple characters as the field separator.

  • Mathematical Operations: Can perform mathematical operations on fields.

  • Built-in Variables: Utilizes built-in variables like NF (number of fields), NR (record number), etc.


CUT

To print the 2nd column of the file with space as delimiter

cut -d "" -f2 <file_name>

d --> delimiter

To print from 2nd to 4th columns of the file

cut -d "" -f2-4 <file_name>

To print from 2nd and 5th columns of the file

cut -d "" -f2,5 <file_name>

AWK

To print the contents of the file

awk '{$print}' <file_name>

To print the 3rd column of the file

awk -F " " '{print3}' <file_name>

F --> field separator

To print the 3rd and 5th column of the file

awk -F " " '{print3,5}' <file_name>

To print the last column of the file

awk -F " " '{print$NF}' <file_name>

To print the last but one column of the file

awk -F " " '{print$(NF-1)}' <file_name>

To print the total number of columns in each row

awk -F " " '{$print NF}' <file_name>

To print the 2rd row of the file

awk 'NR==2 {print}' <file_name>

To print from 3rd row to 6th row of the file

awk 'NR==3,NR==6 {print}' <file_name>

To print the 2rd and 5th row of the file

awk 'NR==2;NR==5 {print}' <file_name>

To print the number of rows in the file

awk 'END {print NR}' <file_name>

Key Differences:

  • Flexibility:

    • awk is more flexible and powerful, suitable for a wide range of text processing tasks beyond simple column extraction.
  • Pattern Matching:

    • awk allows for the use of patterns and actions, making it more suitable for tasks involving conditional processing and complex text transformations.
  • Delimiter Flexibility:

    • awk supports multiple-character field separators, offering more flexibility in handling varied data formats.
  • Built-in Functions:

    • awk comes with a range of built-in functions, making it capable of more advanced text processing tasks.
0
Subscribe to my newsletter

Read articles from Akash C M directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Akash C M
Akash C M

Empowering community via open source and education