regular expression question

O

Orson

Sorry, this is probably silly question, but I cannot find right
answer.

I am trying to use grep -P (for perl regular expressions). I want to
match lines that do not contain the chars "sync".

I thought I could do:

grep -P "[^(sync)]" mytextfile

I understand [] matches any char in the brackets and (sync) groups
those chars together, so I would negate to mean should not match those
chars.

This is return all lines in the file. I am doing something wrong, just
do not know what. Can someone explain, please?
 
G

Gunnar Hjalmarsson

Orson said:
I am trying to use grep -P (for perl regular expressions). I want to
match lines that do not contain the chars "sync".

I thought I could do:

grep -P "[^(sync)]" mytextfile

I understand [] matches any char in the brackets and (sync) groups
those chars together, so I would negate to mean should not match those
chars.

The parens don't group within brackets, so you match every line with any
character other than
( s y n c )

Try using the -v option instead.

grep -Pv "sync" mytextfile
 
O

Orson

Orson said:
Sorry, this is probably silly question, but I cannot find right
answer.
I am trying to use grep -P (for perl regular expressions). I want to
match lines that do not contain the chars "sync".

Does your grep have the '-v' switch ('--invert-match'):

  grep -v sync


I thought I could do:
grep -P "[^(sync)]" mytextfile
I understand [] matches any char in the brackets and (sync) groups
those chars together, so I would negate to mean should not match those
chars.

Parentheses group, but not inside brackets. "[^(sync)]" will match any
character other than the 6 listed. In Perl, you would negate the test:

  $string !~ /sync/;

but that won't work as an option for grep. My grep doesn't have a '-P'
switch, so I can't tell how to use it. However, the -v switch seems to
do what you want.

Yes, this is interesting and it works. I was trying to get the same
you get in perl when you do $string !~ /sync/ as you mentioned. I have
a solution to problem and I thank you for that. Out of curiosity how
would you negate a word with regex with grep if you didn't have the -v
option or anything like it ? I am not sure I know because my way as
just matching characters, but you want to say lines that do NOT have
the word "sync". Perhaps there is no way using regex.
 
J

Jürgen Exner

Orson said:
I am trying to use grep -P (for perl regular expressions). I want to
match lines that do not contain the chars "sync".

Do you mean contains none of the characters 'c', 'n', 's', and 'y'?
Or do you mean does not contain all of those 4 characters?
Or do you mean does not contain the substring 'sync'?
There are very different requirements and your description can be
interpreted in all three way and it is not clear to me what you expect.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top