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
    • 最新
    • 标签
主页 / server / 问题 / 827764
Accepted
Valeriu
Valeriu
Asked: 2017-01-22 13:53:25 +0800 CST2017-01-22 13:53:25 +0800 CST 2017-01-22 13:53:25 +0800 CST

按文本块/行 Grep

  • 772

我有文本,其中包含一些行。所以,我需要制作几行的 GREP。例如,我有重复的文本,我应该 GREP 获取具有此重复关键字的行。

grep -o "test|test2" textfile

我的文字:

123|never for your|test
123421|never for your|test2
123412|never for your|test3
12341|never for your|test4
12311|never for your|test2
123312312|never for your|test
123321312|never for your|test2

我应该:

123|never for your|test
123421|never for your|test2
123312312|never for your|test
123321312|never for your|test2

它可以工作,但不能按我的意愿工作。它在文本中搜索所有单词“test”和“test2”。但我想获得文本块,比如一些模式,只有在“test”之后才会出现“test2”。你有什么想法吗?

scripting bash shell shell-scripting
  • 4 4 个回答
  • 5862 Views

4 个回答

  • Voted
  1. Best Answer
    Chris Rees
    2017-01-23T08:51:54+08:002017-01-23T08:51:54+08:00

    使用 sed 的简短 shell 脚本。列出第二种情况的行号,并与第一种情况的行号进行比较。打印匹配对。使用第一个参数作为文件名。可以很容易地扩展为将第二个和第三个参数作为匹配的模式。可以另存为 findnext.sh,然后运行:

    $ sh findnext.sh testfile
    

    应该很快,因为它只涉及文件的两次传递,并且具有完全可移植的优点。

    #!/bin/sh 
    # Line numbers matching test1
    mt2=$(sed -ne '/test1/=' < $1 | tr '\n' '/')
    
    for l in $(sed -ne '/test/=' < $1); do
        nextline=$(expr $l + 1)
        [ "${mt2#*$nextline/}" != "$mt2" ] && sed -ne $l,${nextline}p <$1
    done
    
    • 2
  2. Talal Al-Khalifa
    2017-01-22T14:15:24+08:002017-01-22T14:15:24+08:00

    您可以尝试 grep -E 或 egrep。请像这样尝试

    #this will show lines that have test or test2
        grep -E "test|test2" file
    

    如果你想显示有 test 和 test2 这样的 test|test2 的行,请执行此操作

    # This will show lines that has test|test2
        grep "test\|test2" file
    
    • 1
  3. Greg Tarsa
    2017-01-22T19:48:16+08:002017-01-22T19:48:16+08:00

    awk可能是您的工具:

    awk '/test$/, /test2$/' < block-text-lines.txt 
    

    一般形式是:

    awk '/start-pattern/, /end-pattern/{command}'
    

    但是由于命令块默认为打印,所以只有开始和结束模式就可以了。

    查看man awk或Gnu Awk 用户指南了解更多详细信息。

    • 0
  4. Marco
    2017-01-23T04:31:48+08:002017-01-23T04:31:48+08:00
    grep -A 1 "test$" in.txt | grep -B 1 "test2$"
    

    在 grep 手册中

    -A NUM在匹配行之后打印 NUM 行尾随上下文。

    -B NUM在匹配行之前打印 NUM 行前导上下文。

    该命令grep -Pzo ".*test$\n.*test2$" in.txt也有效,但在手册中是“这是高度实验性的, grep -P 可能会警告未实现的功能。”

    • 0

相关问题

  • 知道任何适用于 Windows 的快速可编写脚本的 ftp 客户端吗?[关闭]

  • 如果同一个任务已经在运行,如何防止计划任务运行?

  • 需要从 Batch For 循环中排除特定结果

  • Bash 脚本:要求脚本以 root 身份运行(或使用 sudo)

  • 从命令行删除旧的(非引导)Windows Vista 目录

Sidebar

Stats

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

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve