Kyle R . Burton on Tue, 20 Nov 2001 19:00:14 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] sed question


> I'm trying to replace some text in a bunch of docs using sed...when I do this all is fine:
> 
> $ sed s/class=\"bodytext\"//g computing.php > newfilename.php
> 
> but if I sed the file and don't change the name I get an empty file, all the contents get wiped out and the size is shown as 0 bytes:
> 
> $ sed s/class=\"bodytext\"//g computing.php > computing.php
> 
> am I missing a command line arg or is my use of the command wrong?
> 
> I'm using GNU sed version 3.02 on a RedHat 6.2 based distro.

I don't think sed supports an 'edit in place' flag.  In the second example,
the shell opens computing.php and truncates it before running the sed
command.  So sed ends up operating on an empty file.

You could rewrite the above with perl:

  perl -i.bak -pe 's/class="bodytext"//g;' computing.php

the '-i.bak' tells perl to edit the file in place ('-i'), but first back
it up with an extension of .bak.  You can omit the .bak to get it to 
just edit the file in place (though it stil uses a temporary file to do so).

  perl -i -pe 's/class="bodytext"//g;' computing.php

The '-e' tells perl the next argument is the expression to run.  The '-p'
tells it to execute that expression once per line of input and print the
resulting line.

hth
k

-- 

------------------------------------------------------------------------------
Enter in.  Be one with the object of contemplation if you
wish to understand.  Don't be an outside observer of life.
Be life.
        -- T'ao-shan
mortis@voicenet.com                            http://www.voicenet.com/~mortis
------------------------------------------------------------------------------

______________________________________________________________________
Philadelphia Linux Users Group       -      http://www.phillylinux.org
Announcements-http://lists.phillylinux.org/mail/listinfo/plug-announce
General Discussion  -  http://lists.phillylinux.org/mail/listinfo/plug