1828225 Members
2392 Online
109975 Solutions
New Discussion

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...
spex
Honored Contributor

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
Geoff Wild
Honored Contributor

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.
James R. Ferguson
Acclaimed Contributor

Re: Awk field seperator

Hi (again):

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

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor
Solution

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.

albanese
Occasional Advisor

Re: Awk field seperator

I prefer James R. Ferguson Solution.. Cheers.
maliaka
Advisor

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.
Peter Nikitka
Honored Contributor

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

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
Denver Osborn
Honored Contributor

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
Denver Osborn
Honored Contributor

Re: Awk field seperator

also w/ awk using your syntax modified...

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

-denver
maliaka
Advisor

Re: Awk field seperator

Denvor,

the NR doesn't work?
Denver Osborn
Honored Contributor

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
Hein van den Heuvel
Honored Contributor

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.




maliaka
Advisor

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
Peter Nikitka
Honored Contributor

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"