AWK Command Examples
AWK: Powerful Text Processing
AWK is a versatile text-processing utility that excels at manipulating structured text data. It reads input line by line and can perform actions based on patterns. This guide explores common AWK use cases with practical examples.
Extracting Specific Fields
AWK treats each line of input as a record and splits it into fields based on a delimiter (whitespace by default). You can access these fields using variables like $1, $2, $3, etc., where $1 is the first field, $2 is the second, and so on. $0 represents the entire line.
Example: Displaying the Second Word in a String
To extract and display only the second word from a given string, you can use AWK's field splitting capabilities.
echo "one two three" | awk '{print $2}'
This command will output: two
Manipulating Columns in Data Files
AWK is particularly useful for processing delimited files, such as CSV (Comma Separated Values). You can specify the field separator using the -F option and control the output field separator with OFS.
Example: Removing the Second Column from a CSV File
Consider a CSV file named data.csv with the following content:
2023-08-31 13:40:44,19.90,66.30
2023-08-31 13:41:45,19.90,66.10
2023-08-31 13:42:46,19.90,66.10
To display only the first and third columns, effectively removing the second column, you can use the following AWK command:
cat data.csv | awk -F, 'BEGIN {OFS=FS} {print $1, $3}'
This command sets the input field separator to a comma (-F,), ensures the output field separator is also a comma (BEGIN {OFS=FS}), and then prints the first and third fields for each line.
Further AWK Applications
AWK can perform much more complex operations, including pattern matching, arithmetic operations, variable manipulation, and conditional logic. It's an essential tool for system administrators and developers working with text-based data on Unix-like systems.
For more advanced usage and detailed explanations, refer to the official AWK documentation or resources like GNU AWK Manual and MDN Date Documentation (for date-related processing contexts).