|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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-- rint}When the expression is true and {} is omitted, print is the default action. -- Michael Tosch @ hp : com |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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-- rint}When the expression is true and {} is omitted, print is the default action. -- Michael Tosch @ hp : com |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Heinz Müller wrote:
> "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 > > > 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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Heinz Müller wrote:
> "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 > > > 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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
Heinz Müller wrote:
> "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? 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. |
|
![]() |
| Outils de la discussion | |
|
|