PDA

View Full Version : Wierd sed error



imyjimmy
February 27th, 2009, 10:07 AM
Ok, so I'm getting this weird sed error while programming. I have a bunch of c code that have a variable named 'tofree' which I want to rename to 'freelist'

So of course, what I should do is:

sed 's\tofree\freelist\' getmem.c > getmem.c

and so on for every .c file.

You'd think this would work, but what happens is getmem.c is actually overwritten and turns into an empty file. WTF?

I had to svn revert everything. I thought sed was legit, what's going on?

Occasionally Correct
February 27th, 2009, 10:24 AM
Writing to the file you're operating on via redirection can often cause data loss. You should either write to a temporary file (a > b; mv b a) or use sed's -i option. :)

geirha
February 27th, 2009, 10:26 AM
This is because >file is interpreted by the shell, and not sed. When you type in that command, the shell will see that you want to redirect the output of a command to a file called getmem.c. So it truncates the file in preparation and starts the command ...

Look at sed's -i option. It does the change in-place, which seems to be what you want.

Also, \ is used to escape characters in sed's regular expressions. You should use / instead.


sed -i 's/\<tofree\>/freelist/g' getmem.c

imyjimmy
February 28th, 2009, 10:11 PM
thanks you guys, the -i option worked.