Operating System - HP-UX
1753894 Members
7659 Online
108809 Solutions
New Discussion юеВ

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 ...)