Comment 19 for bug 15051

Revision history for this message
Emmanuel Rodriguez (potyl) wrote :

In reply to Marco Rodrigues:

The command grep can only be used for finding text strings, it can't be used for replacing text in a file. Based on the command that you have posted it looks like if you where trying to replace all the occurrences of 'foo' by 'bar', unless if you where really looking for the string 's/foo/bar/;', where you?

First of all you have to be careful with the text that's passed to grep, in your case you wanted to pass a semicolon ';' but you didn't escaped it. This had for effect of splitting your single command into two commands:
grep -P s/foo/bar/
x

Finally, if you want to replace text in a file you have to use another program such as sed or perl, here are some examples:

# Create a modified copy of x
sed 's/foo/bar/g' x > modified-x

# Modify the file in place, x will be replaced
sed -i 's/foo/bar/g' x

# Create a modified copy of x
perl -pe 's/foo/bar/g' x > modified-x

# Modify the file in place, x will be replaced
perl -i -pe 's/foo/bar/g' x

I hope that it helps.