[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: use grep, egrep to print matched pattern?



Hello all

> > How can grep or egrep pattern in a file and print the pattern only? I
> > will not print the whole line. e.g. to search the email address in
> > /var/log/mail.log.  I am wondering is it possible to achieve this
> > without writing a perl script?
> 
> Pipe the output to sed to trim the stuff on either side of the pattern.
> 
> grep 'localhost' myfile | sed 's/^\(.*?\)\(localhost\)\(.*?\)$/\2/'
> 
> or some such...
i.e.

sed -n 's/.*\([^ ]*@[^ ]*\).*/\1/p' myfile


Not with grep. But use sed:

  sed -e 's,.*(pattern-to-match).*,\1,' < /var/log/mail.log



Reply to: