Skip to content

Instantly share code, notes, and snippets.

@djeraseit
Forked from W-Floyd/zero_strip.sh
Created August 28, 2021 22:33
Show Gist options
  • Save djeraseit/f24e009764e19a550bdd400ae3b2dc20 to your computer and use it in GitHub Desktop.
Save djeraseit/f24e009764e19a550bdd400ae3b2dc20 to your computer and use it in GitHub Desktop.
A one-liner sed expression to nicely strip leading and trailing 0's from each line of piped input
################################################################################
# ... | __zero_strip
################################################################################
#
# Zero Strip
#
# A one-liner sed expression to nicely strip leading and trailing 0's from each
# line of piped input.
#
# For example:
#
# echo '1.0
# 5.000
# 9.0001
# 50.60
# 7.24235
# .9000
# .00001' | __zero_strip
#
# Generates:
#
# 1
# 5
# 9.0001
# 50.6
# 7.24235
# 0.9
# 0.00001
#
################################################################################
__zero_strip () {
cat | sed -e 's|\(\..*[^0]\)0\{1,\}$|\1|' -e 's|\.0*$||' -e 's|^0*||' -e 's|^\.|0.|' -e 's|^$|0|'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment