Unix/Linux Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

How to replace special characters?

UNIX for Advanced & Expert Users


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-21-2017   -   Original Discussion by Ravi.K
Ravi.K Ravi.K is offline
Registered User
 
Join Date: Jan 2017
Last Activity: 21 April 2017, 12:20 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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..
Sponsored Links
    #2  
Old Unix and Linux 04-21-2017   -   Original Discussion by Ravi.K
Padow1 Padow1 is offline
Registered User
 
Join Date: Sep 2016
Last Activity: 23 May 2017, 2:58 PM EDT
Posts: 37
Thanks: 0
Thanked 8 Times in 7 Posts

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

Sponsored Links
    #3  
Old Unix and Linux 04-21-2017   -   Original Discussion by Ravi.K
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 November 2017, 5:53 PM EST
Location: Saskatchewan
Posts: 22,519
Thanks: 1,154
Thanked 4,273 Times in 3,946 Posts
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 Unix and Linux 04-21-2017   -   Original Discussion by Ravi.K
wisecracker's Unix or Linux Image
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 13 November 2017, 2:52 PM EST
Location: Loughborough
Posts: 1,224
Thanks: 367
Thanked 323 Times in 253 Posts
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~> _

The Following User Says Thank You to wisecracker For This Useful Post:
jim mcnamara (04-21-2017)
Sponsored Links
    #5  
Old Unix and Linux 04-22-2017   -   Original Discussion by Ravi.K
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 22 November 2017, 7:48 PM EST
Location: Simplicity
Posts: 3,828
Thanks: 319
Thanked 1,283 Times in 1,161 Posts
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'

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
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
Using sed to replace special characters nimo Shell Programming and Scripting 5 08-12-2010 03:42 AM



All times are GMT -4. The time now is 01:44 AM.