#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    2
    Rep Power
    0

    Question on script using awk


    2131100 COVS/100C E|FWP AREAS|MIDWEST|
    2131100 COVS/100C E|FWP AREAS|CONSTRUCTION|
    2131100 COVS/100C E|FWP AREAS|FTC|
    2131100 COVS/100C E|FWP AREAS|FWP AREAS|
    2131100 COVS/100C E|FWP AREAS|CLEANING|
    2131100 COVS/100C E|FWP AREAS|FCRE|
    2131200 COVS/100C UPS|UPS ROOMS|CONSTRUCTION|
    2131200 COVS/100C UPS|UPS ROOMS|UPS ROOMS|
    2131200 COVS/100C UPS|UPS ROOMS|FCRE|
    2131200 COVS/100C UPS|UPS ROOMS|EMERGENCY|

    I am running a report that produces the output shown above. I am trying to use awk in the script to format the output to look like this

    2131100 COVS/100C E|FWP AREAS|MIDWEST|CONSTRUCTION|FTC|FWP AREAS|CLEANING|FCRE|

    All information would be on one line but the carriage return was forced when typing into the area for this post.
    Basically, I want to keep only the first occurence of the information in colums 1 and 2. I then want to list the information in column 3 from left to right in the same row. I want to do this for each new number listed, ie: one row that looks like the example above for 2131100 another row for 2131200 etc...

    Attached below is a copy of the AWK script which does not seem to be working properly.

    Any insight on how to accomplish this would be greatly appreciated.

    awk -F"|" 'BEGIN {SS_A=""}
    {if (NR==1)
    {SS_A=$1; printf $0}
    else
    {if (SS_A==$1)
    {printf "|"$2}
    else
    {SS_A=$1;
    printf "\n"$0}
    }
    } END {printf "\n"}' $SF_OUT
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    52
    Rep Power
    12
    The following works for me:

    Code:
    #!/usr/bin/awk -f
    
    BEGIN {
      FS="|"
      line=""
    }
    
    $1 $2 == line {
      printf $3 FS
      next
    }
    
    {
      if (line != "")
        print ""
      printf $0
      line=$1 $2
    }
    
    END {
      print ""
    }
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    2
    Rep Power
    0

    reply to awk code


    thank you for the response. Any idea why the script suggested would be producing output that contains 2 occurences of the 2nd field?

    Any thoughts are welcome. Also, if an example of input and/or output would be helpful I can provide them.

    Thank you.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    52
    Rep Power
    12
    Please do provide an example of the input you used that caused a problem, as well as the output you saw. The output I get from the example input you posted originally is:
    2131100 COVS/100C E|FWP AREAS|MIDWEST|CONSTRUCTION|FTC|FWP AREAS|CLEANING|FCRE|
    2131200 COVS/100C UPS|UPS ROOMS|CONSTRUCTION|UPS ROOMS|FCRE|EMERGENCY|

IMN logo majestic logo threadwatch logo seochat tools logo