Copied to clipboard

awk

awk is a text processing tool that can be used for data extraction, formatting, calculation, and other operations on text. Its command-line syntax is simple and easy to learn and can be widely used in Linux and Unix systems.

Example

awk is a text processing tool commonly used for:

  1. Extracting data from files and manipulating it according to field separators.
  2. Using awk in shell scripts for conditional statements and flow control.
  3. Combining awk with other shell tools (such as sed and grep) to perform more complex text processing tasks.
  4. Formatting and calculating data with awk, such as generating reports or calculating totals, averages, etc. in files.
  5. Filtering data with awk, such as extracting only certain data from files based on specific conditions or deleting certain lines from files.

Common awk commands include:

  • awk -F"separator" '{print $1}' filename: Split the contents of the file according to the specified separator and output the first field of each line.
  • awk '/match rule/{print $0}' filename: Find the lines in the file that match the specified rule and output the entire contents of these lines.
  • awk '{total += $1} END {print total}' filename: Calculate the sum of the first column of numbers in the file and output the result.
  • awk '{if ($1 > 10) print $0; else print "Less than or equal to 10"}' filename: Output the lines in the file where the first column of numbers is greater than 10; otherwise, output "Less than or equal to 10".
  • awk 'NR > 1' filename: Output all lines in the file except the first one.