ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

文本多行替换

2022-07-07 19:04:16  阅读:168  来源: 互联网

标签:多行 文本 string text after cat txt 替换 before


 

 

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
  •   perl -p or perl -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 
  •   @Steffen, thanks. I feared that. Yet note that the example is also multi-line actually. But he just writes the pattern explicitly using \n. (Which I want to avoid.)  – Kvothe  Dec 6, 2019 at 18:07 
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
  •   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 contains text\n. The other has a dot in before the newline, the other doesn't, so they don't match. You could use chomp $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
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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有