Skip to main content
added 135 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441
perl -i -p0e 's/`cat before.txt`/`cat after.txt`/se' text.txt

Here, you have backticks inside single-quotes, so they are not processed by the shell, but Perl sees them as-is. Then again, Perl also supports backticks as a form of quoting, but it doesn't work inside s///.

Having a multi-line pattern is not an issue, as long as you use -0 or -0777 on the Perl command line. (-0 will have it separate lines with the NUL character, which a text file won't have, and -0777 would read the whole file in one go.)

You could do it with double-quotes, but that would expand the contents of the files directly in the Perl script, and any special characters would be taken as part of the script. A single slash would end the s/// operator and cause issues.

Instead, have Perl read the files:

perl -i -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; s/$b/$a/s' text.txt

Here, contents of before.txt would still be taken as a regular expression. If you want to stop that, use s/\Q$b\E/$a/s instead.

I don't think you want the e flag to s/// either, it would make the replacement taken as a Perl expression (which again only really matters if you have the shell expand the file contents to the Perl command line).


In your later example you have some text.\n in text.txt and text\n in before.txt, where the \n represents a newline as usual. When the files are loaded in Perl, they're taken as-is, so the final newline in before.txt counts. The other file has a dot before the newline, the other doesn't, so they don't match.

You can remove a possible trailing newline with chomp $b; after loading the files. You can remove a possible trailing newline with e.g. chomp$b $b;=~ s/\n$//; after loading the files:

perl$ -iperl -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; chomp$b $b;=~ chomps/\n$//; $a;$a =~ s/\n$//; s/$b/$a/s' text.txt
Here is 
some whatever.
perl -i -p0e 's/`cat before.txt`/`cat after.txt`/se' text.txt

Here, you have backticks inside single-quotes, so they are not processed by the shell, but Perl sees them as-is. Then again, Perl also supports backticks as a form of quoting, but it doesn't work inside s///.

Having a multi-line pattern is not an issue, as long as you use -0 or -0777 on the Perl command line. (-0 will have it separate lines with the NUL character, which a text file won't have, and -0777 would read the whole file in one go.)

You could do it with double-quotes, but that would expand the contents of the files directly in the Perl script, and any special characters would be taken as part of the script. A single slash would end the s/// operator and cause issues.

Instead, have Perl read the files:

perl -i -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; s/$b/$a/s' text.txt

Here, contents of before.txt would still be taken as a regular expression. If you want to stop that, use s/\Q$b\E/$a/s instead.

I don't think you want the e flag to s/// either, it would make the replacement taken as a Perl expression (which again only really matters if you have the shell expand the file contents to the Perl command line).


In your later example you have some text.\n in text.txt and text\n in before.txt, where the \n represents a newline as usual. When the files are loaded in Perl, they're taken as-is, so the final newline in before.txt counts. The other file has a dot before the newline, the other doesn't, so they don't match. You can remove a possible trailing newline with chomp $b; after loading the files:

perl -i -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; chomp $b; chomp $a; s/$b/$a/s' text.txt
perl -i -p0e 's/`cat before.txt`/`cat after.txt`/se' text.txt

Here, you have backticks inside single-quotes, so they are not processed by the shell, but Perl sees them as-is. Then again, Perl also supports backticks as a form of quoting, but it doesn't work inside s///.

Having a multi-line pattern is not an issue, as long as you use -0 or -0777 on the Perl command line. (-0 will have it separate lines with the NUL character, which a text file won't have, and -0777 would read the whole file in one go.)

You could do it with double-quotes, but that would expand the contents of the files directly in the Perl script, and any special characters would be taken as part of the script. A single slash would end the s/// operator and cause issues.

Instead, have Perl read the files:

perl -i -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; s/$b/$a/s' text.txt

Here, contents of before.txt would still be taken as a regular expression. If you want to stop that, use s/\Q$b\E/$a/s instead.

I don't think you want the e flag to s/// either, it would make the replacement taken as a Perl expression (which again only really matters if you have the shell expand the file contents to the Perl command line).


In your later example you have some text.\n in text.txt and text\n in before.txt, where the \n represents a newline as usual. When the files are loaded in Perl, they're taken as-is, so the final newline in before.txt counts. The other file has a dot before the newline, the other doesn't, so they don't match.

You can remove a possible trailing newline with chomp $b; after loading the files. You can remove a possible trailing newline with e.g. $b =~ s/\n$//;:

$ perl -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; $b =~ s/\n$//; $a =~ s/\n$//; s/$b/$a/s' text.txt
Here is 
some whatever.
added 110 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441
perl -i -p0e 's/`cat before.txt`/`cat after.txt`/se' text.txt

Here, you have backticks inside single-quotes, so they are not processed by the shell, but Perl sees them as-is. Then again, Perl also supports backticks as a form of quoting, but it doesn't work inside s///.

Having a multi-line pattern is not an issue, as long as you use -0 or -0777 on the Perl command line. (-0 will have it separate lines with the NUL character, which a text file won't have, and -0777 would read the whole file in one go.)

You could do it with double-quotes, but that would expand the contents of the files directly in the Perl script, and any special characters would be taken as part of the script. A single slash would end the s/// operator and cause issues.

Instead, have Perl read the files:

perl -i -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; s/$b/$a/s' text.txt

Here, contents of before.txt would still be taken as a regular expression. If you want to stop that, use s/\Q$b\E/$a/s instead.

I don't think you want the e flag to s/// either, it would make the replacement taken as a Perl expression (which again only really matters if you have the shell expand the file contents to the Perl command line).


In your later example you have some text.\n in text.txt and text\n in before.txt, where the \n represents a newline as usual. When the files are loaded in Perl, they're taken as-is, so the final newline in before.txt counts. The other file has a dot before the newline, the other doesn't, so they don't match. You can remove a possible trailing newline with chomp $b; after loading the files.:

perl -i -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; chomp $b; chomp $a; s/$b/$a/s' text.txt
perl -i -p0e 's/`cat before.txt`/`cat after.txt`/se' text.txt

Here, you have backticks inside single-quotes, so they are not processed by the shell, but Perl sees them as-is. Then again, Perl also supports backticks as a form of quoting, but it doesn't work inside s///.

Having a multi-line pattern is not an issue, as long as you use -0 or -0777 on the Perl command line. (-0 will have it separate lines with the NUL character, which a text file won't have, and -0777 would read the whole file in one go.)

You could do it with double-quotes, but that would expand the contents of the files directly in the Perl script, and any special characters would be taken as part of the script. A single slash would end the s/// operator and cause issues.

Instead, have Perl read the files:

perl -i -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; s/$b/$a/s' text.txt

Here, contents of before.txt would still be taken as a regular expression. If you want to stop that, use s/\Q$b\E/$a/s instead.

I don't think you want the e flag to s/// either, it would make the replacement taken as a Perl expression (which again only really matters if you have the shell expand the file contents to the Perl command line).


In your later example you have some text.\n in text.txt and text\n in before.txt, where the \n represents a newline as usual. When the files are loaded in Perl, they're taken as-is, so the final newline in before.txt counts. The other file has a dot before the newline, the other doesn't, so they don't match. You can remove a possible trailing newline with chomp $b; after loading the files.

perl -i -p0e 's/`cat before.txt`/`cat after.txt`/se' text.txt

Here, you have backticks inside single-quotes, so they are not processed by the shell, but Perl sees them as-is. Then again, Perl also supports backticks as a form of quoting, but it doesn't work inside s///.

Having a multi-line pattern is not an issue, as long as you use -0 or -0777 on the Perl command line. (-0 will have it separate lines with the NUL character, which a text file won't have, and -0777 would read the whole file in one go.)

You could do it with double-quotes, but that would expand the contents of the files directly in the Perl script, and any special characters would be taken as part of the script. A single slash would end the s/// operator and cause issues.

Instead, have Perl read the files:

perl -i -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; s/$b/$a/s' text.txt

Here, contents of before.txt would still be taken as a regular expression. If you want to stop that, use s/\Q$b\E/$a/s instead.

I don't think you want the e flag to s/// either, it would make the replacement taken as a Perl expression (which again only really matters if you have the shell expand the file contents to the Perl command line).


In your later example you have some text.\n in text.txt and text\n in before.txt, where the \n represents a newline as usual. When the files are loaded in Perl, they're taken as-is, so the final newline in before.txt counts. The other file has a dot before the newline, the other doesn't, so they don't match. You can remove a possible trailing newline with chomp $b; after loading the files:

perl -i -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; chomp $b; chomp $a; s/$b/$a/s' text.txt
added 418 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441
perl -i -p0e 's/`cat before.txt`/`cat after.txt`/se' text.txt

Here, you have backticks inside single-quotes, so they are not processed by the shell, but Perl sees them as-is. Then again, Perl also supports backticks as a form of quoting, but it doesn't work inside s///.

Having a multi-line pattern is not an issue, as long as you use -0 or -0777 on the Perl command line. (-0 will have it separate lines with the NUL character, which a text file won't have, and -0777 would read the whole file in one go.)

You could do it with double-quotes, but that would expand the contents of the files directly in the Perl script, and any special characters would be taken as part of the script. A single slash would end the s/// operator and cause issues.

Instead, have Perl read the files:

perl -i -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; s/$b/$a/s' text.txt

Here, contents of before.txt would still be taken as a regular expression. If you want to stop that, use s/\Q$b\E/$a/s instead.

I don't think you want the e flag to s/// either, it would make the replacement taken as a Perl expression (which again only really matters if you have the shell expand the file contents to the Perl command line).


In your later example you have some text.\n in text.txt and text\n in before.txt, where the \n represents a newline as usual. When the files are loaded in Perl, they're taken as-is, so the final newline in before.txt counts. The other file has a dot before the newline, the other doesn't, so they don't match. You can remove a possible trailing newline with chomp $b; after loading the files.

perl -i -p0e 's/`cat before.txt`/`cat after.txt`/se' text.txt

Here, you have backticks inside single-quotes, so they are not processed by the shell, but Perl sees them as-is. Then again, Perl also supports backticks as a form of quoting, but it doesn't work inside s///.

Having a multi-line pattern is not an issue, as long as you use -0 or -0777 on the Perl command line. (-0 will have it separate lines with the NUL character, which a text file won't have, and -0777 would read the whole file in one go.)

You could do it with double-quotes, but that would expand the contents of the files directly in the Perl script, and any special characters would be taken as part of the script. A single slash would end the s/// operator and cause issues.

Instead, have Perl read the files:

perl -i -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; s/$b/$a/s' text.txt

Here, contents of before.txt would still be taken as a regular expression. If you want to stop that, use s/\Q$b\E/$a/s instead.

I don't think you want the e flag to s/// either, it would make the replacement taken as a Perl expression (which again only really matters if you have the shell expand the file contents to the Perl command line).

perl -i -p0e 's/`cat before.txt`/`cat after.txt`/se' text.txt

Here, you have backticks inside single-quotes, so they are not processed by the shell, but Perl sees them as-is. Then again, Perl also supports backticks as a form of quoting, but it doesn't work inside s///.

Having a multi-line pattern is not an issue, as long as you use -0 or -0777 on the Perl command line. (-0 will have it separate lines with the NUL character, which a text file won't have, and -0777 would read the whole file in one go.)

You could do it with double-quotes, but that would expand the contents of the files directly in the Perl script, and any special characters would be taken as part of the script. A single slash would end the s/// operator and cause issues.

Instead, have Perl read the files:

perl -i -0 -pe '$b = `cat before.txt`; $a = `cat after.txt`; s/$b/$a/s' text.txt

Here, contents of before.txt would still be taken as a regular expression. If you want to stop that, use s/\Q$b\E/$a/s instead.

I don't think you want the e flag to s/// either, it would make the replacement taken as a Perl expression (which again only really matters if you have the shell expand the file contents to the Perl command line).


In your later example you have some text.\n in text.txt and text\n in before.txt, where the \n represents a newline as usual. When the files are loaded in Perl, they're taken as-is, so the final newline in before.txt counts. The other file has a dot before the newline, the other doesn't, so they don't match. You can remove a possible trailing newline with chomp $b; after loading the files.

Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441
Loading