Operating System - HP-UX
1829035 Members
2529 Online
109986 Solutions
New Discussion

Re: awk will not parse the way I want it to.

 
SOLVED
Go to solution
Steven E. Protter
Exalted Contributor

awk will not parse the way I want it to.

Because I'm a pathetic awk programmer.

Here's the code:

IP=192.168.0.50
OCT1=`echo $IP | awk -F: '/^[^#*]+./ {print $1}' -`
echo $OCT1

OCT2=`echo $IP | awk -F: '/^[^#*]+./ {print $2}' -`
echo $OCT2

OCT3=`echo $IP | awk -F: '/^[^#*]+./ {print $3}' -`
echo $OCT3

Output is

192.168.0.50

then two blank lines.

Intended output is

192
168
0

Yes, I'm trying to break up an IP adderss.

I tried some awk code that I use to pull the data from a colon : delimited oratab database, replaced the : with a dot . and get bad results.

Thanks in Advance for the help.

Points for any effort. Rabbit for any code that works.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
13 REPLIES 13
John Carr_2
Honored Contributor

Re: awk will not parse the way I want it to.

awk -F"=" '{ print $2}' | awk -F"." '{ print $1)'

you can work the rest out :-) John.
john korterman
Honored Contributor

Re: awk will not parse the way I want it to.

Hi,
# OCT1=`echo $IP | awk -F. '{print $1}'`

and of course OCT2 for $2 etc.

regards,
John K.
it would be nice if you always got a second chance
John Carr_2
Honored Contributor
Solution

Re: awk will not parse the way I want it to.

echo $IP | awk -F"=" '{ print $2}' | awk -F"." '{ print $1)'

echo $IP | awk -F"=" '{ print $2}' | awk -F"." '{ print $2)'

echo $IP | awk -F"=" '{ print $2}' | awk -F"." '{ print $3)'

ignore the last entry this is what i meant
:-) jOHN.
Steven E. Protter
Exalted Contributor

Re: awk will not parse the way I want it to.

Thank you gentlemen.

Thread closed.

My embarassment forgotten.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
John Carr_2
Honored Contributor

Re: awk will not parse the way I want it to.

SEP you put a big smile on my face and a new hat on my head
:-) John

off to dinner now see you all later
Steven E. Protter
Exalted Contributor

Re: awk will not parse the way I want it to.

I do so much like handing out hats.

Even by accident like this time.

itrc rocks!

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
curt larson_1
Honored Contributor

Re: awk will not parse the way I want it to.

just another way

print "$IP" | tr -d "\." | read oct1 oct2 oct3 oct4 rest

print "1st = $oct1\n2nd = $oct2\n3rd = $oct3"
Steven E. Protter
Exalted Contributor

Re: awk will not parse the way I want it to.

Nice work, oh wizardly one.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Jean-Louis Phelix
Honored Contributor

Re: awk will not parse the way I want it to.

Hi,

Just for fun and for those who don't know IFS in shell :

IP=192.168.0.50
OFS="$IFS" # save $IFS just in case
IFS=.
set +A OCT $IP
IFS="$OFS" # restore default IFS
echo ${OCT[0]} ${OCT[1]} ${OCT[2]} ${OCT[3]}
echo ${OCT[@]} # print whole array ...

Regards.
It works for me (© Bill McNAMARA ...)
James Lynch
Valued Contributor

Re: awk will not parse the way I want it to.

Since you orignially asked how to use awk to do this:

echo $IP | awk '{ FS ="." ; printf("%d\n%d\n%d\n", $1, $2, $3); }'

And produces the following:
192
168
0

JL
Wild turkey surprise? I love wild turkey surprise!
Jean-Louis Phelix
Honored Contributor

Re: awk will not parse the way I want it to.

Hi,

Getting IP fields is not so hard :^) . Even tr (lightest than awk or sed can do it) :

echo $IP | tr '.' '\012'

Setting variables is more useful in a script :

echo $IP | tr '.' ' ' | read OCT1 OCT2 OCT3 REMAINDER

Regards.
It works for me (© Bill McNAMARA ...)
Jean-Luc Oudart
Honored Contributor

Re: awk will not parse the way I want it to.

another one :

IP=192.168.0.50
echo $IP | awk '{split($1,tab,"."); printf("%d\n%d\n%d\n",tab[1],tab[2],tab[3]);
}'

Regards,
Jean-Luc
fiat lux
Steven E. Protter
Exalted Contributor

Re: awk will not parse the way I want it to.

I am bookmarking this post and assigning solution level pointage to this latest input.

That is in spite of the thread closed notice.

The input is on topic, tests out correctly and is much appreciated. Thats why my threads are never truly closed.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com