From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10ad19,1a7ef15e5c7743b8 X-Google-Attributes: gid10ad19,public X-Google-Thread: f678f,1a7ef15e5c7743b8 X-Google-Attributes: gidf678f,public From: Pete Ford Subject: Re: AWK on digital UNIX. Date: 1997/03/07 Message-ID: <331FF348.2CBBFAAF@computing-services.ox.ac.uk>#1/1 X-Deja-AN: 223753003 References: <331DC423.38DB@rpspo2.atlantaga.ncr.com> Organization: Oxford University Computing services Newsgroups: comp.unix.osf.misc,comp.lang.awk rchannav@rpspo2.atlantaga.ncr.com wrote: > > Hi All, > > Recently I had to write a script in awk on DIGITAL - UNIX 4.0. > I was surprised by this ambiguous output and see if it is the > way in DIGITAL UNIX. The code segment is as follows > > awk -F\, '{ > ....... > > gsub(/ /,//,$2) > print $0 > > ....... > }' < /tmp/tmp.txt > > the tmp.txt is like this > > hello,fo o,123 > foo,bar,123 > bar,ba r,123 etc., > > Basically I am stripping the blanks from second field. The output it > produces is in the following format > > hello,fo10,123 > foo,bar,123 > bar,ba1r,123 > > I know that gsub returns the no.of occurences, but why in $2?? > > Thanks > > -- > -------------------------------------------------------- > You might be a redneck if you think I speak for NCR > -------------------------------------------------------- >From the man page: Substitutes the string repl in place of the first instance of the extended regular expression ERE in string in and return the number of substitutions. That is, the second argument of 'sub' and 'gsub' is a *string*, not a regular expression. So your gsub line should read: gsub(/,/,"",$2) The '1' appears to be the result of a match of the second regular expression - in the case of // this always matches. But putting /_/ (for example) in as the second parameter puts a '0' in the spaces, and putting /a/ in puts a '0' in the first example line, and a 1 in the third line. Hmmm... -- Pete Ford Unix Sys. Admin. Oxford University Computing Services Phone: +44 1865 2~73266 Look mum, a real job! "I'm sorry but you need more help than I can give you over the phone"- R. Letts The above message represents my personal opinion; neither my employers, nor any other organisation, is responsible for statements made therein