1748259 Members
3586 Online
108760 Solutions
New Discussion юеВ

Re: Awk field seperator

 
SOLVED
Go to solution
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"
Hein van den Heuvel
Honored Contributor

Re: Awk field seperator

Are these questions bulding up on one another? Does this new double match need to occur on the first line, or any line?
If it could be any line, then you need to read on until match or the END. If the END is reached, return other status:

awk '($1 ~ /^[0-9]+$/ && ($2 ~ /^[a-zA-Z0-9]+$/) {print; exit 0} END {exit 1}' tmp.txt

It's time to read that awk man page or book my friend!

hth,

Hein

Peter Nikitka
Honored Contributor

Re: Awk field seperator

Hi,

to clarify the difference of Hein's solution to mine.

My solution aborts at the first line NOT containing twu fields, where the first consists of digits only.

Hein's solution terminates at the first correct entry with status 0; the return value is 1 when not a single correct field was found.

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"
Sandman!
Honored Contributor

Re: Awk field seperator

Not sure if i understand your requirement but based on your posts try the awk construct below:

# awk '/hell/ && !/awk/ {print (($3=="")?0:$3);exit}' file

~cheers
maliaka
Advisor

Re: Awk field seperator

closed