Solved

Extracting a specific pattern from a file...

Posted on 2011-03-21
3
561 Views
Last Modified: 2012-06-27
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
Comment
Question by:bganoush
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
svgmuc earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:bganoush
Comment Utility
Thanks....
0
 

Author Comment

by:bganoush
Comment Utility

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now