How to replace special characters?

Tags
advanced

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 04-21-2017
How to replace special characters?

Hi Team,

I have data like this.

Code:
|*|.5|*|0.2|*|A.B|*|

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

Code:
|*|0.5|*|0.2|*|A.B|*|

How to replace |*|. with |*|0.

I tried below command which didn't work

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

Thanks...

Last edited by Corona688; 04-21-2017 at 03:47 PM..
# 2  
Old 04-21-2017
Code:
$ echo '|*|.5|*|0.2|*|A.B|*|' | sed 's/|\./|0\./'
|*|0.5|*|0.2|*|A.B|*|

# 3  
Old 04-21-2017
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 [code] and [/code] 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
# 4  
Old 04-21-2017
Why use sed?
Longhand using OSX 10.12.4, default bash terminal.
(Assumption bash is available.)
Interactive mode is a great way to test.
Code:
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~> _

This User Gave Thanks to wisecracker For This Post:
jim mcnamara (04-21-2017)
# 5  
Old 04-22-2017
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.
Code:
echo '|*|.5|*|0.2|*|A.B|*|' | sed 's/|\(\.[0-9]\)/|0\1/g'

Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to replace value of password tag in xml with blanks when special characters are there? saroopkris85 How to Post in the The UNIX and Linux Forums 7 10-27-2015 04:35 PM
Search special characters in a file and replace with meaningful text messages like Hello raka_rjit UNIX for Dummies Questions & Answers 2 08-11-2015 05:28 AM
Help to replace the string with special characters nikhil jain Shell Programming and Scripting 3 02-13-2015 08:16 AM
Replace special characters with backslash and character temp_user Shell Programming and Scripting 3 11-25-2014 12:47 AM
How to replace special characters? ken002 Shell Programming and Scripting 25 11-05-2012 10:10 AM
Replace special characters with Escape characters? laknar Shell Programming and Scripting 8 01-06-2012 12:40 AM
Replace special characters ratheeshjulk Shell Programming and Scripting 0 12-22-2011 09:18 PM
How to replace special characters in vi? rogerben Solaris 2 08-03-2011 06:53 AM
Replace new line with <br /> & escape special characters ted_chou12 Shell Programming and Scripting 2 02-07-2011 09:25 PM
Using sed to replace special characters nimo Shell Programming and Scripting 5 08-12-2010 03:42 AM
Single/Multiple Line with Special characters - Find & Replace in Unix Script r_sarnayak Shell Programming and Scripting 0 06-02-2010 03:34 AM
Replace special characters in multiple files - perl RMSoares Shell Programming and Scripting 1 09-01-2009 07:09 PM
Find and replace special characters in a file sujithchandra UNIX for Dummies Questions & Answers 2 10-24-2008 07:59 AM
Help with find and replace w/string containing special characters CAGIRL UNIX for Dummies Questions & Answers 4 10-07-2008 08:13 PM
Replace Special characters in a file solai UNIX for Dummies Questions & Answers 1 07-13-2006 11:36 AM
All times are GMT -4. The time now is 02:01 PM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?