简体   繁体   English

Perl 一个使用正则表达式替代的班轮

[英]Perl one liner using substitute with a regex

I have a file that looks like this :我有一个看起来像这样的文件:

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

And I want it to look like this :我希望它看起来像这样:

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

So I run this over the file, the goal is to take the new line off of the string ending with the persons name, followed by a colon.所以我在文件上运行这个,目标是从以人名结尾的字符串中取出新行,后跟一个冒号。 I want to change, in this case "Bill Smith:\\n" and "Tom Jones:\\n" to "Bill Smith: " and Tom Jones: ". If you look at the one liner, it does not work on the replace.我想改变,在这种情况下,“比尔史密斯:\\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

You were going for你是为了

perl -pe's/(\w+\s\w+:)\n/$1 /'

The substring matched by the first capture ( () ) is assigned to $1 , which you can use in the replacement expression.与第一个捕获 ( () ) 匹配的子字符串分配给$1 ,您可以在替换表达式中使用它。


The above can be simplified/optimized to以上可以简化/优化为

perl -pe's/\w+\s\w+:\K\n/ /'

What matched before \\K is "kept" (not replaced), so only the line feed is replaced (with a space). \\K之前匹配的是“保留”(未替换),因此仅替换换行符(带空格)。


Alternatively, you could simply replace the line feeds of odd-numbered lines.或者,您可以简单地替换奇数行的换行符。

perl -pe's/\n/ / if $. % 2'

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

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