rajeshkumar created the topic: Perl commandline search and replace
Perl commandline search and replace
The better option is using perl command line search and replace option. The syntax is the following.
# perl -i.bak -p -e’s/old/new/g’ filename
s/old/new/g
what this does is searches for pattern “old” and replace it pattern “new”.The g option in does the serch n replace globally in the file, otherwise perl searches and replaces only the first instance of the pattern.
Lets explain the options used.
-e option allows you to define Perl code to be executed by the compiler. For example, it’s not necessary to write a “Hello World” program in Perl when you can just type this at the command line.
# perl -e ‘print “Hello World\n”‘
-p option, adds loops around your -e code.It creates code like this:
LINE:
while (<>) {
# your code goes here
} continue {
print or die “-p destination: $!\n”;
}
-i option. Actually, Perl renames the input file and reads from this renamed version while writing to a new file with the original name. If -i is given a string argument, then that string is appended to the name of the original version of the file. For example, to change all occurrences of “PHP” to “Perl” in a data file you could write something like this:
# perl -i -pe ’s/PHP/Perl/g’ file.txt
Perl reads the input file a line at a time, making the substitution, and then writing the results back to a new file that has the same name as the original file — effectively overwriting it. If you’re not so confident of your Perl abilities you might take a backup of the original file, like this:
# perl -i.bak -pe ’s/PHP/Perl/g’ file.txt
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals