3

I have tried this:

sed -i 's/'Twas/It certainly was/g' *.txt

any suggestions??

1
  • I think I know what's wrong, but in the future, please describe what happened, as well as what you expected to happen, when asking a question like this. That makes it much easier for people to figure out what's going on. Commented Nov 3, 2011 at 23:13

3 Answers 3

2

Use double quotes

sed -i "s/'Twas/It certainly was/g" *.txt
Sign up to request clarification or add additional context in comments.

Comments

2

The apostrophe in 'Twas is being interpreted as a close quote (by the shell, not sed), and then the subsequent single quote after /g is being interpreted as an open quote, which cheerfully gobbles all the way to the end of the script (or the command prompt, and then you get the mysterious > that means the shell thinks there's more to come). For this situation,

sed -i "s/'Twas/It certainly was/g" *.txt

should work; however, shell double-quoted strings do a lot of stuff you don't usually want with sed programs. If there were any regexp metacharacters at all in there I'd do instead

sed -i 's/'\''Twas/It certainly was/g' *.txt

3 Comments

oh ok how about if i wanted to change "the" or "The" with "a" or "A" (preserving the capitalization of the original)? If that makes sense...
You can't do that easily with sed because it doesn't support word-boundary matches. But perl -pi~ -e 's/\bthe\b/a/g; s/\bThe\b/A/g' will do it. I don't know any clever way to do both replacements with one regexp, but maybe someone else does.
Ok ill post another post But i mean i might just use perl thanks for all the help though :)
1

Try using following

grep -rl "Old string" directoryPath | xargs sed -i 's/oldString/new String/g'

Example :

grep -rl 10.113.1.115 matchdir | xargs sed -i 's/10.113.1.115/10.113.1.65/g'

Comments

Your Answer

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