gpt4 book ai didi

shell - 每当遇到逗号时在文件中插入换行符 - Shell 脚本

转载 作者:行者123 更新时间:2023-12-04 21:58:09 25 4
gpt4 key购买 nike

我需要编写一个 shell 脚本来通过插入换行符来重新格式化文件。条件是当我们在文件中遇到逗号时应该插入一个换行符。

例如,如果文件 delimiter.txt包含:

this, is a file, that should, be added, with a, line break, when we find, a comma.



输出应该是:
this
is a file
that should
be added
with a
line break
when we find a
a comma.

可以这样做吗 grepawk ?

最佳答案

使用 GNU sed :

sed 's/, /\n/g' your.file

输出:
this
is a file
that should
be added
with a
line break
when we find a
a comma.

注意:上述语法仅适用于具有 \n 的系统。作为 Linux 和大多数 UNIX 的行分隔符。

如果您需要脚本中的门户解决方案,请使用以下表达式,该表达式使用文字新行而不是 \n :
sed 's/,[[:space:]]/\
/g' your.file

感谢@EdMorten 的建议。

关于shell - 每当遇到逗号时在文件中插入换行符 - Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18486203/

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