Skip to main content

awk

awk is a powerful programming language designed for text processing and typically used as a data extraction and reporting tool. It processes text line by line and can perform complex logic, arithmetic, and string manipulation.

Basic Syntax

awk [options] 'program' [file...]

Common Options

OptionDescription
-F fsUse fs for the input field separator (the value of the FS predefined variable).
-v var=valAssign the value val to the variable var before execution of the program begins.
-f progfileRead the AWK program source from the file progfile.

Real-world Examples

  1. Print specific columns (e.g., 1st and 3rd):

    awk '{print $1, $3}' file.txt
  2. Use a custom field separator (e.g., comma):

    awk -F ',' '{print $2}' data.csv
  3. Print lines matching a pattern:

    awk '/error/ {print $0}' /var/log/syslog
  4. Sum the values in the first column:

    awk '{sum += $1} END {print sum}' numbers.txt