Bash替换为CSV多列 [英] Bash replace in CSV multiple columns
本文介绍了Bash替换为CSV多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下CSV格式:
I have the following CSV format:
data_disk01,"/opt=920MB;4512;4917;0;4855","/=4244MB;5723;6041;0;6359","/tmp=408MB;998;1053;0;1109","/var=789MB;1673;1766;0;1859","/boot=53MB;656;692;0;729"
我想从每一列中获取数据,除了第一列,它是数组中的最后一个值,像这样:
I would like to take from each column, except the first one, the last value from the array, like this:
data_disk01,"/opt=4855","/=6359","/tmp=1109","/var=1859","/boot=729"
我尝试过类似的事情:
awk 'BEGIN {FS=OFS=","} {if(NF==!1);gsub(/\=.*/,",")} 1'
仅仅是字符串,我设法做到了:
Just the string, I managed to do it with:
string="/opt=920MB;4512;4917;0;4855"
echo $string | awk '{split($0,a,";"); print a[1],a[5]}' | sed 's#=.* #=#'
/opt=4855
但是无法使它适用于整个CSV. 任何提示表示赞赏.
But could not make it work for the whole CSV. Any hints are appreciated.
推荐答案
如果输入的引号中从未包含逗号,则简单的sed脚本应该起作用:
If your input never contains commas in the quoted fields, simple sed script should work:
sed 's/=[^"]*;/=/g' file.csv
这篇关于Bash替换为CSV多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文