Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: ORS Flush p...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 3 Topic 2163 of 2241
Post > Topic >>

Re: ORS Flush problem??

by Janis Papanagnou <Janis_Papanagnou@[EMAIL PROTECTED] > Feb 13, 2008 at 12:25 AM

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




 3 Posts in Topic:
ORS Flush problem??
Prateek <prateek.a@[EM  2008-02-12 14:39:30 
Re: ORS Flush problem??
Janis Papanagnou <Jani  2008-02-13 00:25:55 
Re: ORS Flush problem??
Ed Morton <morton@[EMA  2008-02-12 18:37:51 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat May 17 7:49:23 CDT 2008.