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: A better wa...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 7 Topic 2096 of 2341
Post > Topic >>

Re: A better way to convert spaces to tabs while joining 2 lines

by Bruce Gilmore <bruce84@[EMAIL PROTECTED] > Dec 7, 2007 at 01:37 PM

Ed Morton wrote:
> 
> On 12/7/2007 1:18 PM, Bruce Gilmore wrote:
>> Ed Morton wrote:
>>
>>> On 12/7/2007 11:24 AM, Ed Morton wrote:
>>>
>>>> On 12/7/2007 11:11 AM, Janis Papanagnou wrote:
>>>>
>>>>
>>>>> Bruce Gilmore wrote:
>>>>>
>>>>>
>>>>>
>>>>>> Greetings,
>>>>>> Here's my sample data
>>>>>> The "\n" represent new lines and aren't literally in the sample.
They're 
>>>>>> there just to show where the line breaks are.
>>>>>>
>>>>>>
>>>>>>       80688351 10/28/04   07/06/05               .00        94.75 R

>>>>>>   S      3         94.75  07/06/05\n
>>>>>>                07/06/05     S\n
>>>>>>       60162450 02/09/05   07/07/05               .00      5592.48 G

>>>>>>    H      1       5592.48  07/07/05\n
>>>>>>                07/07/05    H\n
>>>>>>       30434369 07/07/04   07/08/05               .00        78.88 E

>>>>>>    H      3         78.88  07/08/05\n
>>>>>>                07/08/05    H\n
>>>>>>       60064052 08/27/03   07/08/05               .00        92.41 G

>>>>>>    H      3         92.41  07/08/05\n
>>>>>>                02/29/04     H\n
>>>>>>       30213466 07/31/03   07/11/05               .00       962.23 E

>>>>>>    S      5        962.23  08/09/07\n
>>>>>>                07/11/05       1\n
>>>>>>
>>>>>>
>>>>>> The file is space delimited and every two lines need to be joined 
>>>>>> together to form a single line.  The output should be tab
delimited.
>>>>>> Here's what I came up with and it appears to work with the
exception of 
>>>>>> an extra tab at the end of every output line.  My question is, how
can 
>>>>>> this script be made better (i.e. more succinct or efficient)?
>>>>> One possibility...
>>>>>
>>>>>  BEGIN {OFS="\t"} {$1=$1; printf("%s%s", $0, NR%2 ? OFS : RS)}
>>>> Or:
>>>> 	
>>>>     BEGIN{OFS="\t"}{ORS=NR%2?OFS:RS)}$1=$1
>>> Spurious ")". Corrected:
>>>
>>>   BEGIN{OFS="\t"}{ORS=NR%2?OFS:RS}$1=$1
>>>
>>
>> These are great!  Thanks.
>> I can tell the $1=$1 is converting the spaces to tabs but I'm not sure
how.
>> Could I ask either of you to explain please?
>>
> 
> Assigning anything to one of the fields causes the current record ($0)
to be
> recompiled using the value of OFS as the field separator, so if all you
want to
> do is change the field separation, just pick a field and assign it to
itself.
> 
> 	Ed.
> 
That's good to know.  Thanks.
BG
 




 7 Posts in Topic:
A better way to convert spaces to tabs while joining 2 lines
Bruce Gilmore <bruce84  2007-12-07 10:58:52 
Re: A better way to convert spaces to tabs while joining 2 lines
Janis Papanagnou <Jani  2007-12-07 18:11:27 
Re: A better way to convert spaces to tabs while joining 2 lines
Ed Morton <morton@[EMA  2007-12-07 11:24:33 
Re: A better way to convert spaces to tabs while joining 2 lines
Ed Morton <morton@[EMA  2007-12-07 11:32:50 
Re: A better way to convert spaces to tabs while joining 2 lines
Bruce Gilmore <bruce84  2007-12-07 13:18:01 
Re: A better way to convert spaces to tabs while joining 2 lines
Ed Morton <morton@[EMA  2007-12-07 13:21:41 
Re: A better way to convert spaces to tabs while joining 2 lines
Bruce Gilmore <bruce84  2007-12-07 13:37:50 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Wed Aug 27 17:48:02 CDT 2008.