SED - Delete - Delete leading zeroes from selected double quoted fields

Strip leading zeros from fields 3 and 5 only. There could be one or many leading zeroes.

"ACCOUNT","REAL","022000046977525","REAL","00000220000488","ONLINE",......

Desired output:

"ACCOUNT","REAL","22000046977525","REAL","220000488","ONLINE",......

sed -re '   s/","/\n/4;s//\n/2;   s/\n0*([0-9])/","\1/g' filename

NOTE: Assumes that all the fields are quoted.

Marks the third and fifth fields by a newline and remove any leading zeros.

In cases where the entire field is zeros, it will preserve the last zero rather than make the field vanish.


Using awk

awk -F, '{OFS=","; sub(/"0+/, "\"", $3); sub(/"0+/, "\"", $5)}1'

Shouldn't be an issue unless your header actually has leading zeros but if needed you could do:

awk -F, 'NR > 1{OFS=","; sub(/^"0+/, "\"", $3); sub(/^"0+/, "\"", $5)}1'

This will replace the “ and all leading zeros in fields 3 and 5 with just a quote.