gareth0402
gareth0402

Reputation: 103

Removing leading 0 from third column

I'm trying to remove the first 0 from the third column in my CSV file

tel.csv -

 test,01test,01234567890
 test,01test,09876054321

I have been trying to use the following with no luck -

cat tel.csv | sed 's/^0*//'

Upvotes: 1

Views: 74

Answers (3)

markp-fuso
markp-fuso

Reputation: 35546

Assumptions:

  • 3rd column consists of only numbers (0-9)
  • 3rd column could have multiple leading 0's

Adding a row with a 3rd column that has multiple leading 0's:

$ cat tel.csv
test,01test,01234567890
test,01test,09876054321
test,02test,00001234567890

One awk idea:

$ awk 'BEGIN{FS=OFS=","}{$3=$3+0}1' tel.csv
test,01test,1234567890
test,01test,9876054321
test,02test,1234567890

Where: adding 0 to a number ($3+0) has the side effect of removing leading 0's.

Upvotes: 1

M. Nejat Aydin
M. Nejat Aydin

Reputation: 10133

If the third field is the last field, as it is in the sample lines:

sed 's/,0\([^,]*\)$/,\1/' file

Upvotes: 0

Jetchisel
Jetchisel

Reputation: 7841

Something like:

sed 's/^\([^,]*\),\([^,]*\),0\(.*\)$/\1,\2,\3/' file.csv

Or awk

awk 'BEGIN{FS=OFS=","}{sub(/^0/, "", $3)}1' file.csv

Upvotes: 2

Related Questions