2

I am trying to write a regex to pull all non-comment and non-empty lines from /etc/samba/smb.conf. Comments are lines that:

  1. start with #
  2. start with ;
  3. start with any amount of whitespace followed immediately by either # or ;

I tried the following, but it did not properly handle comment type 3.

grep -P '^\s*[^#;]' /etc/samba/smb.conf

This one worked for all 3 types of comments:

grep -P '^\s*[^#;\s]' /etc/samba/smb.conf

Can you explain why adding \s to the character class successfully filtered out comment type 3?

nhavens
  • 252
  • 1
  • 3
  • 11
  • @vks no it shouldn't, this one works `cat smb.conf |grep -P '\s*[#;]' -v`. Read about `^` in begin of pattern and inside of character group defintion – Reishin May 13 '15 at 18:50

2 Answers2

1

[^...] means does not match any of the characters given at the place of ....

You need: ^\s*[#;].

Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • This is incorrect.Read the question carefully. ` pull all non-comment lines from /etc/samba/smb.conf.` – vks May 13 '15 at 18:42
  • in case of grep, `^` not needed, `cat smb.conf |grep -P '\s*[#;]' -v` . Proposed answer with OP question provide wrong result – Reishin May 13 '15 at 18:46
  • and PS `^` at start of string - means start of line, read about `^...$`. So answer is completely wrong – Reishin May 13 '15 at 18:49
  • @Reishin please read about character classes before classifing this answer as "completely wrong". Thanks! – Wouter J May 14 '15 at 08:51
1

The problem here is partial matches as you have not used an end anchor $.

In case of example 3

      ;

There will be partial matching upto ; done by \s*.In the other regex you have disabled \s so it will not capture the space and partial match is disabled.

The correct regex here is

 (?m)^(?!\s*[#;]).+$

See demo

vks
  • 67,027
  • 10
  • 91
  • 124
  • it's wrong, test on my file not filter lines like `#fsdfs` (with spaces at begin) – Reishin May 13 '15 at 18:55
  • i tell to you actual result of command output, that they didn't do what OP expect. – Reishin May 13 '15 at 18:59
  • This answer still matches lines that begin with spaces followed by #. – nhavens May 13 '15 at 19:03
  • @vks check the output [here](http://pastebin.com/CMq44Dj9) and [here](http://pastebin.com/V7pu5rpV) , i'm not care about regex101 matching, i'm check that in console – Reishin May 13 '15 at 19:05
  • @vks with the `m` flag, the first answer incorrectly matches comment type 3. The second answer incorrectly matches comment types 1 and 2. – nhavens May 13 '15 at 19:14
  • @vks It's correct now, except that it includes blank lines. I updated the original question to clarify. – nhavens May 13 '15 at 19:23