How to convert the string to negative decimal number in ksh - unix

≯℡__Kan透↙ 提交于 2019-12-12 03:43:29

问题


if a particular character is found at certain position, I need to replace it with a number and make entire string as negative decimal number . for ex:

if } is found at 14th position it need to be replaced with 2 and make it negative decimal number:

sed -e 's/^\(.\{9\}\)}/.\12/;s/\(.\)/-\1/' <<< '123 00}000150}'

output is:

-.123 00}0001502

But, expected output is:

123 -00}00015.02

回答1:


This will work:

sed -e 's/\(.* \)\(.\{8\}\)\(.*\)}/\1-\2.\32/' <<< '123 00}000150}'

\1 will have value: 123 ( Matches from first character till first space)

\2 : 00}00015 ( Next 8 characters)

\3 : 0 ( Characters until next } is found)



来源:https://stackoverflow.com/questions/36790322/how-to-convert-the-string-to-negative-decimal-number-in-ksh-unix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!