Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Posts
    52

    Thumbs up Unanswered: How to recognize patterns in a file

    Hi everybody.

    I have being trying to solve this with awk but there is always something
    missing. I have one file like this :

    CONFIGURATION : 1
    a b c 2.5
    aa b1 c1 1.3
    a2 b2 c2 4.9
    ...
    aN bN cN 8.7
    CONFIGURATION : 2
    z d g 4.2
    aa d1 g1 12.3
    z2 d2 g2 4.
    ...
    zN dN gN 6.4
    CONFIGURATION : 3
    t e h 54.
    CONFIGURATION : 4
    CONFIGURATION : 5
    ...
    ...
    CONFIGURATION : M
    w y i 0.
    aa y1 i1 1.
    w2 y2 i2 4.2
    ...
    wN yN iN 2.4

    As you can see, between the lines containing the word CONFIGURATION
    there is a number of lines (0 to N), each of them contains 4 columns.

    What I need to do is quite simple but painful without a good script.
    For each configuration, I must look for the word "aa". When I have
    a hit, I must print the third and fourth columns and the configuration number.

    I know how to do this with awk. The problem is when I do NOT have
    hit. In the lines above, configurations 4 and 5 shows nothing.
    I could not figure out how to build a logic flag yet.

    Therefore, the output for the example above should be :

    (configuration) (third column) (fourth column)
    1 c1 1.3
    2 g1 12.3
    3 0 0
    4 0 0
    5 0 0
    ...
    M i1 1.

    I appreciate any help !

    Thanks,

    Serg

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Code:
    BEGIN {
      FS=":"
    }
    
    $1 ~ "^CONFIGURATION" { block = $2 ; next }
    { print block, $0 }

  3. #3
    Join Date
    Feb 2004
    Posts
    52
    Thanks for the reply.

    How do I execute your script ?
    I tried using awk but it did not work :

    [serg@galactic test]$ awk -f sh.script data
    1 a b c 2.5
    1 aa b1 c1 1.3
    1 a2 b2 c2 4.9
    1 aN bN cN 8.7
    2 z d g 4.2
    2 aa d1 g1 12.3
    2 z2 d2 g2 4.
    2 zN dN gN 6.4
    3 t e h 54.
    5

    Thanks,

    Serg

  4. #4
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Sorry - I thought you needed the ENTIRE line printed.
    Here's the modified version and yes, that's how you would run script.

    Code:
    BEGIN {
      FS=":"
    }
    
    $1 ~ "^CONFIGURATION" { block = $2 ; next }
    { print block, $3, $4 }

  5. #5
    Join Date
    Feb 2004
    Posts
    52
    Excellent ! It worked nicely !

Posting Permissions

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