How to replace special characters?

Hi Team,

I have data like this.

|*|.5|*|0.2|*|A.B|*|

Would like to add zero (0) before the decimal point where there is no zero as

|*|0.5|*|0.2|*|A.B|*|

How to replace ||. with ||0.

I tried below command which didn't work

echo '|*|.5|*|0.2|*|A.B|*' | sed 's/\|\*\|\./\|\*\|0\./g'

Thanks...

$ echo '|*|.5|*|0.2|*|A.B|*|' | sed 's/|\./|0\./'
|*|0.5|*|0.2|*|A.B|*|

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

Why use sed?
Longhand using OSX 10.12.4, default bash terminal.
(Assumption bash is available.)
Interactive mode is a great way to test.

Last login: Fri Apr 21 22:35:25 on ttys000
AMIGA:amiga~> STRING='|*|.5|*|0.2|*|A.B|*|'
AMIGA:amiga~> PREFIX="|*|0"
AMIGA:amiga~> echo "$PREFIX${STRING:3}"
|*|0.5|*|0.2|*|A.B|*|
AMIGA:amiga~> _
1 Like

Post#4 requires the match at the beginning of the string.
Post#2 does only one substitution and also does it for A.B .
The following requires a digit after the dot, and because it is part of the match must mark it in \( \) and put it back via a back-reference. Also it does the substitution globally.

echo '|*|.5|*|0.2|*|A.B|*|' | sed 's/|\(\.[0-9]\)/|0\1/g'