Solved

Extracting a specific pattern from a file...

Posted on 2011-03-21
3
574 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Call Shell Script from Perl Script 6 195
spectrum scale snapshot  resotre/mount 1 58
AWK How do I reorder the columns in a csv 7 79
FTP Script 25 88
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.

738 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