user1768029
user1768029

Reputation: 425

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

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

Upvotes: 0

Views: 66

Answers (1)

jijinp
jijinp

Reputation: 2662

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)

Upvotes: 1

Related Questions