bash - sed 分行(高级)

标签 bash awk sed command-line grep

我正在寻找使用 sed(或将其与另一个 grep 命令结合使用)来转换以下字符串

John: Hi!,How are you,?,Dylan: Hey,OK

进入

John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK

如果不行我愿意妥协

John: Hi!,How are you,?
Dylan: Hey,OK

非常感谢

最佳答案

sed 可以使用。 有一个很棒的Sed - An Introduction and Tutorial by Bruce Barnett在线页面,我在编写 sed 脚本时总是打开它。

试试这个:

printf 'John: Hi!,How are you,?,Dylan: Hey,OK\n' | sed -n '
:1
/./ {
  /^[^:,][^:,]*: / {
    h
    s/^\([^:,][^,:]*: \).*$/\1/
    x
    s/,/\n/
    P
    D
  }
  x
  /./ {
    x
    H
    x
    s/\n//g
    x
    s/.//g
    x
    b 1
  }
}'

输出是:

John: Hi!
John: How are you
John: ?
Dylan: Hey
Dylan: OK

关于bash - sed 分行(高级),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53334047/

相关文章:

sed - 使用与Sed不同的分隔符删除

bash - 启动自定义 init.d 脚本会导致在本地主机上找不到 404 页面

linux - 输入文件没有被正确读取脚本 awk

sed - 如何使用 awk/sed 更改小数点分隔符?

bash - 删除模式前的所有内容

linux - 使用 grep 或其他命令返回多行模式的行号

linux - 在所有 bash 命令之后自动运行命令?

linux - 如何使用一系列参数调用期望脚本

bash - 检查上一个命令是否为 "valid"

linux - 如何在考虑其他文件的情况下显示文件的内容?