osexp2000
osexp2000

Reputation: 3165

Grep non-inline-comment lines without -v option

I want to grep all none # comment lines,

Although I can use grep -v -E '^\s*#', I want to know if there is another way not using -v(revert) option.

    #a comment line from a tab
     abcd
xx
#a comment line from head
 #a comment line from a space

I tried many patterns but failed:

grep -E '^\s*[^#]'
grep -E '^[ \t]*[^#]'

Upvotes: 1

Views: 97

Answers (1)

John1024
John1024

Reputation: 113834

Try:

$ grep -E '^\s*[^#[:space:]]' hashfile 
     abcd
xx

Discussion

Since a space is not #, the regex ^\s*[^#] will any line that begins with zero or more spaces followed by one space.

We could have used a blank and tab explicitly if we wanted:

$ grep -E '^\s*[^# \t]' hashfile 
     abcd
xx

Compatibility

\s is a GNU extension to grep. In extended regular expressions (-E), [:space:] is the character class containing white space characters. For a portable solution, use:

$ grep -E '^[[:space:]]*[^#[:space:]]' hashfile 
     abcd
xx

Upvotes: 1

Related Questions