使用regex替换的Perl一行程序

使用regex替换的Perl一行程序,regex,perl,Regex,Perl,我有一个文件如下所示: 7th Aug 2020 10:18:35 am Bill Smith: NW: RE: Matt Reid - EUC23284 - INC1020721599 7th Aug 2020 10:22:02 am Bill Smith: VK: RE: don't think we send the price, pls help check what happened - INC1020721668 7th Aug 2020 11:00:06 am Bill Smith

我有一个文件如下所示:

7th Aug 2020 10:18:35 am Bill Smith:
NW: RE: Matt Reid - EUC23284 - INC1020721599
7th Aug 2020 10:22:02 am Bill Smith:
VK: RE: don't think we send the price, pls help check what happened - INC1020721668
7th Aug 2020 11:00:06 am Bill Smith:
*mailbox handover*
7th Aug 2020 11:06:04 am Tom Jones:
BJ - RE: Megan Holleran Unmatched Trader Trades 08/06/2020 17:35 [Restricted - External] INC1020722335
7th Aug 2020 11:07:37 am Tom Jones:
DS - RE: All summit books missing from multiple reports in ICE INC1020722348
7th Aug 2020 12:36:10 pm Tom Jones:
NW - confirm trade receipt for Jon Lett from GFI ID: 1922979 INC1020723352
7th Aug 2020 10:18:35 am Bill Smith: NW: RE: Matt Reid - EUC23284 - INC1020721599
7th Aug 2020 10:22:02 am Bill Smith: VK: RE: don't think we send the price, pls help check what happened - INC1020721668
7th Aug 2020 11:00:06 am Bill Smith: *mailbox handover*
7th Aug 2020 11:06:04 am Tom Jones: BJ - RE: Megan Holleran Unmatched Trader Trades 08/06/2020 17:35 [Restricted - External] INC1020722335
7th Aug 2020 11:07:37 am Tom Jones: DS - RE: All summit books missing from multiple reports in ICE INC1020722348
7th Aug 2020 12:36:10 pm Tom Jones: NW - confirm trade receipt for Jon Lett from GFI ID: 1922979 INC1020723352
我希望它看起来像这样:

7th Aug 2020 10:18:35 am Bill Smith:
NW: RE: Matt Reid - EUC23284 - INC1020721599
7th Aug 2020 10:22:02 am Bill Smith:
VK: RE: don't think we send the price, pls help check what happened - INC1020721668
7th Aug 2020 11:00:06 am Bill Smith:
*mailbox handover*
7th Aug 2020 11:06:04 am Tom Jones:
BJ - RE: Megan Holleran Unmatched Trader Trades 08/06/2020 17:35 [Restricted - External] INC1020722335
7th Aug 2020 11:07:37 am Tom Jones:
DS - RE: All summit books missing from multiple reports in ICE INC1020722348
7th Aug 2020 12:36:10 pm Tom Jones:
NW - confirm trade receipt for Jon Lett from GFI ID: 1922979 INC1020723352
7th Aug 2020 10:18:35 am Bill Smith: NW: RE: Matt Reid - EUC23284 - INC1020721599
7th Aug 2020 10:22:02 am Bill Smith: VK: RE: don't think we send the price, pls help check what happened - INC1020721668
7th Aug 2020 11:00:06 am Bill Smith: *mailbox handover*
7th Aug 2020 11:06:04 am Tom Jones: BJ - RE: Megan Holleran Unmatched Trader Trades 08/06/2020 17:35 [Restricted - External] INC1020722335
7th Aug 2020 11:07:37 am Tom Jones: DS - RE: All summit books missing from multiple reports in ICE INC1020722348
7th Aug 2020 12:36:10 pm Tom Jones: NW - confirm trade receipt for Jon Lett from GFI ID: 1922979 INC1020723352
所以我在文件中运行它,目标是从以人名结尾的字符串中去掉新行,后跟冒号。在本例中,我想将比尔·史密斯:\n和汤姆·琼斯:\n更改为比尔·史密斯:和汤姆·琼斯:。如果您查看一个衬里,它在更换上不起作用

cat incfile | perl -p -e 's/\w+\s\w+\:\n/\w+\s\w+\:/g'

7th Aug 2020 10:18:35 am w+sw+:NW: RE: Matt Reid - EUC23284 - INC1020721599
7th Aug 2020 10:22:02 am w+sw+:VK: RE: don't think we send the price, pls help check what happened - INC1020721668
7th Aug 2020 11:00:06 am w+sw+:*mailbox handover*
7th Aug 2020 11:06:04 am w+sw+:BJ - RE: Megan Holleran Unmatched Trader Trades 08/06/2020 17:35 [Restricted - External] INC1020722335
7th Aug 2020 11:07:37 am w+sw+:DS - RE: All summit books missing from multiple reports in ICE INC1020722348
7th Aug 2020 12:36:10 pm w+sw+:NW - confirm trade receipt for Jon Lett from GFI ID: 1922979 INC1020723352
你要去的地方

perl-pe的/\w+\s\w+:\n/$1/' 与第一个捕获匹配的子字符串被分配给$1,您可以在替换表达式中使用它

上述内容可简化/优化为:

perl-pe的/\w+\s\w+:\K\n/' 在\K之前匹配的内容不会被替换,因此只有换行符被替换为空格

或者,您可以简单地替换奇数行的换行

perl-pe的/\n//if$。%2' 你要去的地方

perl-pe的/\w+\s\w+:\n/$1/' 与第一个捕获匹配的子字符串被分配给$1,您可以在替换表达式中使用它

上述内容可简化/优化为:

perl-pe的/\w+\s\w+:\K\n/' 在\K之前匹配的内容不会被替换,因此只有换行符被替换为空格

或者,您可以简单地替换奇数行的换行

perl-pe的/\n//if$。%2'
如何在冒号后添加空格?或者在这种情况下,在$1Woops之后。固定的将替换表达式视为字符串文字。事实上,s/../../../s与s/../../../s是一回事/E因此,您可以使用s/../$1/e、 s/../$1/e或只是s/../$1/.Regexp特殊转义\K出现在perl 5.10.0中,以防有人怀疑。@Ouki,是的,但该版本是13年前推出的。如果有人使用的是18年前的Perl版本,我希望他们会提到它。我如何在冒号后面添加空格?或者在这种情况下,在$1Woops之后。固定的将替换表达式视为字符串文字。事实上,s/../../../s与s/../../../s是一回事/E因此,您可以使用s/../$1/e、 s/../$1/e或只是s/../$1/.Regexp特殊转义\K出现在perl 5.10.0中,以防有人怀疑。@Ouki,是的,但该版本是13年前推出的。如果有人使用的是18年前的Perl版本,我希望他们会提到它。