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 a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Do you want to learn Quantum Computing?
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND