如何将不以特定模式开头的行连接到 UNIX 中的上一行?

2024-01-09

请查看下面的示例文件和所需的输出,以了解我正在寻找的内容。

它可以通过 shell 脚本中的循环来完成,但我正在努力获得一个awk/sed一艘班轮。

示例文件.txt

These are leaves.
These are branches.
These are greenery which gives
oxygen, provides control over temperature
and maintains cleans the air.
These are tigers
These are bears
and deer and squirrels and other animals.
These are something you want to kill
Which will see you killed in the end.
These are things you must to think to save your tomorrow.

所需输出

These are leaves.
These are branches.
These are greenery which gives oxygen, provides control over temperature and maintains cleans the air.
These are tigers
These are bears and deer and squirrels and other animals.
These are something you want to kill Which will see you killed in the end.
These are things you must to think to save your tomorrow.

使用 sed:

sed ':a;N;/\nThese/!s/\n/ /;ta;P;D' infile

导致

These are leaves.
These are branches.
These are greenery which gives oxygen, provides control over temperature and maintains cleans the air.
These are tigers
These are bears and deer and squirrels and other animals.
These are something you want to kill Which will see you killed in the end.
These are things you must to think to save your tomorrow.

下面是它的工作原理:

sed '
:a                   # Label to jump to
N                    # Append next line to pattern space
/\nThese/!s/\n/ /    # If the newline is NOT followed by "These", append
                     # the line by replacing the newline with a space
ta                   # If we changed something, jump to label
P                    # Print part until newline
D                    # Delete part until newline
' infile

The N;P;D是在模式空间中保留多行的惯用方法;条件分支部分负责处理我们追加多行的情况。

这适用于 GNU sed;对于其他 sed,例如 Mac OS 中的 sed,必须将 oneliner 分开,以便分支和标签位于单独的命令中,换行符可能必须转义,并且我们需要一个额外的分号:

sed -e ':a' -e 'N;/'$'\n''These/!s/'$'\n''/ /;ta' -e 'P;D;' infile

This last command is untested; see this answer https://stackoverflow.com/a/24276470/3266847 for differences between different seds and how to handle them.

另一种选择是按字面输入换行符:

sed -e ':a' -e 'N;/\
These/!s/\
/ /;ta' -e 'P;D;' infile

但根据定义,它不再是一句俏话。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将不以特定模式开头的行连接到 UNIX 中的上一行? 的相关文章

  • 带有存储在文件中的通配符的 grep

    我希望 grep 通过读取需要从文本文件中过滤掉的内容来过滤掉行 这是我给 grep 的内容 它存储在foo txt Users 1337 X Users 1337 R Users 1337 W 这是它应该过滤的内容 它存储在bar txt
  • XAMPP Windows 上的 Php Cron 作业

    嗯 我是这个词的新手CRON 据我所知 这是一个Unix安排特定操作在定义的时间间隔后执行的概念 我需要运行一个php文件 每小时更新一次数据库 但我的困惑在于安排执行 我在用XAMPP用于 Windows 7 上的本地开发测试 我发现了什
  • 用于读取文件的 Bash 脚本

    不知道为什么最后一行没有从脚本中删除 bin bash FILENAME 1 while read line do cut d f2 echo line done lt FILENAME cat file 1 test 2 test 3 t
  • 为什么 shell=True 的 subprocess.Popen() 在 Linux 和 Windows 上的工作方式不同?

    使用时subprocess Popen args shell True 跑步 gcc version 仅作为示例 在 Windows 上我们得到 gt gt gt from subprocess import Popen gt gt gt
  • Python子进程Exec格式错误

    抱歉 如果这个问题很愚蠢 我正在使用Pythonsubprocess在 Ubuntu Natty 11 04 中调用 bat 文件的语句 但是 我收到错误消息 Traceback most recent call last File pfa
  • 如何列出 nginx 中的所有虚拟主机

    有没有一个命令可以列出 CentOS 上 nginx 下运行的所有虚拟主机或服务器 我想将结果通过管道传输到文本文件以用于报告目的 我正在寻找与我用于 Apache 的命令类似的命令 apachectl S 2 gt 1 grep 端口 8
  • awk 返回两个变量

    现在这就是我正在做的事情 ret ls la awk print 3 9 usr echo ret awk print 1 fil echo ret awk print 2 问题是我没有运行ls我正在运行一个需要时间的命令 因此您可以理解其
  • Python bash 管道

    我想将 python 脚本的输出通过管道传输到 bash 脚本 到目前为止我所做的是尝试使用os popen sys subprocess 并试图给出一个管道的例子 os popen echo P 1 1 591336 4927369 1
  • 使用运算符 j 操作 zsh 数组

    以下代码摘自here http eseth org 2010 git in zsh html function vi git st local ahead behind remote local a gitstatus Are we on
  • sed + 删除以 ### 开头的行,包括行开头和 ### 之间的空格

    I have sed i d file 如何更改 sed 语法以便像 awk 语法一样做出反应 awk t file 例如 sed 需要删除以空格或 TAB 开头的行 然后 sed i t d file
  • Bash touch - 非法选项 -d

    昨天我拍了很多照片 但我忘记更改相机中的时区 现在所有图片的修改日期都有错误 我想将特定目录中所有文件的修改日期更改为minus 10 hours bin sh for i in Users slick Desktop 100D5200 d
  • 使用 IFS bash 进行 CSV 解析:选择“;”作为分隔符

    我有一个包含 130 列的 CSV 我需要用它来做 3 个 csv 我用 while 和 IFS 循环 因为我需要对每一行的变量进行一些处理 这是我所做的 while IFS read my 130 vars what i do with
  • 如何回忆上一个 bash 命令的参数?

    Bash 有没有办法回忆上一个命令的参数 我通常这样做vi file c其次是gcc file c Bash 有没有办法回忆上一个命令的参数 您可以使用 or 调用上一个命令的最后一个参数 Also Alt can be used to r
  • sed - 删除行尾的句点

    我正在尝试删除文本文件中行尾的句点 有些行末尾有句点 有些则没有 cat textfile sometexthere 123 22 no period moretext with period lt remove this period n
  • 从命令行更改 konsole 选项卡标题并使其持久?

    如何更改 konsole 选项卡标题 默认情况下 它设置为 u n 所以它总是自动改变 我可以这样设置 qdbus org kde konsole KONSOLE DBUS SESSION setTitle 1 My Title 但是一旦您
  • 获取 Bash 中最后运行的程序的名称

    我有一个 bash 脚本 我使用 trap 命令捕获错误 并且我希望能够打印最后一个命令的名称 失败的命令 bin bash function error echo program name trap error ERR Some comm
  • 使用 strace 修复挂起内存问题

    我有一个在 RHEL6 x 64 位 上运行的多线程进程 我发现当我尝试启动该进程时 该进程挂起并且大多数线程 同一进程的 崩溃 有些线程等待线程之间的共享内存被创建 我可以看到所有的共享内存都没有被创建 但是当我使用 strace 时 进
  • Unix 中的访问时间是多少

    我想知道访问时间是多少 我在网上搜索但得到了相同的定义 读 被改变 我知道与touch我们可以改变它 谁能用一个例子来解释一下它是如何改变的 有没有办法在unix中获取创建日期 时间 stat结构 The stat 2 结构跟踪所有文件日期
  • 如何在 bash_profile 文件中添加导出语句?

    我正在尝试了解是否必须添加导出语句来在 bash profile 文件中设置变量 我该怎么做呢 例如 如果我必须添加 export AX name 那么我应该将其简单地写在文件末尾还是我还需要编写其他内容 简单写一下export AS na
  • 在 Django shell 会话期间获取 SQL 查询计数

    有没有办法打印 Django ORM 在 Django shell 会话期间执行的原始 SQL 查询的数量 Django 调试工具栏已经提供了此类信息 例如 5 QUERIES in 5 83MS但如何从 shell 中获取它并不明显 您可

随机推荐