Here are some common use cases for AWK:
-
Text Processing: Extracting specific columns from structured text files (e.g., CSV, TSV).
awk -F, '{print $1, $3}' file.csv # Prints the first and third columns of a CSV file. -
Data Summarization: Calculating sums, averages, or counts from data.
awk '{sum += $2} END {print sum}' file.txt # Sums the values in the second column. -
Pattern Matching: Filtering lines based on specific patterns.
awk '/error/ {print}' logfile.txt # Prints lines containing the word "error". -
Field Manipulation: Modifying or formatting fields in a file.
awk '{print $1, toupper($2)}' file.txt # Converts the second column to uppercase. -
Report Generation: Creating formatted reports from raw data.
awk '{count[$1]++} END {for (word in count) print word, count[word]}' file.txt # Counts occurrences of each word in the first column. -
Data Transformation: Reformatting data for further processing or analysis.
awk '{print $3, $1, $2}' file.txt # Rearranges columns in the output.
These use cases demonstrate AWK's versatility in handling text and data processing tasks efficiently.
