Site icon New Generation Enterprise Linux

Stop using grep to find non-matching lines when v is right there

Text Processing (Grep/Sed/Awk)

Stop using grep to find non-matching lines when v is right there

🧩 The Challenge

Dealing with massive configuration files where you want to see everything except the lines starting with a hash or just whitespace is a total pain. I spent way too much time in my early days piping output into an extra grep command just to filter the noise out.

💡 The Fix

Just add the invert match flag to your primary grep call so you can ditch the extra pipe and keep your terminal history readable. It cleans up your command line and makes your scripts run just a little bit faster.

grep -vE '^\s*($|#)' /etc/nginx/nginx.conf

⚙️ Why It Works

Adding the -v flag tells the utility to output only the lines that do not match your pattern, and using -E allows for extended regular expressions so you can handle blank lines and comment lines in a single pass. The regex logic here targets lines that are either empty or consist purely of whitespace followed by a hash.

🚀 Pro-Tip: Always include the -E flag when you know you’ll eventually need a pipe or a plus sign in your regex anyway.

Linux Tips & Tricks | © ngelinux.com | 8/31/2026

0 0 votes
Article Rating
Exit mobile version