logo

Awk

🌱 2024-01-29


Awk (like sed) is one of those tools I have to look up every time I use it. This year that is going to change.

To print the first word of each line:

awk '{print $1}'

To print the last word of each line:

awk '{print $NF}'

And, to print the last two words of each line.

awk '{print $NF, $(NF-1)}'

Printing with row number and print first word:

awk '{print NR ") " $1 " -> " $(NF-2)}' sample

Splitting with field separator in awk:

awk 'BEGIN {FS=","}{print $1}'

If statement in awk:

awk '{if($1>10)print $1}'

Or, comparing third last work from the end of the string:

awk '{if($NF==3)print $NF}'

FNR (File Line Number). FNR gets reset from one file to the next. The \t here is the tab character.

awk '{print FNR "\t" $0}'

Number only non-blank lines:

awk 'NF { $0=++a " :" $0 }; { print }'
  1. {$0=++a " :" $0}
   { $0 = ++a ": " $0 }  // Modifies each record or line (due to NF being true)
   # Explanation:
   # a is an integer variable in AWK, incremented for every non-empty line
   # and its new value is converted back to string due to the enclosing quotation marks.
   # The ':' character is appended afterwards.
   # The remaining content of the line (represented by $0), which includes all fields, is concatenated with the incremented counter.
   # This forms a new line that preserves the original structure but with an incrementing counter at the beginning.
   # NF being true makes this rule apply to each record or line.
  1. {print}
  { print }  // Applies to every non-empty input line after the above modification and prints each line as it is, preserving any modifications made by the previous rule.
   # Explanation:
   # This rule simply prints each line without any further modifications, allowing the incremented counter to be visible in the output.
   # It ensures that each line is processed according to the rules defined above before being printed out.

Links: