Talk About Network

Google


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: OFS use
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 3 Topic 2130 of 2371
Post > Topic >>

Re: OFS use

by Janis Papanagnou <Janis_Papanagnou@[EMAIL PROTECTED] > Jan 15, 2008 at 12:52 AM

Prateek wrote:
> Hi,
> I have a record:
> 
> 10276386,12/08/07 00:13:27,12/13/07 00:07:58,Closed,AUTO
> EVENT,"CENTRAL TWS_JOB_ABEND JOB JMKTIS_3TPEMGR00M_LOAD2_FA_FILE
> FAILED, STREAM SMKTIS_TPEMGR01G, NO RECOVERY
> SPECIFIED",P3,F1BCSPSMKTORI
> 
> the FS for above is "|"
> 
> If i want to output this with "|" replaced by "," along with $6 being
> quoted, I use the following:
> gawk -F\| -v OFS=',' '{gsub(/.*/,"&",$6); print $0>"a"}'

The gsub() is a no-op WRT $6; you could as well have written $6=$6 .
I thought that you wanted quotes; then you either substitute them
by  gsub(/.*/,"\"&\"",$6)  or without function call by  $6 = Q $6 Q
(where you've defined  Q = "\"" ), or slightly less readable as
$6 = "\"" $6 "\"" .

(BTW, if the above is the whole program I'd let the output redirection
be done by the shell[*]...

   gawk -F\| -v OFS=',' '$6 = "\"" $6 "\""' > a

[*] Which is equivalent but in case of an empty input file.)

> F1BCSRTECRD.txta
> 
> The above works fine as expected.
> 
> 
> Now if I just try this:
> gawk -F\| -v OFS=',' '{print $0>"a"}' F1BCSRTECRD.txta
> I am not getting an output whose fields are seperated by "," but they
> are instead seperated by existing "|". Can someone please let me know
> why this happens.

If you just reference the fields for reading they are not reformatted
for output. To have the output reformatted you need to change any field;
your sub() code does this (as the assignments would do which I suggested
above).

Janis

> 
> Thanks,
> Prateek
 




 3 Posts in Topic:
OFS use
Prateek <prateek.a@[EM  2008-01-14 15:17:29 
Re: OFS use
Ed Morton <morton@[EMA  2008-01-14 17:40:02 
Re: OFS use
Janis Papanagnou <Jani  2008-01-15 00:52:43 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Tue Oct 7 12:39:41 CDT 2008.