1

What's the command line equivalent of: For every file that contains "AAA" within its contents, find "BBB" and replace it with "CCC"

Thus, the command would match and replace BBB in a file:

<html>
<head></head>
<body>
AAA
Hello world!
BBB    
</body>
</html>

But Not in a file:

<html>
<head></head>
<body>
Don't match me!
BBB    
</body>
</html>

Thanks in advance!

2 Answers 2

2

Something like grep -l AAA file-names | xargs sed -i .bak 's/BBB/CCC/g' should work. In the future, you might want to ask questions like this on https://serverfault.com/ instead.

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

2 Comments

Can I replace file-names with . to go through all files? what's the .bak about? I've already made backups.
Thanks! I asked elsewhere and got the following. What does yours do differently? ::: grep -Rl AAA . | xargs sed -i -e 's/BBB/CCC/'
0

Using positive look behind also can do the trick s/(?<=AAA).*?(BBB)/CCC/g.

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.