user1209304
user1209304

Reputation: 428

How to remove 0 from a string using sed

I have string "001.036.020" and I need to convert it to "1.36.20". Saying other words I need to remove all "0" before digit. Is it possible to do this using sed?

Upvotes: 1

Views: 789

Answers (2)

anubhava
anubhava

Reputation: 785058

This sed should work:

sed 's/0*\([1-9]\)/\1/g'

EDIT: To handle more complex cases like:

  • 0s in between digits:
  • handle a segment with only 0s (would be collapsed to a single zero)

On Linux:

sed -r -e 's/(^|\.)0+([1-9])/\1\2/g' -e 's/(^|\.)(0)0*(\.|$)/\1\2\3/g'

OR on Mac:

sed -E -e 's/(^|\.)0+([1-9])/\1\2/g' -e 's/(^|\.)(0)0*(\.|$)/\1\2\3/g'

Upvotes: 3

Vash2593
Vash2593

Reputation: 119

echo '001.036.020' | sed 's/^0*//'

Upvotes: 0

Related Questions