Replacing multiple special chars with single char | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Replacing multiple special chars with single char

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-14-2013
smile689 smile689 is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 28 January 2014, 5:59 AM EST
Posts: 59
Thanks: 38
Thanked 0 Times in 0 Posts
Replacing multiple special chars with single char

Hi
I've a string . And i need to replace set of characters with a single character
Means .. or . or ... and so on should be replaced with single % character
Irrespective of number of dots in between the characters , those should be replaced with single %
All the above strings should be replaced with like below
For ex

Code:
abc..cd
bcd...ad
ac.rfd
Should be replaces with 
abc%cd
bcd%ad
ac%rfd

Please help in achieving this,
Thank You
Sponsored Links
    #2  
Old 06-14-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 7 November 2014, 10:50 AM EST
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts

Code:
sed 's/\.\{1,3\}/\%/' file

Sponsored Links
    #3  
Old 06-14-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 27 November 2014, 2:41 AM EST
Location: San Jose, CA, USA
Posts: 5,103
Thanks: 196
Thanked 1,706 Times in 1,449 Posts
I believe the
Quote:
.. or . or ... and so on
in the original post means any number of consecutive periods and that any number of those strings in a line should be replaced; not just the 1st occurrence of 1 to 3 periods on a line. If my reading is correct, I believe the following simple revision of pamu's suggestion will yield the desired results:

Code:
sed 's/\.\{1,\}/%/g' file

With the input file:

Code:
abc..cd
abc..def....ghi..........jkl
bcd...ad
ac.rfd

the output produced is:

Code:
abc%cd
abc%def%ghi%jkl
bcd%ad
ac%rfd

The Following User Says Thank You to Don Cragun For This Useful Post:
smile689 (06-14-2013)
    #4  
Old 06-17-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 22 November 2014, 10:54 PM EST
Location: Simplicity
Posts: 2,053
Thanks: 134
Thanked 603 Times in 547 Posts

Code:
tr -s '.' '%' < file

tr always reads stdin, therefore the < file
The -s is the "squeeze" option.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing a single line with multiple lines in a file Priya Amaresh Shell Programming and Scripting 4 03-11-2013 05:21 PM
Single/Multiple Line with Special characters - Find & Replace in Unix Script r_sarnayak Shell Programming and Scripting 0 06-02-2010 03:34 AM
Adding a single char to a char pointer. pallak7 Programming 5 12-06-2008 11:46 PM
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-28-2008 03:43 AM
Special Char in Multiple Files thinakarmani Shell Programming and Scripting 5 12-12-2006 11:05 AM



All times are GMT -4. The time now is 04:08 AM.