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/

相关文章:

awk - 如何使用awk解析具有多种记录类型的文件

unix - 在变量中使用 sed 中的特殊字符

linux - sed 模式命令

bash - 在centos7 docker镜像上连接到mongodb

python - 用于执行 Python 程序的 Bash 脚本

linux - cut、colrm、awk 和 sed : fail to cut characters from a pipe stream 的奇怪问题

string - 在列开头的每一行中添加字符串

bash - 在 sed 中按模式排除行

sql - Bash Shell 脚本中的数据库访问

bash - 将最后一个命令存储在 Bash 中的变量中