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: Is that pro...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 9 of 16 Topic 2155 of 2241
Post > Topic >>

Re: Is that problem for awk?

by Ed Morton <morton@[EMAIL PROTECTED] > Feb 8, 2008 at 11:29 AM

On 2/8/2008 11:11 AM, Janis Papanagnou wrote:
> Luuk wrote:
<snip>
>>normally i use to do:
>>awk '{ sub(/./,NR); print $0 }' file
>>
>>which i indeed something longer... ;-)
>>
> 
> 
> You can shorten that a bit without sacrificing the action block by
> 
>    awk '{ sub(/./,NR) } 1' file
> 
> Personally I consider the concatenation of sub() and 1
> 
>    awk 'sub(/./,NR) 1' file
> 
> as a hack; it's an unnecessary level of obfuscation[*].

I'm all about conciseness. Within that, normally, I'd trade clarity over
brevity
if there is a trade-off as in this case BUT in this particular case, I
really
like favoring brevity since it's a very small script so there isn't a lot
of
clutter with other details and to understand what awk is doing in that
script,
you have to understand how awk works in terms of condition-action
segments,
string concatenation, default actions, and return codes so to understand
what
the above is doing requires you to know a lot that you SHOULD know anyway
before
doing any awk programming.

So, I like the above because if you understand the awk paradigm already,
the
script is perfectly clear and if you don't then the small amount of effort
required to learn how that small script works takes you a long way to that
understanding.

> A bit more
> verbose but IMO conceptually clearer (no implicit casts) might be
> 
>    awk 'sub(/./,NR) || 1' file

That seems confusing to me as I can't see any benefit to it over any other
approach so I think people would waste their time trying to figure out
what the
benefit is. If you're going to do that, IMHO you'd be better off with:

     awk '{sub(/./,NR);print}' file
or
     awk '{sub(/./,NR)}1' file

as they're both clearer and about the same length.

Regards,

	Ed

> OTOH, the case where you just want those lines printed where you
> actually substituted something, the expression
> 
>    awk 'sub(/./,NR)' file
> 
> seems more natural compared to introducing a block (and maybe an
> unnecessary if statement).
> 
> Janis
> 
> [*] Mixing integral expressions and "invisible" operators, having
> implicit type conversions, and just for a boolean condition result.




 16 Posts in Topic:
Is that problem for awk?
Kurda Yon <kurdayon@[E  2008-02-07 12:37:10 
Re: Is that problem for awk?
Ed Morton <morton@[EMA  2008-02-07 14:45:08 
Re: Is that problem for awk?
"Luuk" <luuk  2008-02-08 13:50:57 
Re: Is that problem for awk?
Ed Morton <morton@[EMA  2008-02-08 08:58:17 
Re: Is that problem for awk?
Luuk <Luuk@[EMAIL PROT  2008-02-08 16:19:40 
Re: Is that problem for awk?
Ed Morton <morton@[EMA  2008-02-08 10:19:07 
Re: Is that problem for awk?
gazelle@[EMAIL PROTECTED]  2008-02-08 16:48:13 
Re: Is that problem for awk?
Janis Papanagnou <Jani  2008-02-08 18:11:32 
Re: Is that problem for awk?
Ed Morton <morton@[EMA  2008-02-08 11:29:42 
Re: Is that problem for awk?
Janis Papanagnou <Jani  2008-02-08 19:11:04 
Re: Is that problem for awk?
"Luuk" <luuk  2008-02-08 10:05:11 
Re: Is that problem for awk?
William James <w_a_x_m  2008-02-09 01:00:53 
Re: Is that problem for awk?
Luuk <Luuk@[EMAIL PROT  2008-02-09 10:34:28 
Re: Is that problem for awk?
gazelle@[EMAIL PROTECTED]  2008-02-09 13:09:48 
Re: Is that problem for awk?
Ed Morton <morton@[EMA  2008-02-09 09:49:35 
Re: Is that problem for awk?
Andrew Schorr <aschorr  2008-02-09 10:55:04 

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 4:20:29 CDT 2008.