latest.csv cat latest.csv | sed --6ren">
gpt4 book ai didi

Linux/Unix bash 基本脚本 awk/sed

转载 作者:可可西里 更新时间:2023-11-01 11:43:34 25 4
gpt4 key购买 nike

我正在研究 bash 脚本。

var=$(ls -t1 | head -n1);
cat $var | sed 's/"//g' > latest.csv
cat latest.csv | sed -e 's/^\|$/"/g' -e 's/,/","/g' > from_epos.csv
echo "LATEST: $var";

这是整个脚本,它的目的是删除当前文件中的所有引号并在每个字段之间添加新的引号。

输入:

"sku","item","price","qty"
5135,"ITEM1",1.79,5
5338,"ITEM2",1.39,5
5318,"ITEM3",1.09,5
5235,"ITEM4",1.09,5
9706,"ITEM5",1.99,5

输出:

"sku","item","price","qty"
"5135","ITEM1","1.79","5
"
"5338","ITEM2","1.39","5
"
"5318","ITEM3","1.09","5
"
"5235","ITEM4","1.09","5
"
"9706","ITEM5","1.09","5
"

我的理想输出是:

"sku","item","price","qty"
"5135","ITEM1","1.79","5"
"5338","ITEM2","1.39","5"
"5318","ITEM3","1.09","5"
"5235","ITEM4","1.09","5"
"9706","ITEM5","1.99","5"

它似乎在当前输出的行之间输入随机字符 "引号在 CR 和 LF 之间。

问题是什么以及如何实现我的理想愿景?

谢谢,

亚当

最佳答案

awk 'BEGIN{FS=OFS=","}{gsub(/\"/,"");gsub(/[^,]+/,"\"&\"")}1' input

关于Linux/Unix bash 基本脚本 awk/sed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17176403/

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