3

私は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 の間にあります。

何が問題で、どうすれば理想のビジョンに到達できますか?

ありがとう、

アダム

4

3 に答える 3

4
awk 'BEGIN{FS=OFS=","}{gsub(/\"/,"");gsub(/[^,]+/,"\"&\"")}1' input
于 2013-06-18T18:45:39.793 に答える
2

sedを使用したソリューション:

sed -e 's/"//g; s/,/","/g; s/^/"/; s/$/"/'

長いパイプのコメント付きバージョン:

sed -e 's/"//g' | # removes all quotations
sed -e 's/,/","/g' | # changes all colons to ","
sed -e 's/^/"/; s/$/"/' # puts quotations in the start and end of each line
于 2013-06-18T19:02:35.920 に答える