文本多行替换
作者:互联网
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 1Say 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.
perlstringreplace Share Improve this question edited Dec 7, 2019 at 15:30 asked Dec 6, 2019 at 17:19 Kvothe 28122 silver badges1010 bronze badges Add a comment2 Answers
5perl -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
ilkkachu
117k1515 gold badges205205 silver badges342342 bronze badges
-
Thanks, 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. (New lines are not shown in comments. Moved it to question.) – Kvothe Dec 7, 2019 at 15:25 -
@Kvothe, in your example, the main file contains
text.\n
, while before.txt containstext\n
. The other has a dot in before the newline, the other doesn't, so they don't match. You could usechomp $b
after reading into$b
to remove a possible trailing newline from it if you don't want to match it. (I only tested with full lines, so I didn't think of that case.) – ilkkachu Dec 7, 2019 at 17:38 -
Thanks! How do I use chomp. I tried adding
$b = chomp($b)
, plus variations with backticks and different places of chomp, i.e.chomp($b = `cat before. txt`)
, but did not manage to get it working. – Kvothe Dec 9, 2019 at 15:56 -
@Kvothe, I've just used
chomp $b
, it modifies the variable directly.chomp($b =
cat before.txt)
seems to work too, it's also mentioned in the docs for chomp. – ilkkachu Dec 9, 2019 at 16:31 - Thanks, are you saying that your last command works for you for the simple test case I proposed? For me it does not. It still does not seem to find a match. (The file is overwritten but the text remains the same, same as would happen when the pattern does not occur). – Kvothe Dec 9, 2019 at 16:49
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
perl -p
orperl -n
handle the lines of the file one after the other and don't deal with the whole file at once. So you cannot simply replace a multi-line string using this method. – Steffen Ullrich Dec 6, 2019 at 17:47