What are common AWK use cases?

Here are some common use cases for AWK:

  1. 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.
  2. Data Summarization: Calculating sums, averages, or counts from data.

    awk '{sum += $2} END {print sum}' file.txt  # Sums the values in the second column.
  3. Pattern Matching: Filtering lines based on specific patterns.

    awk '/error/ {print}' logfile.txt  # Prints lines containing the word "error".
  4. Field Manipulation: Modifying or formatting fields in a file.

    awk '{print $1, toupper($2)}' file.txt  # Converts the second column to uppercase.
  5. 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.
  6. 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.

0 Comments

no data
Be the first to share your comment!