awk
awk
is a powerful programming language and command-line utility for pattern scanning and processing. It is used for manipulating data and generating reports. awk
processes input line by line and applies a set of rules to each line.
Basic Syntax
Commonly Used Options
-F fs
: Set the input field separator tofs
.-v var=value
: Assign a value to a variable.-f file
: Read theawk
program from a file.-W
: Enable compatibility with older versions ofawk
.
Built-in Variables (1)
FS
: Input field separator (default is space).OFS
: Output field separator (default is space).RS
: Input record separator (default is newline).ORS
: Output record separator (default is newline).NR
: Number of records (lines) read so far.NF
: Number of fields in the current record.$0
: Entire current record.$1, $2, ...
: Individual fields in the current record.
Examples
Basic Print
Print all lines in a file:
Print the first field of each line:
Field Separator
Specify a comma as the field separator:
Pattern Matching
Print lines that contain "foo":
Print lines where the second field is greater than 10:
Built-in Variables (2)
Print the line number and the line itself:
Print the number of fields and the last field of each line:
Arithmetic Operations
Print the sum of the first and second fields:
String Operations
Print the length of the first field:
Using Variables
Assign a value to a variable and use it in the action:
Using a Script File
Create a script file script.awk
with the following content:
Run awk
with the script file:
Output Field Separator
Set the output field separator to a comma:
Conditional Statements
Print "High" if the second field is greater than 10, otherwise print "Low":
Loops
Print the sum of all fields in each line:
Conclusion
awk
is a versatile tool for text processing and data extraction. Understanding its options, built-in variables, and syntax allows for efficient and powerful text manipulation directly from the command line.