0

I'm trying to use grep to find non-plural instances of a particular string.

For example, I want to find all instances of "string", but leave out any instances of "strings".

How can I do this?

Edit: I do want to find instances that are followed by other characters. I literally just need to leave out the ones that end in 's'

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
user2824889
  • 1,085
  • 5
  • 16
  • 28
  • 1
    To make answerers can easily figure out your problem, please ad some example with expected result. – fronthem Feb 26 '16 at 16:31
  • You should get rid of the text about finding non-plurals since that's an extremely difficult problem given cases like `octupus/octopi`, bus/busses` and `sheep/sheep`. Just keep your question focused on what you want - to delete words that end in `s` so, for example, `bus` will be deleted even though singular and `octopi` will remain even though plural. – Ed Morton Feb 26 '16 at 22:20

3 Answers3

2

You can use word boundaries in grep:

grep '\<string\>' file

Examples:

# with word boundaries
grep '\<string\>' <<< $'string\nstrings'
string

# without word boundaries
grep 'string' <<< $'string\nstrings'
string
strings

EDIT: Based on comments below you can use:

grep -E '\<string([^s]|$)' file

Example:

grep -E '\<string([^s]|$)' <<< $'string\nstrings'
string
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • The tricky thing (sorry for being unclear) is that I am looking for special characters. This is in a python script, so I am looking for instances that have a colon, left brace, new line, etc. I literally just need to leave out instances that end in 's' – user2824889 Feb 26 '16 at 15:48
  • 1
    Yep! Thank you very much – user2824889 Feb 27 '16 at 23:02
1

You can use \b to match a word boundary. Hope this helps:

[root@rh57quinn ~]# echo 'find instances of a particular string - any "string", but leave out "strings" and drawstring' |grep -Eo '\bstring\b'
string
string
Quinn
  • 4,394
  • 2
  • 21
  • 19
1

If you use a general grep you can done it in this way

echo "stringsMystring1string2strings" | grep -Eo 'strings?' | grep -Eo '^string$'

The idea is you just list all strings by using both pattern string and strings first, by

command:

`grep -Eo 'strings?'`

results:

strings
string
string
strings

The grep the result again by

command:

grep -Eo '^string$'

result:

string
string

And according to regex-lookahead-for-not-followed-by-in-grep, some people suggest to use GNU grep where you can use an option -P or --perl-regexp to enable lookaround feature. A given regex might take form like this

echo "stringsMystring1string2strings" | grep -P 'string(?!s)'
Community
  • 1
  • 1
fronthem
  • 4,011
  • 8
  • 34
  • 55