Mailing List Archive


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

Re: [tlug] sed / shell / redirection question



On Sun, May 06, 2007 at 11:04:40PM +0900, Mark Makdad wrote:
> TLUG,
> 
> sed 's*old/path/name*new_variable_name*' ....
> 
> But then I decided to make this into a shell script, so:
> 
> #!/bin/bash
> for X in *.php
> do
>    sed 's*old/path/name*new_variable_name*' $X > $X
> done
> 
> Which seemed to work?  I'm not sure, I tried many things, and when I executed 
> sed by itself on the shell I got the desired results.  However, I don't want to 
> rename the files a bunch of times; I want to edit them as-is.  My question is:
> 
> Why did this take all of my input files and truncate them at zero bytes?

That one was expected.  You can try it with something like creating a
file called test with the word testing.

sed 's/ing//' test > test.  You'll see that the new test has nothing in
it. 

Unless Linux's sed is far different than FreeBSD's what you'd want is
something like  sed -i '' then your commands without the redirect.  

For example, with the same file

sed -i '' 's/ing//' test; cat test

will give me the expected result, that the word testing has been changed
to test in the file called test. 

Another way to do it (I haven't used this one in awhile, so don't try it
wihtout testing it first) is

sed 'do whatever' test > test$$; mv test$$ test

The $$ will give it a unique name, based on the shell's PID (in zsh at
least).  

The -i option means make the changes to the file--in general,  sed just
echoes to the console rather than actually changing the file.  The ''
means don't make a backup, otherwise you do something like -i bak and it
will edit the file called test and also create test.bak.
 
> 
> Am I screwing up a basic redirection thing there?  And also, does anyone have a 
> stiff drink?   

No, I ran out of Diet Mountain Dew, had to buy some so it's in the
fridge now--it should be cold in an hour or so.  :)



-- 

Scott Robbins

PGP keyID EB3467D6
( 1B48 077D 66F6 9DB0 FDC2 A409 FA54 EB34 67D6 )
gpg --keyserver pgp.mit.edu --recv-keys EB3467D6

Xander: It's time for me to act like a man... and hide. 


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links