osx - Reformat how a number is displayed in a line of text file with sed -
i have text file has several lines , looking replace lines formatted like:
cat: -0600, dog: +0900 cat: -1000, snake: -0500 cat: -0900 cat: +0100
to say
cat: -6, dog: +0900 cat: -10, snake: -0500 cat: -9 cat: +1
there other lines are:
dog: -0700 dog: +1000
that shouldn't touched. i've searched, can't quite figure out. prefer use sed, unless there's better way.
given composite data file, script gives sample output below:
sed 's/^\(cat: [-+]\)0\{0,1\}\([1-9]\{0,1\}[0-9]\)00/\1\2/' data
it looks cat:
followed +
or -
, optional 0 (which not captured), optional [1-9]
followed [0-9]
followed 2 0
's, , replaces 2 remembered parts. convert +0000
+0
. if last 2 digits not 00
, modify first regex match [0-5][0-9]
or [0-9][0-9]
.
sample output:
cat: -6, dog: +0900 cat: -10, snake: -0500 cat: -9 cat: +1 dog: -0700 dog: +1000
Comments
Post a Comment