Replacing text in multiple files! Guru stuff

Every day solutions to every day challenges. + Brilliant stuff

Moderators: b1o, jkerr82508

Forum rules
Please feel free to post your tip it does not have to be advanced. Also ask questions directly related to the tip here. But do not start new threads with questions or ask for help here. That is what the help section is for. forum rules: http://bjoernvold.com/forum/viewtopic.php?f=8&t=568
User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Replacing text in multiple files! Guru stuff

Postby viking60 » 18 Nov 2011, 00:10

Here is a tip with high Guru factor that will make you the center of attention on every party:
You have just broke up with Cyntia and you rather fancy Jane now.
Your Documents directory is full of those romantic love letters that worked so well with Cyntia.
So in Cyntia1.txt you put:
Cyntia I love you

In Cyntia2.txt
You are the best Cyntia

Etc etc. So to avoid doing all that hard work again you can do the following in your Documents directory:

Code: Select all

perl -i -pe 's/Cyntia/Jane/;' Cyntia*

And voila in all files starting with Cyntia "she" will be replaced with Jane.
You can also replace Cyntia in the text with a sentence like this:

Code: Select all

perl -i -pe 's/Cyntia/Jane I love you/;' Cyntia*


What a great party trick! ... If that doesn't get you the girl nothing will!

LibreOffice is somewhat unromantic though and registers that the odt file has been manipulated. You get the offer to "fix" it and thus you are stuck with Cyntia.

Works great on text files though.
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
dubigrasu
Gaming-Berserk
Posts: 81
Joined: 20 Nov 2010, 15:59

Re: Replacing text in multiple files! Guru stuff

Postby dubigrasu » 18 Nov 2011, 06:52

Poor Cyntia :|

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Replacing text in multiple files! Guru stuff

Postby viking60 » 18 Nov 2011, 11:49

:-D
Yes ..but you see he had no choice:
Jane is an Eduard Khil fan. +1
Image
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"


Return to “Tips & Tricks”