PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Please explain awk command
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Please explain awk command

Réponse
 
LinkBack Outils de la discussion
Vieux 27/08/2007, 18h47   #1
Heinz Müller
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Please explain awk command

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


  Réponse avec citation
Vieux 27/08/2007, 19h23   #2
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Please explain awk command

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
  Réponse avec citation
Vieux 27/08/2007, 19h23   #3
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Please explain awk command

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
  Réponse avec citation
Vieux 27/08/2007, 20h04   #4
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Please explain awk command

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
  Réponse avec citation
Vieux 27/08/2007, 20h04   #5
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Please explain awk command

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
  Réponse avec citation
Vieux 27/08/2007, 20h20   #6
Heinz Müller
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Please explain awk command


"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



  Réponse avec citation
Vieux 28/08/2007, 10h16   #7
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Please explain awk command

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
  Réponse avec citation
Vieux 28/08/2007, 10h16   #8
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Please explain awk command

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
  Réponse avec citation
Vieux 29/08/2007, 17h22   #9
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Please explain awk command

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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h18.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,22694 seconds with 17 queries