Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
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

Report this thread to moderator Post Follow-up to this message
Old Post
ingo
09-16-04 02:21 AM


Re: awk newbie: how replacing dots with colons?
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

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Harris
09-16-04 02:21 AM


Re: awk newbie: how replacing dots with colons?
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


Report this thread to moderator Post Follow-up to this message
Old Post
E. Rosten
09-16-04 03:51 PM


Re: awk newbie: how replacing dots with colons?
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

Report this thread to moderator Post Follow-up to this message
Old Post
Jürgen Kahrs
09-16-04 03:51 PM


Re: awk newbie: how replacing dots with colons?

>
> 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


Report this thread to moderator Post Follow-up to this message
Old Post
E. Rosten
09-16-04 03:51 PM


Re: awk newbie: how replacing dots with colons?
"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

Report this thread to moderator Post Follow-up to this message
Old Post
ingo
09-17-04 01:10 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:12 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.