#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Help with AWK !!!


    Hi all,
    I am completely new to awk. I really need some help.
    I want to write a script to suppress all the lines between "reference1" and "reference2"

    I tried something like:

    BEGIN {doprint=0}
    {
    match($0,"reference1") == 1 {doprint=0;};
    match($0,"reference2") == 1 {doprint=1;} ;
    if (doprint==1){print $0} }
    }


    without success.
    Anyone can help ?


    thanks a lot !!!!

    Julien
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    You can do this more easily with sed:

    sed -n '/reference1/,/reference2/p' filename

    The -n flag suppresses the default behaviour of printing every line.

    /regex1/,regex2/p prints the lines from the one that matches regex1 up to the one that matches regex2.

    Your awk program should also work I think, but is a bit verbose. You can simplify it to this:
    Code:
    /reference1/ {doprint = 1}
    /reference2/ {doprint = 0}
    { if (doprint) {print $0}}
    Variables are magically created when first used and initialised to 0 or "" depending on the context.
    You can match lines using /regex/ instead of using the match function.

    Dave
  4. #3
  5. PixHost.tk Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    58
    Rep Power
    0

    Exclamation


    Just one other thing: put a better title in your questions, like what you really need and please put the code tags on your code.
    Like this:
    Code:
    BEGIN {doprint=0}
    {
    match($0,"reference1") == 1 {doprint=0;};
    match($0,"reference2") == 1 {doprint=1;} ;
    if (doprint==1){print $0} }
    }
    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo