Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2003
    Posts
    71

    Unanswered: file manupulation

    i believe its very easy to do in awk .need some help

    here is my requirement

    #input data ( prepared from cut command from different source file)
    440 708 BAINBRIDGE TWP PD
    440 708 CHARDON PD
    440 708 CHESTER TWP PD
    440 708 GEAUGA
    440 708 MIDDLEFIELD PD
    ==================
    #input data format
    ==================
    col1=3 char
    col2=3 char
    col3=32 char with space padding ,mulitple words allowed separated by space
    ==================
    #output data format
    ==================
    col1=3 char
    col2=3 char
    col3=comma separated list with trimming space in 3rd col of input file
    ==================
    #desired output
    ==================
    440 708 BAINBRIDGE_TWP_PD,CHARDON_PD,CHESTER_TWP_PD,GEAUGA ,MIDDLEFIELD_PD

  2. #2
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Hi SKD,
    your requirement is not very difficult.

    - but -

    I think, when col1 or col2 change the values, then you want to have a new row with your desired output.

    can it be ???
    Greetings from germany
    Peter F.

  3. #3
    Join Date
    Sep 2003
    Posts
    71
    yes, you got that right.

  4. #4
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Talking

    Try this:

    Code:
    #!/bin/ksh
    nawk 'BEGIN {key=substr($0,1,8); lin=""; s=","}
    {key2=substr($0,1,8); txt=substr($0,9); gsub(" ","_",txt);
     if (key == key2)  {lin = lin s txt;}
     else {gsub(" ","",lin); print key lin; key=key2; lin=txt;};
    } END {gsub(" ","",lin); print key lin;}' MyFile.txt >NewFile.txt
    Last edited by LKBrwn_DBA; 07-07-04 at 15:34.
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  5. #5
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    or:

    Code:
    nawk '
    {
      idx= $1 " " $2;
      gsub("^" $1 " " $2 " ", "");
      gsub(/ /, "_")
      a[idx] =  ( idx in a ) ? a[idx] "," $0 : $0;
    }
    
    END {
       for (i in a)
         printf("%s %s\n", i, a[i]);
    }' file

  6. #6
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Quote Originally Posted by LKBrwn_DBA
    Try this:

    Code:
    #!/bin/ksh
    nawk 'BEGIN {key=substr($0,1,8); lin=""; s=","}
    {key2=substr($0,1,8); txt=substr($0,9); gsub(" ","_",txt);
     if (key == key2)  {lin = lin s txt;}
     else {gsub(" ","",lin); print key lin; key=key2; lin=txt;};
    } END {gsub(" ","",lin); print key lin;}' MyFile.txt >NewFile.txt
    Hi LKBrwn_DBA
    I am brim over with enthusiasm for your solution.
    I test it in SCO Unix and it works fine. I can use this in another problem I have. Thanks a lot

    @ skd
    I hope col1 and col2 are sorted. Otherwise it does not work.
    Last edited by fla5do; 07-07-04 at 16:28.
    Greetings from germany
    Peter F.

  7. #7
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Lightbulb

    Quote Originally Posted by fla5do
    @ skd
    I hope col1 and col2 are sorted. Otherwise it does not work.
    True, but you could do this:
    Code:
    sort MyFIle.txt |\
    nawk 'BEGIN {key=substr($0,1,8); lin=""; s=","}
    {key2=substr($0,1,8); txt=substr($0,9); gsub(" ","_",txt);
     if (key == key2)  {lin = lin s txt;}
     else {gsub(" ","",lin); print key lin; key=key2; lin=txt;};
    } END {gsub(" ","",lin); print key lin;}' >NewFile.txt
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  8. #8
    Join Date
    Sep 2003
    Posts
    71
    First of all , many thanks to three of you for your enthusiam in your solution.

    you guys seems a guru in awk.

    Thanks a lot.

    how do i trim _ at end of each word.
    further desired output is
    ===
    216 440 BAINBRIDGE_TWP_PD,CHARDON_RD,CHESTER_TWP_PD,GEAUGA _COUNTY
    note :- in input , 3rd field is 32 char left justified text with blanks padding
    ==
    instead of
    216 440 BAINBRIDGE_TWP_PD__________,CHARDON_PD____________ __________,CHESTER_TWP_PD_____________,GEAUGA_COUN TY___________________


    thanks again.

  9. #9
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Quote Originally Posted by skd
    First of all , many thanks to three of you for your enthusiam in your solution.

    you guys seems a guru in awk.

    Thanks a lot.

    how do i trim _ at end of each word.
    further desired output is
    ===
    216 440 BAINBRIDGE_TWP_PD,CHARDON_RD,CHESTER_TWP_PD,GEAUGA _COUNTY
    note :- in input , 3rd field is 32 char left justified text with blanks padding
    ==
    instead of
    216 440 BAINBRIDGE_TWP_PD__________,CHARDON_PD____________ __________,CHESTER_TWP_PD_____________,GEAUGA_COUN TY___________________


    thanks again.
    what's the INDIVIDUAL width of fields separated by commas?
    Meaning:
    BAINBRIDGE_TWP_PD__________,
    what IS the total width of this field?
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  10. #10
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    for example to have 20 char wide character fields left justified and padded with spaces:

    Code:
    {
      idx= $1 " " $2;
      gsub("^" $1 " " $2 " ", "");
      gsub(/ $/, ""); gsub(/ /, "_");
      $0=sprintf("%-20.20s", $0);
      a[idx] =  ( idx in a ) ? a[idx] "," $0 : $0;
    }
    
    END {
       for (i in a)
         printf("%s %s\n", i, a[i]);
    }
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  11. #11
    Join Date
    Sep 2003
    Posts
    71
    indivisual field width for each word separated by comma is variable in output.

    this is how i am getting input.

    cat aciw.oh|\
    awk ' { print substr($0,2,3) " " substr($0,5,3) " " substr($0,94,32) } '|\
    grep -v "^[HT]L"|sort -u > input.txt

    cat input.txt
    =========
    216 440 BAINBRIDGE TOWNSHIP PD
    216 440 CHARDON PD
    216 440 CHESTER TOWNSHIP PD
    216 440 GEAUGA COUNTY

    my desire output
    ==============
    216 440 BAINBRIDGE_TOWNSHIP_PD,CHARDON_PD,CHESTER_TOWNSHIP _PD,GEAUGA_COUNTY

  12. #12
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Code:
    {
      idx= $1 " " $2;
      gsub("^" $1 " " $2 " ", "");
      gsub(/ $/, ""); gsub(/ /, "_");
      a[idx] =  ( idx in a ) ? a[idx] "," $0 : $0;
    }
    
    END {
       for (i in a)
         printf("%s %s\n", i, a[i]);
    }
    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
  •