【问题标题】:Perl one liner using substitute with a regexPerl 一个使用正则表达式替换的衬里
【发布时间】:2020-08-26 21:12:57
【问题描述】:

我有一个看起来像这样的文件:

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

【问题讨论】:

    标签: regex perl


    【解决方案1】:

    你要去

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

    第一次捕获匹配的子字符串(())被赋值给$1,可以在替换表达式中使用。


    以上可以简化/优化为

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

    \K 之前匹配的是“保留”(未替换),因此仅替换了换行符(带有空格)。


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

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

    【讨论】:

    • 如何在冒号后面加一个空格?或者在这种情况下 $1
    • 糟糕。固定的!将替换表达式视为字符串文字。其实s/.../.../s/.../ "..." /e是一回事。所以你可以使用s/.../ $1 . " " /es/.../ "$1 " /e 或者只是s/.../$1 /
    • Regexp 特殊转义 \K 出现在 perl 5.10.0 中,以防万一有人想知道。
    • @Ouki,是的,但那个版本是 13 年前发布的。如果有人在使用 18 年前的 Perl 版本,我希望他们提及。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 2017-08-28
    • 2013-09-27
    相关资源
    最近更新 更多