19

Possible Duplicate:
How to replace a string in an existing file in Perl?

I need to create a subroutine that does a search and replace in file.

Here's the contents of myfiletemplate.txt:

CATEGORY1=youknow_<PREF>  
CATEGORY2=your/<PREF>/goes/here/

Here's my replacement string: ABCD

I need to replace all instances of <PREF> to ABCD

Community
  • 1
  • 1
cr8ivecodesmith
  • 2,021
  • 5
  • 21
  • 30
  • 1
    The right column [is your friend](http://stackoverflow.com/questions/6994947/how-to-replace-a-string-in-an-existing-file-in-perl). – Linus Kleen Nov 22 '11 at 09:29

3 Answers3

44

A one liner:

perl -pi.back -e 's/<PREF>/ABCD/g;' inputfile
Toto
  • 89,455
  • 62
  • 89
  • 125
  • This saved my day, thank you! – sasi_personal Jan 06 '16 at 07:43
  • 1
    @sasi_personal: You're welcome, glad it helps. – Toto Jan 06 '16 at 12:51
  • 2
    Anyone else wondering, the `.back` creates backup files. You can remove it. – Steve Bennett Mar 22 '18 at 03:57
  • I have been trying to replace the first occurrence of a string via variables like: `perl -pi -e 's/$oldKey/$trimmedNewKey/g' AppConstants.txt`, but nothing happens. oldKey does exists in the txt file and trimmedNewKey also has value in it. – nr5 Jan 11 '20 at 19:47
  • @nr5: Probably because you haven't defined `$oldKey`. – Toto Jan 12 '20 at 10:11
  • I tried this with strings with `=` and it fails. Any idea why? – Royi Apr 25 '20 at 10:50
  • @Royi: I can't answer without more details. I think you should ask a new question with sample text and expected result. – Toto Apr 25 '20 at 11:55
  • I write `perl -pi.back -e 's/a = b;/a += b;/g;' MyCFile.c`. Yet `a = b` are not replaced. – Royi Apr 25 '20 at 12:35
  • @Royi: It should, may be you have more than 1 space around equal sign or tabulation instead of space. What is `a`? And `b`? Only single letter or expression? But, again, it is not te place to ask a question, ask your own separatly. – Toto Apr 25 '20 at 12:52
  • Think of `C` file I want to replace `a = b` by adding `b` to `a`. So replace `a = b;` by `a += b;`. Nothing but that. There is nothing special in the expression as doing it with PowerShell works (See https://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir#comment102551724_20999154). – Royi Apr 25 '20 at 13:47
  • @Royi: Sorry, I don't get you. The regex is working fine for me. But, again, it is not te place to ask a question, ask your own separatly. – Toto Apr 25 '20 at 13:52
  • When I Googled I got this result. I think better policy is having all information here. I think `perl` process the strings as regex and hence the added information should be how to escape this mode and use "pure strings". – Royi Apr 25 '20 at 14:28
  • @Toto, Now I added the missing information to the answer to be complete for those who only want to replace strings and not utilize regular expressions syntax. – Royi May 01 '20 at 18:38
  • @Royi: I disagree with your edit. The question is how to replace literally `` with `ABCD`. It's not about replacing any kind of string, there're other Q/As that solve this. – Toto May 02 '20 at 08:40
  • Since it was marked as duplicate of the more general case, I think it is viable. You can my comments and @nr5 are about general uses. – Royi May 02 '20 at 09:00
23

Quick and dirty:

#!/usr/bin/perl -w

use strict;

open(FILE, "</tmp/yourfile.txt") || die "File not found";
my @lines = <FILE>;
close(FILE);

foreach(@lines) {
   $_ =~ s/<PREF>/ABCD/g;
}

open(FILE, ">/tmp/yourfile.txt") || die "File not found";
print FILE @lines;
close(FILE);

Perhaps it i a good idea not to write the result back to your original file; instead write it to a copy and check the result first.

ericcurtin
  • 1,499
  • 17
  • 20
Erik
  • 3,777
  • 21
  • 27
16

You could also do this:

#!/usr/bin/perl

use strict;
use warnings;

$^I = '.bak'; # create a backup copy 

while (<>) {
   s/<PREF>/ABCD/g; # do the replacement
   print; # print to the modified file
}

Invoke the script with by

./script.pl input_file

You will get a file named input_file, containing your changes, and a file named input_file.bak, which is simply a copy of the original file.

canavanin
  • 2,559
  • 3
  • 25
  • 36