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 / 问题 / 787834
Accepted
learningregularexpressions
learningregularexpressions
Asked: 2024-12-09 02:35:46 +0800 CST2024-12-09 02:35:46 +0800 CST 2024-12-09 02:35:46 +0800 CST

如何将底线与前一行合并?

  • 772

我有一个非常基本的文件;

15
Chapter name


some text and some more text
some text and some more text

我试图得到这样的东西

Book: 15 Chapter name


some text and some more text
some text and some more text

我尝试使用 sed 和这种模式来做到这一点;

sed 's/^\([[:digit:]]\+\)\n\([[:alpha:]].*$\)\n$/Book: \1 \2\n/g'

显然,这在终端上不起作用,但是在 VIM 版本中可以完美运行sed。

text-processing
  • 3 3 个回答
  • 50 Views

3 个回答

  • Voted
  1. Best Answer
    Kusalananda
    2024-12-09T02:50:53+08:002024-12-09T02:50:53+08:00

    sed逐行读取输入,因此您不能期望在输入中匹配换行符分隔的字符串。

    但是,您可以匹配一个整数,将下一行附加到它并从那里开始工作:

    $ sed -e '/^[0-9][0-9]*$/!b' -e 'N; s/\n/ /' -e 's/^/Book: /' file
    Book: 15 Chapter name
    
    
    some text and some more text
    some text and some more text
    

    这会跳过(仅输出)所有非整数的行。但是,当检测到一个整数时,会使用 将以下行添加到缓冲区末尾N,并在两者之间添加分隔换行符。使用 将此换行符替换为空格s,然后将文本插入Book: 行首。

    如果您知道整数出现在第一行,您可以将初始命令从 更改/^[0-9][0-9]*$/!b为更短的1!b。

    • 3
  2. Gilles Quénot
    2024-12-09T12:33:51+08:002024-12-09T12:33:51+08:00

    使用Perl,在段落模式下:

    $ perl -00 -pe 's/^(\d+)\n/Book: $1 / if /^\d+/' file
    Book: 15 Chapter name
    
    some text and some more text
    some text and some more text
    
    • 0
  3. jubilatious1
    2024-12-10T06:07:49+08:002024-12-10T06:07:49+08:00

    使用Raku(以前称为 Perl_6)

    ~$ raku -ne '/^ \d**1..2 $/  ??  print "Book:\t$_ "  !!  .put;'  file
    

    Raku 是 Perl 家族中功能齐全的编程语言。上面我们使用print/put语法来合并行。在 Raku 中(与 Perl 一样),print不会自动添加\n换行符。Raku 还有一个put例程,代表Print-Using-Terminator,它会\n为您添加换行符。

    一旦您识别出下面 Raku 的新三元运算符,您就会看到它(与print/结合put)如何给出所需的答案:

    测试 ?? 真假 ;!! ​


    示例输入:

    15
    Chapter name
    
    
    some text and some more text
    some text and some more text
    

    示例输出:

    Book:   15 Chapter name
    
    
    some text and some more text
    some text and some more text
    

    https://docs.raku.org/language/5to6-nutshell#%3F_:_Ternary_operator
    https://raku.org

    • 0

相关问题

  • grep 从 $START 到 $END 的一组行并且在 $MIDDLE 中包含匹配项

  • 重新排列字母并比较两个单词

  • 在awk中的两行之间减去相同的列

  • 多行文件洗牌

  • 如何更改字符大小写(从小到大,反之亦然)?同时[重复]

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