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: 14 April 2014, 6:10 AM EDT
Posts: 1,640
Thanks: 58
Thanked 476 Times in 472 Posts

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

Sponsored Links
    #3  
Old 06-14-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 April 2014, 5:26 AM EDT
Location: San Jose, CA, USA
Posts: 3,456
Thanks: 141
Thanked 1,194 Times in 1,012 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: 16 April 2014, 4:46 PM EDT
Posts: 1,596
Thanks: 90
Thanked 452 Times in 409 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 04:21 PM
Single/Multiple Line with Special characters - Find & Replace in Unix Script r_sarnayak Shell Programming and Scripting 0 06-02-2010 02:34 AM
Adding a single char to a char pointer. pallak7 Programming 5 12-06-2008 10:46 PM
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-28-2008 02:43 AM
Special Char in Multiple Files thinakarmani Shell Programming and Scripting 5 12-12-2006 10:05 AM



All times are GMT -4. The time now is 06:55 AM.