gpt4 book ai didi

bash 将换行符替换为引号

转载 作者:行者123 更新时间:2023-11-29 09:48:38 24 4
gpt4 key购买 nike

我有以下 CSV 文件:

"test","test2","test
3 and some other

data"
"test4","test5","test6 and some other


data"

我想用字符 \n 替换所有的换行符(windows 或 unix 风格),所以我会得到:

"test","test2","test\n3 and some other\n\ndata"
"test4","test5","test6 and some other\n\n\ndata"

我试过 awk 但没有成功:

cat myFile.csv | awk -F \" -v OFS=\" '{
for (i=0; i<NF; i++) {
gsub("\\n", "\\\\n", $i)
}
print
}'

最佳答案

这是一种方法:

$ awk '!/"$/{sub(/$/,"\\n");printf "%s",$0;next}1' file
"test","test2","test\n3 and some other\n\ndata"
"test4","test5","test6 and some other\n\n\ndata"

关于bash 将换行符替换为引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16036744/

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