regex - how replace string repeated pattern at his start ,linux terminal? -
i want edit string using regular expression in linux shell failed.
the thing want @ start of string change repeatedlly occurrence of "00" "1"
let : 00000120001 110100001
failed that.
i tried:
echo 00000120001 | sed 's/^\`\<00/1/g'
but got 1000120001
not sure can single regex; have solution juggling hold , pattern spaces of sed
.
in 1 line:
sed 'h;s/[^0].*//;s/00/1/g;x;s/^0*//;x;g;s/\n//' <<< '00000120001'
with details:
sed 'h # copy input line hold space s/[^0].*// # keep 00..0 preffix in pattern space s/00/1/g # replace double 0s 1s in pattern space x # swap hold , pattern spaces (hold 110, pattern 00000120001 s/^0*// # remove 00..0 prefix pattern space x # swap hold , pattern spaces (hold 120001, pattern 110 g # append hold space pattern space (pattern 110\n120001) s/\n//' # remove \n pattern <<< '00000120001'
Comments
Post a Comment