Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Outputting a section of text containing an IP address

12 views
Skip to first unread message

Mark Hobley

unread,
Aug 3, 2008, 1:04:56 PM8/3/08
to
I have a data file that contains a load of junk with an IP address
embedded on each line as follows:

qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp
ritutoe@7655098&totitottp23.7...@oioikm.nuiyill(%@45)
deriii ererk34.228 r...@jojo.net dkowod!kk:^ 37.232.78.18m(45)

I want to extract just the IP address information using a regular
expression. I think the expression that I need looks something like the
following, with the backslash symbols being used to suppress
interpretation of the dot symbols as part of the expression.

/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/

How do I limit the output to just the matched IP addresses, discarding
the rest of the line as follows:

123.28.49.59
23.78.249.248
37.232.78.18

Can I do this using sed?

Mark.

--
Mark Hobley,
393 Quinton Road West,
Quinton, BIRMINGHAM.
B32 1QE.

JC

unread,
Aug 5, 2008, 12:18:11 AM8/5/08
to
"Mark Hobley" wrote...

Yes, but I don't have the regular expression for you. I've
used SED in the past to delete lines out of log files with
a specific IP address. It works great.

Ok, I think this will work for you.

/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/

The + tells it to match 1 or more characters in the match request.
An asterisk, on the other hand, requests matching 0 or more such
characters, so I do not think that works. I'm looking through my
Perl book at the moment on regular expressions, which indicates that
the regular expression inside of Perl matches that of grep, sed and
awk. It also indicates that some minor differences exist for pattern
matching rules in the different programs.

Periods, at least in Perl, represent any character at all, and to
match a period it needs escaping with a backslash.

The sed manual is located here:

http://www.gnu.org/software/sed/manual/sed.html

Let me know if the line I provided works for you, please.

Thanks.

--
JC
Natural Cure For Pink-Eye (Conjunctivitis)
http://www.associatedcontent.com/article/381336/saliva_a_natural_cure_for_conjunctivitis.html


Mark Hobley

unread,
Aug 5, 2008, 7:58:35 AM8/5/08
to
JC <jcar...@127.0.0.1> wrote:
> Ok, I think this will work for you.
>
> /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/
>

echo 'qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp' |
sed '/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/'
sed: -e expression #1, char 32: missing command

I need a command to output only the text that matches the expression.
What command should I be using here?

Mark Hobley

unread,
Aug 14, 2008, 12:00:03 AM8/14/08
to
Mark Hobley <markh...@hotpop.donottypethisbit.com> wrote:

> I need a command to output only the text that matches the expression.
> What command should I be using here?

I keep getting the full line here:

echo 'qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp' |
sed '/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/p'
qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp

echo 'qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp' |
sed 's/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/&/'
qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp

echo 'qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp' |
sed '/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/n'
qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp

echo 'qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp' |
sed '/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/q'
qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp

echo 'qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp' |
sed '/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/x'
qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp

qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp
------------
|
I just want this bit

JC

unread,
Aug 17, 2008, 11:02:32 PM8/17/08
to
"Mark Hobley" <markh...@hotpop.donottypethisbit.com> wrote...

>I keep getting the full line here:
>echo 'qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp' |
>sed '/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/x'
>qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp
>
>qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp
> ------------
> |
> I just want this bit

Are you limited to using SED? I'm thinking Perl or another script-
ing language might work better. I'm not a SED expert and the news-
group here is very slow. I tried a few times to figure out how to
not get the whole line printed, but I couldn't find anything. I
ran across a couple links that might help out, though. I just do
not have the time to go through it myself.

http://sed.sourceforge.net/
http://sed.sourceforge.net/sedfaq.html

I did not download the version of SED from the sourceforge link
listed above, I got mine from unxutils.sourceforge.net, and I can
not match it up to a version listed at one of the links above.

I've used SED in the past to delete certain lines with a specific
IP address, or to view only certain lines in Apache log files. So
I'm guessing SED works line-by-line, displaying whole lines.

The source code for SED can be found at the links above, if you're
willing to spend the time and look at the source. It appears you've
spent a lot of time on this.

My apologies that what I posted did not help. I tend to another
editor, also available at sourceforge, SciTE which is based upon the
scintilla library at http://scintilla.sourceforge.net/. SciTE has a
regular expression parser that works in a similar line-by-line way,
but you can easily edit any text file and extract only the IP number
on each line. The code I posted to extract the IP number was based
upon what I'd use in SciTE.

Hope this helps.

--
Jim Carlock

Cesar Inacio Martins

unread,
Aug 26, 2008, 9:59:31 AM8/26/08
to
Try this, work with the example you send...but need to test over others
texts...

# echo "qwer(023)796...@liverpool12.8ero&roir123.28.49.59(t95)@torprp
ritutoe@7655098&totitottp23.7...@oioikm.nuiyill(%@45)
deriii ererk34.228 r...@jojo.net dkowod!kk:^ 37.232.78.18m(45)" > x
# cat x | sed -e
"s/\(.*[^0-9]\)\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)\(.*\)/\2/"
123.28.49.59
23.78.249.248
37.232.78.18

Regards
Cesar

0 new messages