Prateek wrote:
> Hi,
> My question is having two parts:
>
> 1. I am having this script:
>
> BEGIN {FS="|"}
> {
> gsub(/.*/,"\"&\"",$6);
> ORS=",";
> for(i=1;i<NF;i++){print $i;}
> ORS="\n";
> #print "";
>
> }
>
> In the above script, I am double qouting my sixth field and then
> trying to print all the fields separated by comma. Each record should
> be printed on a new line.
Is that what you're looking for...?
awk 'BEGIN { FS="|"; OFS="," } { gsub(/.*/,"\"&\"",$6); print }'
or even...
awk 'BEGIN { FS="|"; OFS="," } { $6="\""$6"\""; print }'
Input:
1|2|3|4|5|6|7|8|9
A|B|C|D|E|F|G|H
Output:
1,2,3,4,5,"6",7,8,9
A,B,C,D,E,"F",G,H
Janis
>
> But when I do the above, my ORS="\n" does not take into effect as the
> entire file (all records) are printed comma separated and not on new
> line as desired.
>
> So I tried introducing print stmt which is the last commented stmt in
> above script. It does the work i.e the output is as :
>
> 10405349,01/03/08 21:29:03,01/09/08 02:01:21,Closed,AUTO
> EVENT,"CENTRAL TWS_JOB_ABEND JOB JMKTIS_3TPEMGR00M_ARCHIVE_CASES
> FAILED, STREAM SMKTIS_TPEMGR03G, NO RECOVERY SPECIFIED",P3,
> 10416266,01/06/08 21:13:00,01/10/08 08:03:46,Closed,AUTO
> EVENT,"CENTRAL TWS_JOB_ABEND JOB JMKTIS_3TPEMGR00M_ARCHIVE_CASES
> FAILED, STREAM SMKTIS_TPEMGR03G, NO RECOVERY SPECIFIED",P3,
>
> So as seen above, record is getting printed on a new line but the
> previous record is ending with a comma which is not desired after P3.
>
> So my question to you all is does OFS take effect only after one print
> stmt which effectively flushes the buffer. Is this a bug or am I
> missing something?
>
>
> 2. Also when I redirect the same to a file by
>
> BEGIN {FS="|"}
> {
> gsub(/.*/,"\"&\"",$6);
> ORS=",";
> for(i=1;i<NF;i++){print $i >> FILENAME"_formatted";}
> ORS="\n";
> print "";
> }
>
> the entire output is only comma separeted and no new line (which was
> atleast the case when directing to screen)
>
>
>
> Looking fwd to your replies.
>
> Thanks,
> Prateek


|