Unix Shell - string begin with an ip how to check?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2007 > string begin with an ip how to check?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author string begin with an ip how to check?
flyzone@technologist.com

2007-02-23, 1:18 pm

I have a string, and i need to check into a SH script if it begin with
an ip, then to do an operation with that string.
I have search on internet about sed and a regexp but it continue to
give me error.
I tried so to check if the first char are a number with a fullstop
with:

if [[ $(echo $row | cut -d "." -f1) -eq "10" ]]; then

but it's wrong.

I have found this on internet, but the shell doesn't reply
sed 's|.* \([[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\).*|
\1|'`

I can do it just with SH, no other shell available.
I need to learn about regexp...i know..... :-(

Hope someone could help me.
Thanks in advance

Andrew Smallshaw

2007-02-23, 1:18 pm

On 2007-02-23, flyzone@technologist.com <flyzone@technologist.com> wrote:
> I have a string, and i need to check into a SH script if it begin with
> an ip, then to do an operation with that string.
> I have search on internet about sed and a regexp but it continue to
> give me error.
> I tried so to check if the first char are a number with a fullstop
> with:
>
> if [[ $(echo $row | cut -d "." -f1) -eq "10" ]]; then


if echo $row | grep -Eq "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"
then
(...)
fi

A couple of provisos: it won't catch a nonsense IP such as 256.1.1.1,
nor will it IPv6 addresses, but for the time being at least that's
unlikely to be a problem.

--
Andrew Smallshaw
andrews@sdf.lonestar.org
Bo Yang

2007-02-24, 1:23 am

flyzone@technologist.com :
> I have a string, and i need to check into a SH script if it begin with
> an ip, then to do an operation with that string.
> I have search on internet about sed and a regexp but it continue to
> give me error.
> I tried so to check if the first char are a number with a fullstop
> with:
>
> if [[ $(echo $row | cut -d "." -f1) -eq "10" ]]; then
>

I bet there is no error in the above line.
So, what is wrong?
> but it's wrong.
>
> I have found this on internet, but the shell doesn't reply
> sed 's|.* \([[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\).*|
> \1|'`
>

What do you mean by saying your shell does not reply?
Your env does not have the command sed, does it?

> I can do it just with SH, no other shell available.
> I need to learn about regexp...i know..... :-(
>
> Hope someone could help me.
> Thanks in advance
>

flyzone@technologist.com

2007-02-25, 7:16 pm

On 24 Feb, 03:05, Bo Yang <strug...@mail.nankai.edu.cn> wrote:
>
> I bet there is no error in the above line.
> So, what is wrong?


No syntax error, but "logical" is wrong, i check the first 2 number of
an ip....for now it's ok because i need to check just a list of ip
beginning 10.x.x.x .. but in the future? who will remember of that
in case of error with mine program?

>
> What do you mean by saying your shell does not reply?
> Your env does not have the command sed, does it?


Sed is installed in the system, but it wait for the continue of
the line with the ">" console-char, like if the syntax is wrong.

Someone know a really good and complete guide about sed/grep/regexp?

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com