Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, iŽd like to write something using awk, to replace any "." (dot) of an input file with a "," (colon). IŽd like to store the new file under the same name as the input file (overwrite). A real replacement :-) Sometimes there are more than one dot in each line. I did my first "experiments" using the "sub"-command, with poor results. Thanks for any help! regards ingo
Post Follow-up to this messageIn article <ed9a5d.0409151232.1b2b0838@posting.google.com>,
ifbehrens@gmx.de (ingo) wrote:
> Hi,
> iŽd like to write something using awk, to replace any "." (dot) of
> an input file with a "," (colon). IŽd like to store the new file under
> the same name as the input file (overwrite). A real replacement :-)
> Sometimes there are more than one dot in each line.
>
> I did my first "experiments" using the "sub"-command, with poor
> results.
>
> Thanks for any help!
>
> regards ingo
You will have to create a temporary file, and then either rename it or
copy the resulting temp file over the original and then delete the temp
file. Your choice. If you want to do a true replacement and no
temporary file, then consider using an editor of some kind (ed ?).
awk '{gsub(/\./,",",$0); print}' inputfile >outputfile
mv outputfile inputfile
I used a comma and not a colon as your example showed a comma. If you
really meant a colon, then
awk '{gsub(/\./,":",$0); print}' inputfile >outputfile
mv outputfile inputfile
Bob Harris
Post Follow-up to this messageingo wrote:
> Hi,
> iŽd like to write something using awk, to replace any "." (dot) of
> an input file with a "," (colon). IŽd like to store the new file under
> the same name as the input file (overwrite). A real replacement :-)
> Sometimes there are more than one dot in each line.
>
> I did my first "experiments" using the "sub"-command, with poor
> results.
Hi,
awk is a stream editor, so you can't edit files in place. You have to do:
awk '{gsub(/\./,";")} 1' file1 > tmp
mv tmp file1
**** Now a bit OT for an AWK group ****
A very simple substitution like this is slightly more easily done (and
probably quicker) in sed:
sed -e's/\./;/g' file1 > tmp
mv tmp > file1
For in place editing one can use another member of the family, either ed
or ex. (or, GNU sed has added an option -i for "in place" which takes
the streamified version of ED (SED) and turns it in to a non stream
editor. hmmm). Anyway, here's the version using an in-place editor:
echo "%s/\./;/g
wq" | ed -s foo
of, if you have BASH as a shell, you could write:
ed -s foo << E
%s/\./;/g
wq
E
-Ed
--
(You can't go wrong with psycho-rats.) (er258)(@)(eng.cam)(.ac.uk)
/d{def}def/f{/Times findfont s scalefont setfont}d/s{10}d/r{roll}d f 5/m
{moveto}d -1 r 230 350 m 0 1 179{1 index show 88 rotate 4 mul 0 rmoveto}
for /s 15 d f pop 240 420 m 0 1 3 { 4 2 1 r sub -1 r show } for showpage
Post Follow-up to this messageE. Rosten wrote: > **** Now a bit OT for an AWK group **** > > A very simple substitution like this is slightly more easily done (and > probably quicker) in sed: Even simpler is the POSIX-compliant Unix command tr, which was especially created for such things: http://www.opengroup.org/onlinepubs...799/xcu/tr.html
Post Follow-up to this message> > Even simpler is the POSIX-compliant Unix command > tr, which was especially created for such things: > > http://www.opengroup.org/onlinepubs...799/xcu/tr.html Good point. It's probably rather faster as well. Timing on a million line text file of random ASCII, here are the running times: $ time tr '.' ';' < foo.text > /dev/null real 0m1.547s user 0m1.300s sys 0m0.240s $ time sed -e'y/./;/' < foo.text > /dev/null real 0m1.785s user 0m1.690s sys 0m0.100s $ time sed-3.02 -e's/\./;/g' < foo.text > /dev/null real 0m14.522s user 0m14.400s sys 0m0.070s $ time gawk-3.1.0 '{gsub(/\./,";")} 1' < foo.text > /dev/null real 0m7.388s user 0m7.100s sys 0m0.260s $ time mawk '{gsub(/\./,";")} 1' < foo.text > /dev/null real 0m3.271s user 0m2.880s sys 0m0.380s For completeness: $time cat < foo.text > /dev/null real 0m0.217s user 0m0.000s sys 0m0.200s Interesting. tr is king of the hill (as expected), with sed using y only slightly slower. Suprisingly, for regex based search and replace, sed is much slower than awk. -Ed -- (You can't go wrong with psycho-rats.) (er258)(@)(eng.cam)(.ac.uk) /d{def}def/f{/Times findfont s scalefont setfont}d/s{10}d/r{roll}d f 5/m {moveto}d -1 r 230 350 m 0 1 179{1 index show 88 rotate 4 mul 0 rmoveto} for /s 15 d f pop 240 420 m 0 1 3 { 4 2 1 r sub -1 r show } for showpage
Post Follow-up to this message"E. Rosten" <look@my.sig> wrote in message news:<41496F67.7010904@my.sig>...
> ingo wrote:
>
> Hi,
>
> awk is a stream editor, so you can't edit files in place. You have to do:
>
> awk '{gsub(/\./,";")} 1' file1 > tmp
> mv tmp file1
>
>
> **** Now a bit OT for an AWK group ****
>
> A very simple substitution like this is slightly more easily done (and
> probably quicker) in sed:
>
> sed -e's/\./;/g' file1 > tmp
> mv tmp > file1
>
> For in place editing one can use another member of the family, either ed
> or ex. (or, GNU sed has added an option -i for "in place" which takes
> the streamified version of ED (SED) and turns it in to a non stream
> editor. hmmm). Anyway, here's the version using an in-place editor:
>
> echo "%s/\./;/g
> wq" | ed -s foo
>
>
> of, if you have BASH as a shell, you could write:
>
> ed -s foo << E
> %s/\./;/g
> wq
> E
>
>
> -Ed
thanks to all!
ingo
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.