Home > Archive > AWK > April 2007 > print first line (sort of)?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
print first line (sort of)?
|
|
| Tomasz Chmielewski 2007-04-26, 6:56 pm |
| I have a file in the following format:
tid:43 name:iqn.2007-04.net....
sid:649644246252192256
tid:42 name:iqn.2007-04.net....
sid:645703596578243072 initiator:iqn.2006-11.net...
tid:41 name:iqn.2007-04.net....
sid:646266546531664384
I want to get the highest number which stands next to tid: - in that
case, that would be 43.
So our delimiter is : or space, and we print 2nd column:
$ awk -F ":| " '/^tid/{print $2}' < file
43
42
41
To print the first line, I'd normally use "NR>1{exit};1", but in this
case, I don't know how to combine it together with "print".
And a bonus question - don't print the first line, but the highest number?
--
Tomasz Chmielewski
| |
| Tomasz Chmielewski 2007-04-26, 6:56 pm |
| Tomasz Chmielewski wrote:
> I have a file in the following format:
>
> tid:43 name:iqn.2007-04.net....
> sid:649644246252192256
> tid:42 name:iqn.2007-04.net....
> sid:645703596578243072 initiator:iqn.2006-11.net...
> tid:41 name:iqn.2007-04.net....
> sid:646266546531664384
>
>
> I want to get the highest number which stands next to tid: - in that
> case, that would be 43.
>
> So our delimiter is : or space, and we print 2nd column:
>
> $ awk -F ":| " '/^tid/{print $2}' < file
> 43
> 42
> 41
>
>
> To print the first line, I'd normally use "NR>1{exit};1", but in this
> case, I don't know how to combine it together with "print".
>
> And a bonus question - don't print the first line, but the highest number?
Seems that will print the first line I want:
awk -F ":| " '/^tid/ {print $2} NR>1{exit};0'
So a "bonus part" remains - how to print the highest number?
--
Tomasz Chmielewski
| |
| Ed Morton 2007-04-26, 6:56 pm |
| Tomasz Chmielewski wrote:
> Tomasz Chmielewski wrote:
>
>
>
> Seems that will print the first line I want:
>
> awk -F ":| " '/^tid/ {print $2} NR>1{exit};0'
Just curious - what do you think the trailing ";0" does? Assuming your
awk handles REs as FSs and that the first line always starts with tid as
you say in your sample input, all you need is:
awk -F'[ :]' '{print $2; exit}'
> So a "bonus part" remains - how to print the highest number?
awk -F'[ :]' 'NR == 1{print $2} max < $2{max = $2} END{printf "%d\n",max}'
Regards,
Ed.
| |
| Ed Morton 2007-04-26, 6:56 pm |
| Ed Morton wrote:
> Tomasz Chmielewski wrote:
>
>
>
> Just curious - what do you think the trailing ";0" does? Assuming your
> awk handles REs as FSs and that the first line always starts with tid as
> you say in your sample input, all you need is:
>
> awk -F'[ :]' '{print $2; exit}'
>
>
>
> awk -F'[ :]' 'NR == 1{print $2} max < $2{max = $2} END{printf "%d\n",max}'
>
Oops:
awk -F'[ :]' 'NR == 1{print $2} /^tid/{ max = (max < $2 ? $2 : max) }
END{printf "%d\n",max}'
Ed.
| |
| Tomasz Chmielewski 2007-04-26, 6:56 pm |
| Ed Morton wrote:
> Tomasz Chmielewski wrote:
>
>
> Just curious - what do you think the trailing ";0" does?
In that case, 0 would mean "don't print the line that matches
NR>1{exit}". In other words, it would only print the first line matching
both /^tid/ and {print $2}.
But 0 is unnecessary here, true (and my example is probably crappy, too).
> Assuming your
> awk handles REs as FSs and that the first line always starts with tid as
> you say in your sample input, all you need is:
>
> awk -F'[ :]' '{print $2; exit}'
>
>
> awk -F'[ :]' 'NR == 1{print $2} max < $2{max = $2} END{printf "%d\n",max}'
Thanks.
--
Tomasz Chmielewski
| |
| Tomasz Chmielewski 2007-04-26, 6:56 pm |
| Ed Morton wrote:
> Ed Morton wrote:
>
>
> Oops:
>
> awk -F'[ :]' 'NR == 1{print $2} /^tid/{ max = (max < $2 ? $2 : max) }
> END{printf "%d\n",max}'
>
> Ed.
NR == 1{print $2} is unnecessary - you will get the number two times
(and the first one will not necessarily be the correct one).
The whole line should look like this, and can be used to retrieve the
highest tid number on a machine running iSCSI-target (just in case
someone googles it one day):
awk -F'tid:| name' 'NR == 1{print $2} /^tid/{ max = (max < $2 ? $2 :
max) } END{printf "%d\n",max}' < /proc/net/iet/session
Thanks Ed a lot.
--
Tomasz Chmielewski
| |
| Ed Morton 2007-04-26, 6:56 pm |
| Tomasz Chmielewski wrote:
> Ed Morton wrote:
>
>
>
> In that case, 0 would mean "don't print the line that matches
> NR>1{exit}". In other words, it would only print the first line matching
> both /^tid/ and {print $2}.
"NR>1{exit}" is not a condition/pattern to be matched. Neither is
"{print $2}". "/^tid/" and "NR>1" are the only parts of your script in
the condition section. awk scripts are composed of:
condition { action }
sections. The above script is:
/^tid/ = the first condition
{print $2} = the first action (executed when the first condition is true)
NR>1 = the second condition
{exit} = the second action (executed when the second condition is true)
; = a null statement (with no effect)
0 = a condition that can never be true (so also with no effect)
Ed.
|
|
|
|
|