Forrest logo
tool overview
On this page you find all important commands for the CLI tool awk. If the command you are looking for is missing please ask our AI.

awk

Awk is a powerful command line tool and a programming language mainly used for text processing and extraction in Unix and Unix-like operating systems. It is known for its capability to perform pattern scanning and processing on files, interpreting and executing commands provided within the awk script.

Here are some key features and functionalities of awk:

  1. Pattern scanning and processing: Awk allows you to search for specific patterns within a file or text stream and take actions based on those patterns. It supports regular expressions for pattern matching.

  2. Field and record processing: Awk automatically divides input records into fields, separated by a delimiter (by default, whitespace). It provides various built-in variables and functions to manipulate and analyze the fields of each record.

  3. Conditional and control flow statements: Awk supports if-else statements, loops, and flow control structures, allowing you to conditionally execute commands based on specific criteria or perform iterative operations.

  4. Text manipulation: Awk provides numerous built-in string and text manipulation functions, allowing you to modify and transform the input data as needed. These functions include substring extraction, concatenation, replacement, formatting, and more.

  5. Mathematical and arithmetic operations: In addition to text processing, awk supports a wide range of mathematical and arithmetic operations. It allows you to perform calculations, manipulate numbers, and generate complex reports based on mathematical computations.

  6. Input/output redirection: Awk can read input from files or from the standard input, and it can print output to the terminal or redirect it to files. This makes it flexible for handling large amounts of data.

Overall, awk is a versatile tool for efficiently processing and manipulating text-based data, making it widely used in data extraction, report generation, log analysis, file processing, and system administration tasks.

List of commands for awk:

  • awk:tldr:16f09 awk: Print different values based on conditions.
    $ awk '{if ($1 == "foo") print "Exact match foo"; else if ($1 ~ "bar") print "Partial match bar"; else print "Baz"}' ${filename}
    try on your machine
    explain this command
  • awk:tldr:39f5d awk: Print the fifth column (a.k.a. field) in a space-separated file.
    $ awk '{print $5}' ${filename}
    try on your machine
    explain this command
  • awk:tldr:56083 awk: Sum the values in the first column of a file and print the total.
    $ awk '{s+=$1} END {print s}' ${filename}
    try on your machine
    explain this command
  • awk:tldr:5662d awk: Print the last column of each line in a file, using a comma (instead of space) as a field separator.
    $ awk -F ',' '{print $NF}' ${filename}
    try on your machine
    explain this command
  • awk:tldr:88dc4 awk: Print all the lines which the 10th column value is between a min and a max.
    $ awk '($10 >= min_value && $10 <= max_value)'
    try on your machine
    explain this command
  • awk:tldr:89541 awk: Print every third line starting from the first line.
    $ awk 'NR%3==1' ${filename}
    try on your machine
    explain this command
  • awk:tldr:aea97 awk: Print all lines where the 10th column value equals the specified value.
    $ awk '($10 == value)'
    try on your machine
    explain this command
  • awk:tldr:e6726 awk: Print the second column of the lines containing "foo" in a space-separated file.
    $ awk '/${foo}/ {print $2}' ${filename}
    try on your machine
    explain this command
  • file_manipulation:warp:d97df Sum all numbers in a file
    $ awk 'BEGIN {OFMT = "%.0f"} { sum += $1 } END { print sum }' ${file_name}
    try on your machine
    explain this command
tool overview