Re: Awk field seperator (483 Views)
Reply
Advisor
maliaka
Posts: 25
Registered: ‎01-07-2007
Message 1 of 29 (483 Views)
Accepted Solution

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


Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 2 of 29 (483 Views)

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
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 3 of 29 (483 Views)

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
Advisor
maliaka
Posts: 25
Registered: ‎01-07-2007
Message 4 of 29 (483 Views)

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.
Occasional Advisor
albanese
Posts: 11
Registered: ‎07-26-2006
Message 5 of 29 (483 Views)

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
Occasional Advisor
albanese
Posts: 11
Registered: ‎07-26-2006
Message 6 of 29 (483 Views)

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}'
Regular Advisor
Yarek
Posts: 92
Registered: ‎08-16-2006
Message 7 of 29 (483 Views)

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
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 8 of 29 (483 Views)

Re: Awk field seperator

echo hello1 192.168.1.9 | cut -d' ' -f 2 | cut -d'.' -f1-3
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 9 of 29 (483 Views)

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"
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 10 of 29 (483 Views)

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...
Honored Contributor
spex
Posts: 1,367
Registered: ‎05-14-1996
Message 11 of 29 (483 Views)

Re: Awk field seperator

Hi,

Change awk's field separator regex to recognize both . and space.

$ echo hello1 192.168.1.9 | awk -F'[. ]' '{print $2"."$3"."$4}'
192.168.1

PCS
Honored Contributor
Geoff Wild
Posts: 7,170
Registered: ‎06-04-2001
Message 12 of 29 (483 Views)

Re: Awk field seperator

Very nice spex - definately a 10 in my books!

I would have just piped through 2 awk statements - forgot about regex.

IE:

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


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 13 of 29 (483 Views)

Re: Awk field seperator

Hi (again):

Well, it appears that Spex and I agree on the solution :-))

Regards!

...JRF...
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 14 of 29 (483 Views)

Re: Awk field seperator

I would use a -F [ .] solution.
But you could take the default whitespace field seperator and substitute a period followed by numbers at the end of $2 with nothing:

awk '{sub (/.[0-9]+$/,"",$2); print $2}'

Or you can 'match' the first 3 groups of numbers seperated by a period and print that.
That would allow this to work in free formatted text, not just on 'the second field'.

awk '{match($0,/[0-9]+\.[0-9]+\.[0-9]+/); print substr($0,RSTART,RLENGTH)}'

Enjoy,
Hein.

Occasional Advisor
albanese
Posts: 11
Registered: ‎07-26-2006
Message 15 of 29 (483 Views)

Re: Awk field seperator

I prefer James R. Ferguson Solution.. Cheers.
Advisor
maliaka
Posts: 25
Registered: ‎01-07-2007
Message 16 of 29 (483 Views)

Re: Awk field seperator

Thanks a lot for the great solutions.

Have you guys tried to double quote the echo string?
By default the -F,FS and OFS are a space character so if you put the echo string in double quote, it preserves the white space and therefore mess it up if add spaces. Try it.

Hein has provided the best solution so far!!!

Open for debate.
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 17 of 29 (483 Views)

Re: Awk field seperator

Hi,

the result of
echo a b | prog
echo 'a b' | prog
echo "a b" | prog

or
prog $(echo a b)
...

will be the same. In the view of the 'echo' command, it's different - in the first case it sees two parameters, in the other one parameter. But the result is the same: 'echo' will send the two parameters to its output, seperated by a space - which is exactly the same in case 2 or 3, where the space is taken from the original input of the single parameter.

Different will be a scenario like this:
echo ab
echo 'ab'

Only in the first case the result would NOT contain a TAB, but a space!

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"
Advisor
maliaka
Posts: 25
Registered: ‎01-07-2007
Message 18 of 29 (483 Views)

Re: Awk field seperator

I have another question?

I would like to get only the first line from the output? (whatever the output might be).

awk '/'hell'/ && !/awk/ {print
$3}' /tmp/target.txt
HH
HH
HH
HH
Honored Contributor
Denver Osborn
Posts: 1,749
Registered: ‎06-23-2000
Message 19 of 29 (483 Views)

Re: Awk field seperator

to get the first line of the output, no matter what it may be... just pipe it to head -1.

awk '/'hell'/ && !/awk/ {print $2}' /tmp/target.txt |head -1

-denver
Honored Contributor
Denver Osborn
Posts: 1,749
Registered: ‎06-23-2000
Message 20 of 29 (483 Views)

Re: Awk field seperator

also w/ awk using your syntax modified...

awk 'NR>1{exit}/'hell'/ && !/awk/ {print $3}' /tmp/target.txt

-denver
Advisor
maliaka
Posts: 25
Registered: ‎01-07-2007
Message 21 of 29 (483 Views)

Re: Awk field seperator

Denvor,

the NR doesn't work?
Honored Contributor
Denver Osborn
Posts: 1,749
Registered: ‎06-23-2000
Message 22 of 29 (483 Views)

Re: Awk field seperator

yeah sorry, it didn't look right to me either after hitting submit. :(

Anyhow, I'm still a noob but using NR should be able to do it in a one liner.

I'll have to defer to an awk guru for the one liner... otherwise pipe to awk 'NR>1{exit};1' (sorta like head -1)

awk '/'hell'/ && !/awk/ {print $3}' /tmp/target.txt |awk 'NR>1{exit};1'

-denver
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 23 of 29 (483 Views)

Re: Awk field seperator

>> awk '/'hell'/ && !/awk/ {print $3}' /tmp/target.txt |awk 'NR>1{exit};1'

You Unix types... Always seem to think a solution needs a pipe or two to count.

You are in AWK. Stay there.
Let awk do the work!

First step:

whole line: awk '/test/ && (!i++) ' tmp.txt

field: awk '/test/ && (!i++){print $3} ' tmp.txt

Here we test for a piece of string, and if that succeeds test for a variable "i" and increment after the test. If the "i" test is zero, then print.

There is still a 'problem' with this solution. Awk will keep on reading and testing where no more data will ever be produced. So just print and tell it to go home:

Best solution:
awk '/test/ {print $3; exit}' tmp.txt

Enjoy,
Hein van den Heuvel
HvdH Performance Consulting.




Advisor
maliaka
Posts: 25
Registered: ‎01-07-2007
Message 24 of 29 (483 Views)

Re: Awk field seperator

Hien,

You da man!!!
Now, how can I get awk to return a value greater than 0 if the string returns null (nothing)?

Also, if I want to accept a file format of 2 fields. The first field is only numbers and second field is a mixed of numbers and characters and to return a value greater than 0 if the format isn't correct?

100100100100 helloworld
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 25 of 29 (483 Views)

Re: Awk field seperator

Hi,

I wonder how many question you can out into one single message ...

Your latest one:

>>
Also, if I want to accept a file format of 2 fields. The first field is only numbers and second field is a mixed of numbers and characters and to return a value greater than 0 if the format isn't correct?

100100100100 helloworld
<<

awk 'NF != 2 || $1 !~ /^[0-9]*$/ {exit 1}'

should do it.

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"
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.