I wish to replace all the backslashes (which appear on the same line with an include directive) with slashes.
Here’s what I have until now..
echo '#include "..\etc\filename\yes"' | sed 's&\(#include.*\)\\&\1\/&g'
This works as I expect, but the problem is that it replaces only one \ at a time… If I want to replace all three in the above text, I have to run the sed command 3 times… The g flag at the end should make the replacements globally, no?
I’m using sed 4.2.1 on Ubuntu 11.10…
The problem is the way you’re matching. The
.*is greedy, so it matches the last backslash first and then thinks it’s done. Try this:That runs the substitutions only on lines matching the first pattern.