Appending to $0 or $_ 
Author Message
 Appending to $0 or $_

{
    print

Quote:
}

This code will print the current record to stdout. I would like to
append " | " at the
end of the record. In the past, we debated about the use of $0 or $_ for
the current
record. I could use

    print $0 " | "

to get the desired record output. Which of the system variables
represents the
current record? This is for Awk on Linux systems.
--
Subba Rao



Tue, 05 Oct 2004 00:16:35 GMT  
 Appending to $0 or $_


Quote:

>{
>    print
>}

>This code will print the current record to stdout. I would like to
>append " | " at the end of the record. In the past, we debated about
>the use of $0 or $_ for the current record.

We didn't "debate" anything.  $_ is clearly wrong and is the result of
confusing two related but different scriping languages.  That $_ seems to
work under certain circumstances and with certain versions of AWK is just
an interesting coincidence.

Quote:
>I could use

>    print $0 " | "

>to get the desired record output. Which of the system variables
>represents the current record? This is for Awk on Linux systems.

The above is fine, although I would usually write: print $0,"| "
because it saves a string concat, and string concats are normally
expensive.


Tue, 05 Oct 2004 00:07:40 GMT  
 Appending to $0 or $_

Quote:


...
>>I could use

>>    print $0 " | "

>>to get the desired record output. Which of the system variables
>>represents the current record? This is for Awk on Linux systems.

>The above is fine, although I would usually write: print $0,"| "
>because it saves a string concat, and string concats are normally
>expensive.

Behind the scenes there's either a string concatenation in print's
implementation's comma argument processing or a bunch of [C] printf calls in
order to put OFS between $0 and "| ". If you're going to assume OFS == " ",
you should state your assumption explicitly.

To the OP: if this is all you want to do, use

BEGIN { ORS = " | \n" } { print }

to save the unnecessary waste of cycles pushing $0 onto print's argument
stack.



Tue, 05 Oct 2004 01:01:39 GMT  
 Appending to $0 or $_

<snip>

Quote:
> Which of the system variables
> represents the
> current record? This is for Awk on Linux systems.

man awk or man gawk, but don't expect to find $_ in there.

HTH
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"
peter{dot}tillier<at>btinternet[dot]com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Tue, 05 Oct 2004 05:34:26 GMT  
 Appending to $0 or $_

Quote:



>...
>>>I could use

>>>    print $0 " | "

>>>to get the desired record output. Which of the system variables
>>>represents the current record? This is for Awk on Linux systems.

>>The above is fine, although I would usually write: print $0,"| "
>>because it saves a string concat, and string concats are normally
>>expensive.

This is generally valid.  This doesn't mean that you should avoid
string concatenation, but something like

        print a " + " b " + " c " = " d

is more expensive than

        printf "%s + %s + %s = %s\n", a, b, c, d

This may be an issue when running gigabytes of data through awk. Most
of the time the best thing to do is write your program in the clearest
way possible and not worry too much about performance.

Quote:
>Behind the scenes there's either a string concatenation in print's
>implementation's comma argument processing

Not necessarily.  gawk loops over each comma separated string, writing
the string and then OFS.  No concatenation necessary.  gawk happens to
use fwrite for buffered output, but it could just as easily use write(2)
for direct output.  Doing that is probably less efficient.

Quote:
>or a bunch of [C] printf calls in order to put OFS between $0 and "| ".

Also no printf necessary.

I don't know what mawk and the bell labs awk do, but I'd be surprised if
either one did a string concatenation of the print arguments just to do
one write.

Quote:
>To the OP: if this is all you want to do, use

>BEGIN { ORS = " | \n" } { print }

>to save the unnecessary waste of cycles pushing $0 onto print's argument
>stack.

This is good advice. :-)

Arnold
--

P.O. Box 354            Home Phone: +972  8 979-0381    Fax: +1 928 569 9018
Nof Ayalon              Cell Phone: +972 51  297-545
D.N. Shimshon 99785     ISRAEL



Tue, 05 Oct 2004 16:40:29 GMT  
 Appending to $0 or $_

...

Quote:
>>>because it saves a string concat, and string concats are normally
>>>expensive.

>This is generally valid.  This doesn't mean that you should avoid
>string concatenation, but something like

>    print a " + " b " + " c " = " d

>is more expensive than

>    printf "%s + %s + %s = %s\n", a, b, c, d

>This may be an issue when running gigabytes of data through awk. Most
>of the time the best thing to do is write your program in the clearest
>way possible and not worry too much about performance.

Agreed. I was mostly joking - and tweaking Harlan who often goes pretty
non-linear over trivial performance issues like this.  I really just like
the "," notation (using OFS), as a way of getting reasonably good looking
output without a lot of effort.

Quote:
>Not necessarily.  gawk loops over each comma separated string, writing
>the string and then OFS.  No concatenation necessary.  gawk happens to
>use fwrite for buffered output, but it could just as easily use write(2)
>for direct output.  Doing that is probably less efficient.

Yeah - that was what I was thinking - that it just chunked through the list
and output each in turn, separated by OFS, without doing any AWK-level
string mashing.  Of course it all eventually turns into string
concatenation at the device driver level...


Tue, 05 Oct 2004 23:12:30 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. variable as search-pattern: if ($0 ~ myVar)

2. executable awk scripts and shell var $(basename $0)

3. Newbie Help required: Deleting double entries in a line ($0)

4. !x[$0]++

5. mechanism of application ($0 or the line)

6. $0 == false ??

7. program name -> perl's $0

8. $0 == __FILE__ idiom for Unix only?

9. ruby 1.6.5, irb $0

10. __FILE__ == $0 refactored in Ruby?

11. Replacement for __FILE__ == $0

12. Maximum size of $0

 

 
Powered by phpBB® Forum Software