Home > Archive > AWK > September 2004 > awk newbie: how replacing dots with colons?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
awk newbie: how replacing dots with colons?
|
|
|
| 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
| |
| Bob Harris 2004-09-15, 9:21 pm |
| In 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
| |
| E. Rosten 2004-09-16, 10:51 am |
| 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.
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
| |
| Jürgen Kahrs 2004-09-16, 10:51 am |
| E. 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
| |
| E. Rosten 2004-09-16, 10:51 am |
|
>
> 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
| |
|
| "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
|
|
|
|
|