Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2004
    Posts
    6

    Unanswered: extracting specific lines in a text file

    Hi,

    New to scripting. need to go through a text file and extract IP addresses for "Special Users" only as listed in example file below, and write only the IP Addresses to another file. Any help much appreciated.

    File looks like this:

    -- Primary users--------------------------------------------------
    IPAddress | City | Country | Username | misc
    -----------------------------------------------------------------
    11.208.23.56 | NY | USA | user1 |
    -----------------------------------------------------------------
    -- Special users--------------------------------------------------
    IPAddress | City | Country | Username | misc
    -----------------------------------------------------------------
    11.208.23.56 | NY | USA | user1 |
    12.209.23.56 | LA | USA | user1 |
    -----------------------------------------------------------------

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    nawk -f hq.awk yourInput > yourSpecialOutput

    here's hq.awk

    Code:
    BEGIN {
      FS="[|]"
      TAGusers="users"
      TAGspecialusers="Special users"
      PAT_IPaddress="[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+"
    }
    
    $0 ~ TAGusers { block=0 }
    $0 ~ TAGspecialusers { block=1 }
    
    block && match($1, PAT_IPaddress) { print $1 }
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  3. #3
    Join Date
    Jul 2004
    Posts
    6

    re:extracting specific lines in a text file

    Wow, thanks! That works great. Is it possible to run that nawk body within a unix bash script, or must it be in separate file?

    hg

  4. #4
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Code:
    nawk '
    BEGIN {
      FS="[|]"
      TAGusers="users"
      TAGspecialusers="Special users"
      PAT_IPaddress="[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+"
    }
    
    $0 ~ TAGusers { block=0 }
    $0 ~ TAGspecialusers { block=1 }
    
    block && match($1, PAT_IPaddress) { print $1 }
    ' yourInput > yourSpecialOutput
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •