Skip to content
Advertisement

Bash select multiple values in CSV column

I have the following CSV format:

"/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 extract 2 values from each column, the first and the last value from the array, after the “=”, like this:

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

Per column it can work like this:

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

Any hints are highly appreciated.

Advertisement

Answer

Found the solution by using sed:

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

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement