Hi,
I have a file with 500 Lines and I want to search for a pattern within this file which starts with sm_ and ends with ). However I just want to print the pattern only and not the entire line. How do I do it ?
Thanks,
p
Hi,
I have a file with 500 Lines and I want to search for a pattern within this file which starts with sm_ and ends with ). However I just want to print the pattern only and not the entire line. How do I do it ?
Thanks,
p
Hi,
GNU grep has the -o option that does what you want, it will only print the pattern, not the entire line...
from the man page:
-o, --only-matching Show only the part of a matching line that matches PATTERN.
Not tested...
awk 'match($0,"sm_.*)"){print substr($0,RSTART,RLENGTH)}' $FILE
Using sed...
sed 's/^.*\(PATTERN\).*$/\1/' yourFile
does not work, since it will print all 500 lines of the file, not just those lines which contain the pattern. A possible solution using sed would be...
sed -n '/sm_.*)/{s/^.*\(sm_.*)\).*$/\1/;p;}' $FILE
I must be half asleep today
This is what you want...
sed -n 's/^.*\(PATTERN\).*$/\1/p' yourFile
or even better (in terms of performance)...
sed -n '/PATTERN/s/^.*\(PATTERN\).*$/\1/p' yourFile
So for the OPs specific example...
sed -n '/sm_.*)/s/^.*\(sm_.*)\).*$/\1/p' yourFile
(which is pretty much what you have Ygor)