On 2/12/2008 4:39 PM, Prateek wrote:
> Hi,
> My question is having two parts:
>
> 1. I am having this script:
>
> BEGIN {FS="|"}
> {
> gsub(/.*/,"\"&\"",$6);
> ORS=",";
I think you probably meant to set OFS above, not ORS.
> for(i=1;i<NF;i++){print $i;}
> ORS="\n";
Since you don't print anything after doing this, it will have no effect on
the
current record.
> #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.
Then don't mess with the ORS as that what it is by default.
> 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.
Right, because you set ORS to a comma immediately before printing.
> 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?
No, No, and Yes. Also, you didn't set OFS at all.
>
> 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";}
Are you SURE you want to use ">>"? You may want to check your awk manual
if you
think ">>" means the same in awk as it does in shell.
> ORS="\n";
> print "";
> }
>
> the entire output is only comma separeted and no new line (which was
> atleast the case when directing to screen)
Redirecting to the file is no different from printing to the screen. You
would
have seen newlines in your file as a result of the print "" after
ORS="\n".
Also - get rid of all those redundant semi-colons as they're just
cluttering up
your script.
>
>
> Looking fwd to your replies.
See Janis's response for the right way to do it.
Ed.


|