1751962 Members
5016 Online
108783 Solutions
New Discussion юеВ

Re: Awk field seperator

 
SOLVED
Go to solution
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