0

I'm trying to use bash's SED command in OS X and failing.

I need to change this line #"phpunit/phpunit:3.7.*", to the same but without the # (so like this "phpunit/phpunit:3.7.*", in my file.

What is the best way to do that in sed?

I tried this: sed -i -e "s/#\"phpunit/phpunit:3.7.*\",/\"phpunit/phpunit:3.7.*\"," file.txt

but that didn't work :(

1
  • "didn't work" is very rarely a good description of a problem: Did it do nothing? Did it change too much? Did you get any error messages? Commented Feb 16, 2014 at 22:09

3 Answers 3

1

I can see an obvious error, that you use the substitution separator character (the slash) inside the regular expression part. You must escape it, like:

sed -e "s/#\"phpunit\/phpunit:3.7.*\",/\"phpunit\/phpunit:3.7.*\",/" file.txt

An improvement could be to group what you want to keep, like:

sed -e "s/#\(\"phpunit\/phpunit:3.7.*\",\)/\1/" file.txt

Both solutions yield:

"phpunit/phpunit:3.7.*",

I don't use OS X, so I omitted the -i switch. In linux is enought -i with an space following it, but in your system I think you must provide a blank string, like: -i'' but not sure, so test this part.

Sign up to request clarification or add additional context in comments.

Comments

0

Why not just use something like

sed 's/#//' file.txt

And if you need more specificty then

sed 's/#"php/"php/' file.txt

3 Comments

This works but doesn't output to the file, it dumps it into the console. How can I make these output to my file instead of the console?
I got it working with this: sed -i '' 's/#"phpunit/"phpunit/' file.txt
In the future you should probably use -i.bak flag, which will create a backup just in case the inlining fails. But otherwise yea, that'll do.
0
$ cat input
#"phpunit/phpunit:3.7.*",

Some serious back-slashing later, due to the special characters *, . and /:

$ sed  's/#"phpunit\/phpunit:3\.7\.\*",/"phpunit\/phpunit:3\.7\.\*",/' input
"phpunit/phpunit:3.7.*",

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.