简体   繁体   English

如何在perl中查找和替换多行文本

[英]How to find and replace multiple line texts in perl

I have a text file named "data" with the content:我有一个名为“data”的文本文件,内容如下:

a
b
c
abc

I'd like to find all "abc" (doesn't need to be on the same line) and replace the leading "a" to "A".我想找到所有“abc”(不需要在同一行)并将前导“a”替换为“A”。 Here "b" can be any character (one or more) but not 'c'.这里“b”可以是任何字符(一个或多个),但不能是“c”。

(This is a simplification of my actual use case.) (这是我实际用例的简化。)

I thought this perl command would do我认为这个 perl 命令会做

perl -pi.bak -e 's/a([^c]+?)c/A\1c/mg' data

With this 'data' was changed to:将此“数据”更改为:

a
b
c
Abc

I was expecting:我期待:

A
b
c
Abc

I'm not sure why perl missed the first occurrence (on line 1-3).我不确定为什么 perl 错过了第一次出现(在第 1-3 行)。

Let me know if you spot anything wrong with my perl command or you know a working alternative.如果您发现我的 perl 命令有任何问题,或者您知道一个可行的替代方案,请告诉我。 Much appreciated.非常感激。

You're reading a line at a time, applying the code to that one line.您一次阅读一行,将代码应用于该行。 It can't possibly match across multiple lines.它不可能跨多行匹配。 The simple solution is to tell perl to treat the entire file as one line using -0777 .简单的解决方案是告诉perl使用-0777将整个文件视为一行。

perl -i.bak -0777pe's/a([^c]+c)/A$1/g' data
  • Replaced the incorrect \\1 with $1 .$1替换了不正确的\\1 $1
  • Removed the useless /m .删除了无用的/m It only affects ^ and $ , but you don't use those.它只影响^$ ,但您不使用它们。
  • Removed the useless non-greedy modifier.删除了无用的非贪婪修饰符。
  • Moved the c into the capture to avoid repeating it.c移动到捕获中以避免重复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM