重击在CSV列中选择多个值

时间:2018-07-10 11:26:38

标签: linux bash devops

我具有以下CSV格式:

"/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"

我想从每个列中提取2个值,即数组中第一个和最后一个值,在“ =”之后,如下所示:

"/opt=920MB;4855","/=4244MB;6359","/tmp=408MB;1109","/var=789MB;1859","/boot=53MB;729"

每列可以这样工作:

echo "$string" | awk 'BEGIN{FS=OFS=";"} {split($0,a,";"); print a[1],a[5]}'
/opt=920MB;4855

任何提示都值得赞赏。

3 个答案:

答案 0 :(得分:0)

s/\([^;]*\);\([^;]*\)\[^"]*\([^;]*\);\([^;]*\)\[^"]*\([^;]*\);\([^;]*\)\[^"]*\([^;]*\);\([^;]*\)\[^"]*\([^;]*\);\([^;]*\)\[^"]*/\1;\2;\3;\4;\5;\6;\7;\8;\9;\10/

答案 1 :(得分:0)

使用sed找到解决方案:

echo "$string"| sed 's/=*;[^"]*;/,/g'

答案 2 :(得分:0)

您无需这样做即可完成操作:

xargs -d"\"" -n 1 -a file.csv | xargs -d";" | awk '/ key (start|stop) / {next} {if (match($0,"/")) printf("\"%s;%s\",",$1,$NF)} END {print ""}' |  rev | cut -c 2- | rev