Skip to content

Instantly share code, notes, and snippets.

@amekusa
Last active August 19, 2024 07:22
Show Gist options
  • Save amekusa/bf1ad4681d0721199c2c60613b92e73d to your computer and use it in GitHub Desktop.
Save amekusa/bf1ad4681d0721199c2c60613b92e73d to your computer and use it in GitHub Desktop.
Removing comment lines & empty lines with Awk
awk '!/^([[:space:]]*#.*)?$/' testfile

testfile :

Beginning line
  Line starts with spaces
# Comment line
  # Comment line starts with spaces
Line # with a comment
2 empty lines below


Ending line

Result:

Beginning line
  Line starts with spaces
Line # with a comment
2 empty lines below
Ending line

+ Trimming leading & trailing whitespaces

awk '!/^([[:space:]]*#.*)?$/ {$1=$1;print}' testfile

Result:

Beginning line
Line starts with spaces
Line # with a comment
2 empty lines below
Ending line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment