[SOLVED] How to quickly mass replace instances of certain word in all text files?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You'll need to separate the options, as in "sed -i -e", because any string after the -i will be tacked on as a suffix to the backup file name. Usually it's done like this
Code:
sed -iorig -e 's/.../.../g'
sed -iback -e 's/.../.../g'
You'll need to separate the options, as in "sed -i -e", because any string after the -i will be tacked on as a suffix to the backup file name. Usually it's done like this
Code:
sed -iorig -e 's/.../.../g'
sed -iback -e 's/.../.../g'
Oh! Good catch! I seldom use the -i option. Note that -ei should work (with no backup designator), but separating them is probably clearer in any case.
Oh! Good catch! I seldom use the -i option. Note that -ei should work (with no backup designator), but separating them is probably clearer in any case.
find . -name *.txt -exec sed -ie 's/\bone\b/1/g' {} \;
(untested)
#3 gives you the answers, but we're expecting you to plug them in to what you know...also, the examples in #3 didn't include the -i, which is what causes the change to be written to the file instead of just being displayed.
It's difficult to teach regular expressions (regexp) a piece at a time. Search for something like "regexp syntax" or get a good book. The chapter on the subject in "UNIX in a Nutshell" is excellent, IMO. Certainly my copy is very dog-eared.
Hi Sean.
That created a new file with the extension '.txte' and didn't rename any word.
find . -name *.txt -exec sed -i 's/\beleven\b/11/g' {} +
Hi l0f4r0. That too worked fine except if you for example want to turn the word 'one' into '1' - it also replaces all words that contain 'one' into 1 (e.g. 'someone' changes to '1').
That created a new file with the extension '.txte' and didn't rename any word.
That's because of the typo -ie (explained in #16).
Your file(s) *.txte are backup files so no change there.
Check your file(s) *.txt, they reflect changes.
That's because of the typo -ie (explained in #16).
Your file(s) *.txte are backup files so no change there.
Check your file(s) *.txt, they reflex changes.
Hi l0f4r0. That too worked fine except if you for example want to turn the word 'one' into '1' - it also replaces all words that contain 'one' into 1 (e.g. 'someone' changes to '1').
Really?? It's weird because \b used in the sed search pattern are specified in order to avoid this behavior...
As already suggested in #3, what about:
Code:
find . -name *.txt -exec sed -i 's/\<eleven\>/11/g' {} +
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.