{"id":2401,"date":"2017-12-08T09:26:55","date_gmt":"2017-12-08T09:26:55","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2401"},"modified":"2020-01-09T09:55:35","modified_gmt":"2020-01-09T09:55:35","slug":"perl-commandline-search-and-replace","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/perl-commandline-search-and-replace\/","title":{"rendered":"Perl commandline search and replace"},"content":{"rendered":"<p><strong>rajeshkumar created the topic: Perl commandline search and replace<\/strong><\/p>\n<p>Perl commandline search and replace<\/p>\n<p>The better option is using perl command line search and replace option. The syntax is the following.<\/p>\n<p># perl -i.bak -p -e\u2019s\/old\/new\/g\u2019 filename<br \/>\ns\/old\/new\/g<\/p>\n<p>what this does is searches for pattern \u201cold\u201d and replace it pattern \u201cnew\u201d.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.<\/p>\n<p>Lets explain the options used.<\/p>\n<p>-e option allows you to define Perl code to be executed by the compiler. For example, it\u2019s not necessary to write a \u201cHello World\u201d program in Perl when you can just type this at the command line.<\/p>\n<p># perl -e \u2018print \u201cHello World\\n\u201d\u2018<\/p>\n<p>-p option, adds loops around your -e code.It creates code like this:<\/p>\n<p>LINE:<br \/>\nwhile (<>) {<br \/>\n# your code goes here<br \/>\n} continue {<br \/>\nprint or die \u201c-p destination: $!\\n\u201d;<br \/>\n}<\/p>\n<p>-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 \u201cPHP\u201d to \u201cPerl\u201d in a data file you could write something like this:<\/p>\n<p># perl -i -pe \u2019s\/PHP\/Perl\/g\u2019 file.txt<\/p>\n<p>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 \u2014 effectively overwriting it. If you\u2019re not so confident of your Perl abilities you might take a backup of the original file, like this:<\/p>\n<p># perl -i.bak -pe \u2019s\/PHP\/Perl\/g\u2019 file.txt<\/p>\n<p>Regards,<br \/>\nRajesh Kumar<br \/>\nTwitt me @ <a href=\"http:\/\/twitter.com\/RajeshKumarIn\" target=\"_blank\" rel=\"noopener\">twitter.com\/RajeshKumarIn<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[454],"tags":[175],"class_list":["post-2401","post","type-post","status-publish","format-standard","hentry","category-shell-script","tag-perl"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=2401"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2401\/revisions"}],"predecessor-version":[{"id":2402,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2401\/revisions\/2402"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}