Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 584
  • Last Modified:

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?


0
bganoush
Asked:
bganoush
  • 2
1 Solution
 
svgmucCommented:
I'd use sed...

sed -e "s/^.*(\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b).*$/\1/g" file.txt
0
 
bganoushAuthor Commented:
Thanks....
0
 
bganoushAuthor Commented:

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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now