【问题标题】:How can I put a line in front of another with sed?如何使用 sed 将一条线放在另一条线的前面?
【发布时间】:2018-10-29 19:49:17
【问题描述】:

你如何转换这样的文件:

ghosts: [[8, 16]]
pacman: [3, 15]
ghosts: [[4, 26]]
pacman: [4,15]

到这样的文件:

ghosts: [[8, 16]], pacman: [3, 15]
ghosts: [[4, 26]], pacman: [4,15]

使用 sed 命令?

我尝试了sed -e 's/\npac/, pac/g' input_file,结果没有任何变化。

【问题讨论】:

标签: awk sed


【解决方案1】:

这可能对你有用(GNU sed):

 sed 'N;s/\n/, /' file

将下一行添加到当前行并用逗号后跟空格替换换行符。

【讨论】:

  • 这个效果很好,谢谢你的回答
【解决方案2】:

另一个awk

$ awk '{ORS=NR%2?", ":RS}1' file

ghosts: [[8, 16]], pacman: [3, 15]
ghosts: [[4, 26]], pacman: [4,15]

或者,pr

$ pr -2ats', ' file

这应该是最短的路!

【讨论】:

    猜你喜欢
    • 2015-01-03
    • 2022-09-23
    • 1970-01-01
    • 2011-12-12
    • 2021-12-23
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多