Finding strings in a file using regex and grep in Linux -


i trying find proper regexes use grep command on file text.txt.

question

  1. find occurrences of words in text have substring ad, bd, cd, dd, ed.

  2. find occurrences of numbers > 100

  3. find occurrences of numbers > 100 contain digit 0 or 5

my approach

  1. grep -io '[a-e]*d' text

    prints words proper substrings, doesn’t print whole string/word.

    ad d d ed d d ed d d d d ed d d 
  2. grep -io '[199][1-9]*' text

    i believe way off on regex, still prints correct result.

    1973 197 17775 
  3. grep -io '[05][1-9]*' text

    this continuation of 2., don’t understand 2. part in 3., believe have string containing digit 0 or 5 correct.

    0 0 0 5 

a) find occurrences of words in text have substring ad, bd, cd, dd, ed.

grep -ow '.*\(a\|b\|c\|d\|e\)d.*' text 

or

egrep -ow '.*(a|b|c|d|e)d.*' text 

b) find occurrences of numbers > 100

grep -ow '[1-9][0-9][0-9]\+' text 

c) find occurrences of numbers > 100 contain digit 0 or 5

grep -ow '[1-9][0-9][0-9]\+' text | grep '\(0\|5\)' 

or

grep -ow '[1-9][0-9][0-9]\+' text | egrep '(0|5)' 

i'm using option -o output every match on it's own line , not whole line pattern found , option -w specifies before , after match should word boundary.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -