Reut Sharabani
Reut Sharabani

Reputation: 31339

grep's at sign caught as whitespace

I ran into a problem while trying to grep a file that contains the following:

@abc
abc
 abc

@abc@
 @abc@ 
 @abc@
acb
abc@
assd
dsasd
abc@ 

I tried various grep commands, but there was a problem and abc@ or @abc were caught as whitespace in my regexps. I want the output, in general, to simulate the following script's output (only the "Good" ones):

for res in $(grep -F 'abc' test.txt); do if [[ ! $res == *@* ]]; then echo $res is good; else echo $res is bad; fi; done

Upvotes: 0

Views: 141

Answers (2)

ysth
ysth

Reputation: 98508

No -P needed; -E is sufficient:

grep -E '(^|\s)abc(\s|$)'

or even without -E:

grep '\(^\|\s\)abc\(\s\|$\)'

Upvotes: 2

Reut Sharabani
Reut Sharabani

Reputation: 31339

After some time with Google I asked on the ask ubuntu chat room.

A user there was king enough to help me find the solution I was looking for and i wanted to share so that any following suers running into this may find it:

grep -P "(^|\s)abc(\s|$)" gives the result I was looking for. -P is an experimental implementation of perl regexps.

grepping for abc and then using filters like grep -v '@abc' (this is far from perfect...) should also work, but my patch does something similar.

Upvotes: 0

Related Questions