gpt4 book ai didi

linux - 如何将多行输出连接到一行?

转载 作者:IT老高 更新时间:2023-10-28 12:23:22 32 4
gpt4 key购买 nike

如果我运行命令 cat file | grep 模式,我得到了很多行输出。您如何将所有行连接成一行,有效地将每个 "\n" 替换为 "\"" (以 " 结尾,后跟空格)?

cat 文件 | grep 模式 | xargs sed s/\n//g不适合我。

最佳答案

使用 tr '\n' ' ' 将所有换行符转换为空格:

$ grep pattern file | tr '\n' ' '

注意:grep 读取文件,cat 连接文件。不要 cat 文件 | grep!

编辑:

tr 只能处理单个字符的翻译。您可以使用 awk 来更改输出记录分隔符,例如:

$ grep pattern file | awk '{print}' ORS='" '

这会改变:

one
two
three

到:

one" two" three" 

关于linux - 如何将多行输出连接到一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15580144/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com