Finding strings in a file using regex and grep in Linux -
i trying find proper regexes use grep command on file text.txt
.
question
find occurrences of words in text have substring ad, bd, cd, dd, ed.
find occurrences of numbers > 100
find occurrences of numbers > 100 contain digit 0 or 5
my approach
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
grep -io '[199][1-9]*' text
i believe way off on regex, still prints correct result.
1973 197 17775
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
Post a Comment