Help on awk scripting!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Martian
    New Member
    • Aug 2007
    • 1

    #1

    Help on awk scripting!!

    Hello, I am really having a trouble on something that at first glance seemed easy to do in awk...
    I am attempting to reformat a file that has two colums, such as below:

    201 84
    201 370
    201 544
    201 600
    213 99
    213 250
    213 431
    220 65
    220 129
    220 338
    220 408
    220 501
    220 550
    231 101
    231 350

    The idea is to add a third column containing a numeric value (a sort of flag) that increments by a value of 1 (one) as the first column value changes (the value of the first column itself is irrelevant, the important thing is to track when the value changes..). The output should be something like this:

    201 84 1
    201 370 1
    201 544 1
    201 600 1
    213 99 2
    213 250 2
    213 431 2
    220 65 3
    220 129 3
    220 338 3
    220 408 3
    220 501 3
    220 550 3
    231 101 4
    231 350 4

    ...and so on...

    I have tried with the arithmetic and incremental operators but I did not get good results. I will deeply appreciate any help on this from any of the awk gurus..!

    Thanks!
  • shimeng
    New Member
    • Aug 2007
    • 1

    #2
    Originally posted by Martian
    Hello, I am really having a trouble on something that at first glance seemed easy to do in awk...
    I am attempting to reformat a file that has two colums, such as below:

    201 84
    201 370
    201 544
    201 600
    213 99
    213 250
    213 431
    220 65
    220 129
    220 338
    220 408
    220 501
    220 550
    231 101
    231 350

    The idea is to add a third column containing a numeric value (a sort of flag) that increments by a value of 1 (one) as the first column value changes (the value of the first column itself is irrelevant, the important thing is to track when the value changes..). The output should be something like this:

    201 84 1
    201 370 1
    201 544 1
    201 600 1
    213 99 2
    213 250 2
    213 431 2
    220 65 3
    220 129 3
    220 338 3
    220 408 3
    220 501 3
    220 550 3
    231 101 4
    231 350 4

    ...and so on...

    I have tried with the arithmetic and incremental operators but I did not get good results. I will deeply appreciate any help on this from any of the awk gurus..!

    Thanks!
    Assume you have the columns above in a file call list.txt (no extra newline/CRLF at the EOF), and the following awk script in doit.awk.

    BEGIN {
    counter = 0
    prevval = 0
    }

    {
    if ($1 != preval){
    counter += 1
    preval = $1
    }
    printf "%d %d %d\n", $1, $2, counter
    }

    END {
    printf "Hooha!"
    }


    Just run:

    awk -f doit.awk list.txt > list2.txt

    Desired output will be in list2.txt.

    This script compares the first column to the previous known value. If they are different, the counter variable gets incremented, and is printed as an additional column.

    Comment

    • radoulov
      New Member
      • Jun 2007
      • 34

      #3
      Or:

      Code:
      awk '!x[$1]++{c++};$3=c' infile

      Use nawk on Solaris.

      Comment

      Working...