Link to home
Start Free TrialLog in
Avatar of bganoush
bganoush

asked on

Extracting a specific pattern from a file...

Hi,

I have a ksh script which looks through various logs for the ip address pattern as well as email address pattern and other pattern still to be defined.  How do I extract the found patterns from the file?  I tried using awk but it seems that I need to know the position of the found pattern to get a proper output:

For instance, I tried this:

cat file.txt |  awk '/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/{print $5}'

but this assumes that the ip is found in the fifth position in the file (this doesn't happen all the time).

How do I extract just the item i want?

I tried this also but I'm not sure how to do the print part of the statement:

cat file.txt |  awk '/\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)/{print \1}'

I don't think my awk knows about "\1".... or maybe this is only good for sed?


ASKER CERTIFIED SOLUTION
Avatar of svgmuc
svgmuc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bganoush
bganoush

ASKER

Thanks....

Sorry.. one last question.. this script tends to only show the last occurence of the search string in a line. If there are two ips in one line, then only the second one is shown.