Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Extracting a specific pattern from a file...

Posted on 2011-03-21
3
Medium Priority
?
579 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
svgmuc earned 2000 total points
ID: 35180759
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
ID: 35180780
Thanks....
0
 

Author Comment

by:bganoush
ID: 35180959

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.
Suggested Courses

721 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