其他分享
首页 > 其他分享> > 文本多行替换

文本多行替换

作者:互联网

 

 

https://unix.stackexchange.com/questions/555948/replace-multi-line-string-with-multi-line-string-without-escaping-by-hand

 

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

 

 

https://www.baeldung.com/linux/sed-replace-multi-line-string

https://linuxhint.com/use-sed-replace-multiple-lines/

https://unix.stackexchange.com/questions/26284/how-can-i-use-sed-to-replace-a-multi-line-string

 

 

Replace multi line string with multi line string without escaping by hand

Ask Question Asked 2 years, 7 months ago Modified 2 years, 7 months ago Viewed 972 times   1

Say I have a text file text.txt and I want to replace a (multi-line) string that is contained in before.txt with another string that is contained in after.txt, how do I do that? (I dont want to use regular expression I literally want to replace the string contained in before.txt with the text contained in after.txt in text.txt.

I was hoping to use the methods proposed in: https://unix.stackexchange.com/a/26289/288916

I tried:

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

But unless I am a complete idiot and messed something trivial up I cannot simply extend it to loading a string to be found from a file with cat.

Perhaps something is going wrong with the escaping. The file before.txt contains symbols such as /[]".


Thanks @ilkkachu, I tried:

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

, but it is still not working correctly. I got it working in one instance by making sure the string in before exactly matches the whole lines in which the strings was to be replaced. But it does not work for instance to replace a string that is not found at the start of the line. Example: text.txt file containing:

Here is 
some text.

before.txt contains: text

after.txt contains: whatever

No chance is made.

Share Improve this question   edited Dec 7, 2019 at 15:30     asked Dec 6, 2019 at 17:19 user avatar Kvothe 28122 silver badges1010 bronze badges Add a comment

2 Answers

5  
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.
Share Improve this answer   edited Dec 9, 2019 at 17:11     answered Dec 6, 2019 at 18:48 user avatar ilkkachu 117k1515 gold badges205205 silver badges342342 bronze badges Show 2 more comments   0

Backticks inside single quotes will be treated literally. You should be able to replace them with double quotes in the example you have provided.

Also backticks are non-preferable for a few reasons, so here I have changed them for $( )

perl -i -p0e "s/$(cat before.txt)/$(cat after.txt)/se" text.txt

NB: I am merely addressing your quoting problem, I make no promises that your command will do what you intend.

Share Improve this answer      

标签:多行,文本,string,text,after,cat,txt,替换,before
来源: https://www.cnblogs.com/sinferwu/p/16455754.html