Please explain awk command
Please explain awk command
am 27.08.2007 19:47:08 von onkelheinz
Hi,
a few days ago Ed Morton gave hints selecting lines from a file.
Can anyone explain me the following:
e) Print the N records after some pattern:
awk 'c&&c--;/pattern/{c=N}' file
I checked it and it worked fine, but I don't know exactly how it works.
For example what's the meaning of c&&c-- or for the first three loops
what are the assignments for c or why needn't I a print-statement to print
the output lines?
This is my input file:
MARKER
row1
row2
row3
row4
MARKER
row5
row6
row7
MARKER
....
pattern=MARKER
N=2
Output:
row1
row2
row5
row6
Thanks
Heinz
Re: Please explain awk command
am 27.08.2007 20:23:30 von Michael Tosch
Heinz Müller wrote:
> Hi,
>
> a few days ago Ed Morton gave hints selecting lines from a file.
> Can anyone explain me the following:
>
> e) Print the N records after some pattern:
>
> awk 'c&&c--;/pattern/{c=N}' file
>
> I checked it and it worked fine, but I don't know exactly how it works.
> For example what's the meaning of c&&c-- or for the first three loops
> what are the assignments for c or why needn't I a print-statement to print
> the output lines?
>
....
You can write
c&&c--;
as
c!=0&&c--!=0
or
c!=0{c--;print}
When the expression is true and {} is omitted,
print is the default action.
--
Michael Tosch @ hp : com
Re: Please explain awk command
am 27.08.2007 21:04:51 von Bill Marcum
On Mon, 27 Aug 2007 19:47:08 +0200, Heinz Müller
wrote:
>
>
> Hi,
>
> a few days ago Ed Morton gave hints selecting lines from a file.
> Can anyone explain me the following:
>
> e) Print the N records after some pattern:
>
> awk 'c&&c--;/pattern/{c=N}' file
>
> I checked it and it worked fine, but I don't know exactly how it works.
> For example what's the meaning of c&&c-- or for the first three loops
> what are the assignments for c or why needn't I a print-statement to print
> the output lines?
>
"c&&c--" if c != 0, subtract 1 from c
The semicolon, outside braces, terminates a pattern-action pair. A null
action is equivalent to {print}.
--
"Show business is just like high school, except you get paid."
-- Martin Mull
Re: Please explain awk command
am 27.08.2007 21:20:47 von onkelheinz
"Bill Marcum" schrieb im Newsbeitrag
news:r8ibq4-ihq.ln1@don.localnet...
>>
> "c&&c--" if c != 0, subtract 1 from c
> The semicolon, outside braces, terminates a pattern-action pair. A null
> action is equivalent to {print}.
So,
one can write:
awk 'c&&c--{ print $0 };
/pattern/{c=N}' file
instead?
Heinz
Re: Please explain awk command
am 28.08.2007 11:16:30 von Michael Tosch
Heinz Müller wrote:
> "Bill Marcum" schrieb im Newsbeitrag
> news:r8ibq4-ihq.ln1@don.localnet...
>> "c&&c--" if c != 0, subtract 1 from c
>> The semicolon, outside braces, terminates a pattern-action pair. A null
>> action is equivalent to {print}.
>
> So,
>
> one can write:
>
> awk 'c&&c--{ print $0 };
> /pattern/{c=N}' file
>
> instead?
>
> Heinz
>
>
>
Yes, and with an explicit {action}
you can even use an explicit if():
awk '{if(c&&c--){print $0}}
{if(/pattern/){c=N}}'
--
Michael Tosch @ hp : com
Re: Please explain awk command
am 29.08.2007 18:22:01 von Ed Morton
Heinz Müller wrote:
> "Bill Marcum" schrieb im Newsbeitrag
> news:r8ibq4-ihq.ln1@don.localnet...
>
>>"c&&c--" if c != 0, subtract 1 from c
>>The semicolon, outside braces, terminates a pattern-action pair. A null
>>action is equivalent to {print}.
>
>
> So,
>
> one can write:
>
> awk 'c&&c--{ print $0 };
> /pattern/{c=N}' file
>
> instead?
Not quite. It'd be (semicolon removed):
awk 'c&&c--{ print $0 }
/pattern/{c=N}' file
if you wanted to redundantly specify the default action.
Ed.