Stop wrestling with field separators in awk

Text Processing (Grep/Sed/Awk)

Stop wrestling with field separators in awk

🧩 The Challenge

You know that feeling when you’re parsing a log file or a CSV, and the column delimiters are all over the place? I’ve wasted entire afternoons trying to count spaces in a text file only to realize the source data has random tabs thrown in for “alignment.”

💡 The Fix

Just tell awk to treat multiple whitespace characters as a single delimiter using a regex field separator. It saves you from having to guess how many spaces exist between your data points.

awk -F'[[:space:]]+' '{print $2, $4}' your-messy-file.txt

⚙️ Why It Works

Setting the field separator to a character class containing plus signs forces awk to collapse those ugly, inconsistent gaps into one single delimiter. It handles both spaces and tabs simultaneously, which is exactly what you need when dealing with inconsistent CLI output.

🚀 Pro-Tip: Use NF to print the last column regardless of how many fields are in the row.

Linux Tips & Tricks | © ngelinux.com | 7/25/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted