Harnessing `awk` for Dynamic Column Filtering
Quick Tip
Harnessing `awk` for Dynamic Column Filtering
Challenge: You need to process a file with delimited columns and filter rows based on the value in a specific column, but the column number might vary or you want to make your script more flexible.
The Solution: Use `awk` with variables and pattern matching to dynamically filter columns.
# Example: Filter lines where the 3rd column (IP address) is "192.168.1.100" COLUMN_TO_FILTER=3 FILTER_VALUE="192.168.1.100" awk -v col="$COLUMN_TO_FILTER" -v val="$FILTER_VALUE" '$col == val' your_file.txt
Why it works: This `awk` command passes shell variables `COLUMN_TO_FILTER` and `FILTER_VALUE` into `awk`’s internal variables `col` and `val` using the `-v` option. `awk` then uses these variables to dynamically refer to the specified column (`$col`) and compare its value to the desired filter value (`val`).
Pro-Tip: You can combine this with other `awk` features like `print` to select specific columns to output after filtering. For instance, `awk -v col=”$COLUMN_TO_FILTER” -v val=”$FILTER_VALUE” ‘$col == val { print $1, $2 }’ your_file.txt` would print the first two columns of matching lines.
Linux Tips & Tricks | © ngelinux.com | 4/27/2026
