1751979 Members
4769 Online
108784 Solutions
New Discussion юеВ

Re: Awk field seperator

 
SOLVED
Go to solution
maliaka
Advisor

Awk field seperator

Hello,

echo hello1 192.168.1.9|awk 'BEGIN { FS = "." } ; { print $1"."$2"."$3}'
hello1 192.168.1

I only need to pring the first three octats from the second field and delete the first field (hello1).

The echo string is an output from a loop.

Thanks


28 REPLIES 28
Steven E. Protter
Exalted Contributor

Re: Awk field seperator

Shalom,

I have some non awk code that I was given on ITRC that does this job.

IP=`expr "${LINE}" : ".*[^0-9]\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*"`

The expression above grabs the first three octets of an ip address.

It doesn't use awk though.

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
Steven E. Protter
Exalted Contributor

Re: Awk field seperator

Hey, there is some awk code after all.

OCT1=`echo $IP | awk -F. '{print $1}'`
OCT2=`echo $IP | awk -F. '{print $2}'`
OCT3=`echo $IP | awk -F. '{print $3}'`
IPB=`echo $OCT1.$OCT2.$OCT3`

After what I posted.

Sorry for splitting the post, it was not intentional. Please provide me the slapping I so richly deserve in the point assignment phase(e.g. don't assign to both posts).

Thanks.

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
maliaka
Advisor

Re: Awk field seperator

I know that I can use echo strings to split the fields as much as I know there is away to do all of that in one line.

10 points for the right answer.
albanese
Occasional Advisor

Re: Awk field seperator

Try This one

echo hello1 192.168.1.9 | awk 'BEGIN { FS = "." } ; { print substr($1,8,11)"."$2"."$3}'

result

192.168.1
albanese
Occasional Advisor

Re: Awk field seperator

It gives the same output the previous one has an extra character. but doest make a difference.

echo hello1 192.168.1.9 | awk 'BEGIN { FS = "." } ; { print substr($1,8,10)"."$2"."$3}'
Yarek
Regular Advisor

Re: Awk field seperator

pls try:

echo hello1 192.168.1.9 | awk '{print $2}' |
awk -F"." '{print $1"."$2"."$3}'

output should be:

192.168.1



rgds
Peter Godron
Honored Contributor

Re: Awk field seperator

echo hello1 192.168.1.9 | cut -d' ' -f 2 | cut -d'.' -f1-3
Peter Nikitka
Honored Contributor

Re: Awk field seperator

Hi,

because the first field is 'hello1 192' in respect to the filed delimiter '.', you get what awk was told to do.

echo hello1 192.168.1.9|awk '{split($2,a,"."); printf("%s.%s.%s\n" a[1],a[2],a[3])}'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: Awk field seperator

HI:

# echo hello1 192.168.1.9|awk -F"[ .]" 'BEGIN{OFS="."};{print $2,$3,$4>

192.168.1

Note that the input field separator is a character class consisting of a space (blank) or a dot (".").

Regards!

...JRF...