Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
$ cat test.txt
foo
bar

This sed command replaces the first line in text.txt with "baz". It works fine from command line:

$ sed "1s%.*%baz%" "test.txt" > "test.txt"
$ cat test.txt
baz
bar

I'd like to be able to put that sed command inside a script named test.sh like this:

#!/bin/sh
sed "1s%.*%baz%" "test.txt" > "test.txt"

But, when I run ./test.sh it removes all lines out of text.txt (text.txt is completely emptied)?!

Any ideas?

share|improve this question
add comment

1 Answer 1

up vote 6 down vote accepted

You can't (safely) redirect a command's output to a file that it's reading. You're "lucky" that it happened to work from the command line. Probably in one case sed was able to read the entire file before the shell clobbered it, and in the other case it wasn't. The difference is probably due to some buffering issue, but it's not really worth figuring it out.

If you want to modify a file in place, you should write the output to a temporary file, then (if that succeeded) rename the temporary file to the name of the input file.

For example:

sed "1s%.*%baz%" test.txt > $$ && mv $$ test.txt

$$ is the process ID of the current shell; it's a convenient way to get a reasonably unique file name. Or you could use, say, test.txt.$$, or even tmp if you don't have something with that name.

In the particular case of the sed command, you can use the -i option to modify a file in place -- but that just causes sed to create a temporary file internally. The syntax, or even the existence, if the -i option can vary from one sed implementation to another; check your local documentation.

share|improve this answer
    
I knew it was something obvious that I was overlooking. That makes perfect sense. Thanks Keith! –  Dave Paroulek Oct 14 '11 at 12:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.