Bash replace in CSV multiple columns

左心房为你撑大大i 提交于 2020-01-05 04:21:11

问题


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"

I have tried something like:

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

But could not make it work for the whole CSV. Any hints are appreciated.


回答1:


If your input never contains commas in the quoted fields, simple sed script should work:

sed 's/=[^"]*;/=/g' file.csv



回答2:


Could you please try following awk and let me know if this helps you.

awk '{gsub(/=[^"]*;/,"=")} 1' Input_file

In case you want to save output into Input_file then append > temp_file && mv temp_file Input_file in above code too.



来源:https://stackoverflow.com/questions/51250515/bash-replace-in-csv-multiple-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!