AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 783583
Accepted
Sistemas Libsur
Sistemas Libsur
Asked: 2024-09-17 02:41:42 +0800 CST2024-09-17 02:41:42 +0800 CST 2024-09-17 02:41:42 +0800 CST

正则表达式检测两个连续的行,这些行具有相同的单词和相同的顺序,但在前导、尾随和单词之间的空格不同

  • 772

我想找到一个正则表达式或 perl 单行代码来检测两行连续的行,这些行包含相同的单词且顺序相同,但前导空格、尾随空格和单词之间的空格数量可能因行而异。正则表达式必须排除那些不匹配的行。

我使用这个正则表达式来检测相同的连续行,
^\s*(.*\S)(\s\n*\1)+$
当一行与另一行之间的空格量不同时,它会失败。

我有这个示例文本

Mary has a little lamb. The live near the mountain.
Mary has a little lamb. The live near the mountain.

vrfgrgru hfiughf iujhfuihgifughuir nuhuvhfug

Mary has  a little lamb. The live near the mountain.
  Mary has a little lamb. The live near the mountain.

 Mary has a little lamb.  The live near the mountain.
Mary has a little lamb. The live near the mountain.

fghurhg bojghirejyohi ndhguhyituhgi
gbshbsiughsf bnighriugh hurhgiurhgriu

 Elvis was the greatest singer of all time. He has 18 number one in billboard charts.
Elvis was the greatest singer  of all time. He has 18 number one in billboard charts.

sgfuhgdugusd vfjsng orjgoergo ejfio dsmnjn
fbdhsbfgdbhhbfhdsfdf

Mary has a little lamb. The live near the mountain.
 Mary has a little lamb. The live near the mountain.

gsete gerttrb oijoijoijoijoijoiojo gogoerijoi

jhoiyrei  erogjrijgtrio noifdo mkooi 

This has been the coldest winter in the last 20 years.
 This has  been the coldest winter in the  last 20 years.

无论行中有多少个空格,正则表达式都应该显示包含相同信息的连续行。

没有三个连续的并且匹配的行之间没有空行。

grep
  • 1 1 个回答
  • 89 Views

1 个回答

  • Voted
  1. Best Answer
    Ed Morton
    2024-09-17T08:05:55+08:002024-09-17T08:05:55+08:00

    嗯,它不是一个正则表达式,也不是一个 perl 单行程序,但是我认为你可以在每个 Unix 机器上的任何 shell 中使用任何 awk 来做你真正想做的事情:

    $ awk '{c=$0; $1=$1} k==$0{print p ORS c ORS} {k=$0; p=c}' file
    Mary has a little lamb. The live near the mountain.
    Mary has a little lamb. The live near the mountain.
    
    Mary has  a little lamb. The live near the mountain.
      Mary has a little lamb. The live near the mountain.
    
     Mary has a little lamb.  The live near the mountain.
    Mary has a little lamb. The live near the mountain.
    
     Elvis was the greatest singer of all time. He has 18 number one in billboard charts.
    Elvis was the greatest singer  of all time. He has 18 number one in billboard charts.
    
    Mary has a little lamb. The live near the mountain.
     Mary has a little lamb. The live near the mountain.
    
    This has been the coldest winter in the last 20 years.
     This has  been the coldest winter in the  last 20 years.
    

    我假设您喜欢简洁,因为您要求使用正则表达式或 perl 单行代码,但如果您更喜欢易读性,这里再次提供更有意义的变量名和更好的空格:

    awk '
        { currRec=$0; $1=$1; currKey=$0 }
        prevKey == currKey { print prevRec ORS currRec ORS } 
        { prevKey=currKey; prevRec=currRec }
    ' file
    
    • 1

相关问题

  • 来自 `service | 的意外结果 grep`

  • 读取带有单词的文本文件及其出现次数和排序的打印输出

  • 命令 ls | grep 只显示目录(当它也应该显示文件时)

  • grep 什么时候计数,什么时候不计数

  • grep --line-buffered 直到 X 行?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve