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


|