How to use regular expressions in awk?

How to use regular expressions in awk?

Hey Richard

To use regular expressions in awk, utilize the ~ operator:

  1. awk '/pattern/ {print $0}' file - prints lines that match the pattern.
  2. awk '!/pattern/ {print $0}' file - prints lines that don’t match the pattern.
  3. awk '$1 ~ /pattern/ {print $2}' file - prints the second field where the first field matches the pattern.

Replace pattern with your desired regex.