Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Perl commandline search and replace

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

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x