Help: Field Insertion Problem 
Author Message
 Help: Field Insertion Problem

Hi there,

        I'm very new with AWK.  
        I have this file "test" which is shown below:

              200
AAA    BBB    CCC    DDD    EEE
  1     as    313     43    gsg
  2     fe    315     95    ert
  3     lg    327     12    rjw
  4     re    895     31    wrg

              201
AAA    BBB    CCC    DDD    EEE
  5     qw    312     41    gts
  6     fy    765     68    axz
  7     io    877     12    ppp
  8     po    853     42    pwk

How do I make it, like the one shown below, by using awk.

     AAA    BBB    CCC    DDD    EEE
200    1     as    313     43    gsg
200    2     fe    315     95    ert
200    3     lg    327     12    rjw
200    4     re    895     31    wrg

     AAA    BBB    CCC    DDD    EEE
201    5     qw    312     41    gts
201    6     fy    765     68    axz
201    7     io    877     12    ppp
201    8     po    853     42    pwk

        You can send me your response through my email address or you can
respond through this Newsgroup.
        Thank you very much!

--
 Just me :-)



Fri, 30 Mar 2001 03:00:00 GMT  
 Help: Field Insertion Problem
Hi Albert

This should do it

awk '
NF==1 {gsub(/ /,"");f1=$0}
NF>1 {if($0 ~ /^AAA/)
       {print "    " $0}
      else
       {print f1 " " $0}

Quote:
}

NF==0 {print}
' test

Jason

Quote:

> Hi there,

>         I'm very new with AWK.
>         I have this file "test" which is shown below:

>               200
> AAA    BBB    CCC    DDD    EEE
>   1     as    313     43    gsg
>   2     fe    315     95    ert
>   3     lg    327     12    rjw
>   4     re    895     31    wrg

>               201
> AAA    BBB    CCC    DDD    EEE
>   5     qw    312     41    gts
>   6     fy    765     68    axz
>   7     io    877     12    ppp
>   8     po    853     42    pwk

> How do I make it, like the one shown below, by using awk.

>      AAA    BBB    CCC    DDD    EEE
> 200    1     as    313     43    gsg
> 200    2     fe    315     95    ert
> 200    3     lg    327     12    rjw
> 200    4     re    895     31    wrg

>      AAA    BBB    CCC    DDD    EEE
> 201    5     qw    312     41    gts
> 201    6     fy    765     68    axz
> 201    7     io    877     12    ppp
> 201    8     po    853     42    pwk

>         You can send me your response through my email address or you can
> respond through this Newsgroup.
>         Thank you very much!

> --
>  Just me :-)



Fri, 30 Mar 2001 03:00:00 GMT  
 Help: Field Insertion Problem

Quote:

>Hi there,

> I'm very new with AWK.
> I have this file "test" which is shown below:

>              200
>AAA    BBB    CCC    DDD    EEE
>  1     as    313     43    gsg
>  2     fe    315     95    ert
>  3     lg    327     12    rjw
>  4     re    895     31    wrg

>              201
>AAA    BBB    CCC    DDD    EEE
>  5     qw    312     41    gts
>  6     fy    765     68    axz
>  7     io    877     12    ppp
>  8     po    853     42    pwk

>How do I make it, like the one shown below, by using awk.

>     AAA    BBB    CCC    DDD    EEE
>200    1     as    313     43    gsg
>200    2     fe    315     95    ert
>200    3     lg    327     12    rjw
>200    4     re    895     31    wrg

>     AAA    BBB    CCC    DDD    EEE
>201    5     qw    312     41    gts
>201    6     fy    765     68    axz
>201    7     io    877     12    ppp
>201    8     po    853     42    pwk

> You can send me your response through my email address or you can
>respond through this Newsgroup.
> Thank you very much!

>--
> Just me :-)

Program below will work. NF gives the number of awk-parsed fields in the
record, which in this case is a line of text. The  next  statement in the
second line of the program is needed to bypass the fourth line. I assume you
want to reproduce blank lines (lines with whitespace only), which is the
rationale for the program's first line.

NF == 0 { print }
NF == 1 { x = $1; next }
/^ *[A-Z]/ { print "\t", $0 }
/^ *[0-9]/ { print x, $0 }



Fri, 30 Mar 2001 03:00:00 GMT  
 Help: Field Insertion Problem

Quote:


>>Hi there,

>> I'm very new with AWK.
>> I have this file "test" which is shown below:

>>              200
>>AAA    BBB    CCC    DDD    EEE
>>  1     as    313     43    gsg
>>  2     fe    315     95    ert
>>  3     lg    327     12    rjw
>>  4     re    895     31    wrg

>>              201
>>AAA    BBB    CCC    DDD    EEE
>>  5     qw    312     41    gts
>>  6     fy    765     68    axz
>>  7     io    877     12    ppp
>>  8     po    853     42    pwk

>>How do I make it, like the one shown below, by using awk.

>>     AAA    BBB    CCC    DDD    EEE
>>200    1     as    313     43    gsg
>>200    2     fe    315     95    ert
>>200    3     lg    327     12    rjw
>>200    4     re    895     31    wrg

>>     AAA    BBB    CCC    DDD    EEE
>>201    5     qw    312     41    gts
>>201    6     fy    765     68    axz
>>201    7     io    877     12    ppp
>>201    8     po    853     42    pwk

>> You can send me your response through my email address or you can
>>respond through this Newsgroup.
>> Thank you very much!

>>--
>> Just me :-)

>Program below will work. NF gives the number of awk-parsed fields in the
>record, which in this case is a line of text. The  next  statement in the
>second line of the program is needed to bypass the fourth line. I assume you
>want to reproduce blank lines (lines with whitespace only), which is the
>rationale for the program's first line.

>NF == 0 { print }
>NF == 1 { x = $1; next }
>/^ *[A-Z]/ { print "\t", $0 }
>/^ *[0-9]/ { print x, $0 }

I think this might be a better/shorter/simpler solution/approach:

gawk 'NF == 1 { x = $1; print "" }
      NF >  1 {printf "%-5s%s\n", $1=="AAA" ? "" : x , $0}' infile

This skips blank lines, and prints a blank line for those lines
with one field, and inserts a different leading field (as appropriate)
when printing other lines.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Fri, 30 Mar 2001 03:00:00 GMT  
 Help: Field Insertion Problem
              200
AAA    BBB    CCC    DDD    EEE
1     as    313     43    gsg
2     fe    315     95    ert
3     lg    327     12    rjw
4     re    895     31    wrg

Quote:
>               201
> AAA    BBB    CCC    DDD    EEE
>   5     qw    312     41    gts
>   6     fy    765     68    axz
>   7     io    877     12    ppp
>   8     po    853     42    pwk

> How do I make it, like the one shown below, by using awk.

>      AAA    BBB    CCC    DDD    EEE
> 200    1     as    313     43    gsg
> 200    2     fe    315     95    ert
> 200    3     lg    327     12    rjw
> 200    4     re    895     31    wrg

>      AAA    BBB    CCC    DDD    EEE
> 201    5     qw    312     41    gts
> 201    6     fy    765     68    axz
> 201    7     io    877     12    ppp
> 201    8     po    853     42    pwk

 PROGRAM IN PRINCIPLE
================================================================================

awk 'NF==1 { header=$1 ; indicator =1 ;next}
indicator==1 {print "      " $0; indicator=0; next }
{print header ,$0 }' infile > outfile

if you want really nice tabular output, then replace print by printf
e.g. in the last line use something like
printf  "%s    %d    %s   %d   %d   %s", header, $1,$2,$3,$4,$5

some of the options for % allow left/right fill  ...

not tested
LMS
free sed/awk book:
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.ps.gz
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.tar.gz



Mon, 02 Apr 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Straight Insertion Sort and Binary Insertion Sort

2. C5 & SQL7.0 Record insertion problems

3. Help with direction of insertion cursor

4. Problem with hot field display of a memo field

5. help ! problem with date fields entry format

6. Problem with Past to Text field.....Please help!!!!!

7. Help! Clipper S87 Memo Field Corruption Problem

8. Please help with Field Focus problem

9. Help - problem with memo fields

10. HELp/HELP trouble with adding fields

11. Printing large text fields in cw1.5 help help please

12. Pattern match and string insertion between two files

 

 
Powered by phpBB® Forum Software