0

I use below command to filter the file for # and empty lines. But, how can we grep uncommented and blank lines in linux with single grep.

[root@localhost ~]# cat test | grep -v ^# | grep -v ^$
2

For simple cases, my go-to pattern for this is:

$ egrep '^[^#]'

This pattern matches lines which begin with some character OTHER than a pound sign.

If you extend the definition of 'blank' line to include a line entirely of whitespace, then the pattern fails, as it will match such a line. The pattern also fails if you allow arbitrary whitespace before the pound sign in a comment line (as Apache, bash, and others do).

If those cases are important to you, this pattern is better:

$ egrep '^[[:blank:]]*[^[:blank:]#]'

For example:

$ cat test
# comment
  # spaces then comment
config # then comment
before empty line

after empty line
space only on next line


tab only on next line

$ egrep '^[[:blank:]]*[^[:blank:]#]' test
config # then comment
before empty line
after empty line
space only on next line
tab only on next line
$
1

IIUC, you want to show non-blank and non-commented lines. You can do that with -e using a single grep command:

grep -v -e "^#" -e "^$" test

For example, if your test looks like this:

#a
uncommented line

#comment below blank line

output will be:

$ grep -v -e "^#" -e "^$" test
uncommented line
  • thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#] – Ritesh Vishwakarma Dec 11 '18 at 11:56
1

consider the file:

valid config line 1
# Comment
valid config line 2 # Comment
Blank line between

These two
One space in the line between

These two

If you consider the cases:

  • Lines starting with #
  • Blank lines

You can use cat file | grep -v '^$\|^#' or cat file | grep -v '^\($\|#\)'

And you will get something like:

valid config line 1
valid config line 2 # Comment
Blank line between
These two
One space in the line between

These two

However, I would consider also lines that start with a config line and then have an inline comment (supported in several config files), lines that are not blank but have only spaces, for this in a single command, I would use sed:

cat file | sed '/^\(#\|[[:space:]]*$\)/d;s/#.*//g'

Obtaining:

valid config line 1
valid config line 2 
Blank line between
These two
One space in the line between
These two

Explanation

  • [[:space:]]*$ matches 0 or more spaces before the end of the line
  • ^\(a\|b\) matches lines starting with a or b, using # as a and [[:space:]]*$ as b will match all lines starting with #, blank lines, and lines that only have spaces.
  • /match/d deletes all the matching lines
  • ; separates sed commands
  • s/a/b/g replaces a with b globally. Using #.* as a and an empty b will remove all the matching comments after a line.

Hope it helps. Regards

0

Got it!

[root@localhost ~]# cat file | grep -v ^'$\|#'
  • 3
    Hi Ritesh, watch out! if you have a line like config line # Comment you will be taking it out with this. If all of your comments are starting with # as the first char you should use grep -v '^$\|^#' – Jorge Valentini Dec 11 '18 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.