|
Home > Archive > Unix Shell > August 2007 > Please explain awk command
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 |
Please explain awk command
|
|
| Heinz Müller 2007-08-27, 1:25 pm |
| 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
| |
| Michael Tosch 2007-08-27, 1:25 pm |
| 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
| |
| Bill Marcum 2007-08-27, 7:22 pm |
| On Mon, 27 Aug 2007 19:47:08 +0200, Heinz Müller
<onkelheinz@mscologne.de> 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
| |
| Heinz Müller 2007-08-27, 7:22 pm |
|
"Bill Marcum" <marcumbill@bellsouth.net> 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
| |
| Ed Morton 2007-08-29, 1:20 pm |
| Heinz Müller wrote:
> "Bill Marcum" <marcumbill@bellsouth.net> schrieb im Newsbeitrag
> news:r8ibq4-ihq.ln1@don.localnet...
>
>
>
> 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.
|
|
|
|
|