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 6 of 16 Topic 2155 of 2241
Post > Topic >>

Re: Is that problem for awk?

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

On 2/8/2008 9:19 AM, Luuk wrote:
> Ed Morton schreef:
> 
>>On 2/8/2008 6:50 AM, Luuk wrote:
<snip>
>>>could someone explain the '1' in "$ awk 'gsub(/.*\(....|\)$/,"")1'
file" ?
>>
>>It makes sure that even if the input record is empty (in which case
gsub() will
>>return 0) the eventual condition being tested by awk is
non-zero/non-null so
>>that printing the current record occurs even in that case.
>>
>>The operator used to combine the result of the gsub() with the "1" is
>>string-concatenation so you can  put anything after the gsub() to get a
non-null
>>resultant string, even zero (to get the string "00") or the null string
(to get
>>the string "0" as opposed to the number zero).
>>
>>
>>>awk does not seem to do anything with it...
>>>or is it just a typo?
>>
>>No. Look:
>>
>>$ cat file
>>a
>>
>>c
>>$ awk 'sub(/./,NR)' file
>>1
>>3
>>$ awk 'sub(/./,NR)1' file
>>1
>>
>>3
>>
>>
>>>but awk also does not complain when i type:
>>>$ awk 'gsub(/.*\(....|\)$/,"")g' file
>>
>>Right. In that case it evaluates the unassigned variable "g" to the null
string
>>"" which is string-concatenated with the zero result of sub() to give a
non-null
>>"0" string:
>>
>>$ awk 'sub(/./,NR)g' file
>>1
>>
>>3
>>
>>Regards,
>>
>>	Ed.
>>
> 
> 
> 
> i must have skipped that part of the man-page....

I think it's in the fine-print at the bottom under "What???" :-).

> normally i use to do:
> awk '{ sub(/./,NR); print $0 }' file
> 
> which i indeed something longer... ;-)
> 

You could alternatively do:

awk 'BEGIN{
	while ((getline var < ARGV[1]) > 0) {
		sub(/./,++nr,var)
		print var
	}
	close(ARGV[1])
	exit
}' file

if you really need to fill up a file ;-). Just making the point for anyone
else
reading this that while more text can make the script appear to be "more
readable" to non-awk-experienced procedural programmers, it's better to
just get
used to and use the awk idioms than get stuck in a C-like paradigm and
miss all
the benefits of the awk paradigm.

	Ed.




 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:40:45 CDT 2008.