Text Processing (Grep/Sed/Awk)
Stop manually trimming whitespace from legacy CSV exports
🧩 The Challenge
Dealing with those garbage CSV files from proprietary network appliances that add random spaces between commas makes my blood boil. Writing a regex loop to clean that up is a waste of a perfectly good afternoon.
💡 The Fix
Use awk to force the field separator to a comma, then tell it to ignore any leading or trailing whitespace around that delimiter automatically. It cleans the output instantly.
awk -F' *[,]+ *' '{print $1","$2","$3}' messy_data.csv
⚙️ Why It Works
Setting the field separator as a regex that accounts for variable spacing around the comma forces awk to treat the surrounding junk as part of the delimiter itself. This strips the padding during the parsing process, leaving you with clean, usable data.
🚀 Pro-Tip: Pipe it into column -t if you need to quickly preview the output like a readable table.
Linux Tips & Tricks | © ngelinux.com | 10/4/2026
