Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

UNIX command to ignore replacing a search string if it is already present

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-17-2017   -   Original Discussion by mehimadri12
mehimadri12 mehimadri12 is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 20 November 2017, 5:52 PM EST
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
UNIX command to ignore replacing a search string if it is already present

Hello,

I am searching the following string Folder^ in a file and replacing it with Folder^/
However if the file already contains Folder^/ I want to avoid replacing it with Folder^//

To do this I have to do the following today:
1) echo "Folder^" | sed 's/Folder\^/Folder\^\//g'
I get "Folder^/"
2) echo "Folder^/" | sed 's/Folder\^/Folder\^\//g'
I get "Folder^//"

Is there a way in awk or sed to do this a single command. I know I can do the following - but I am looking for a more smarter way to do this
1) replace Folder^/ by Folder^

Code:
sed 's/FOLDER\^\//FOLDER\^/g'

2) replace Folder^ by Folder^/

Code:
sed 's/FOLDER\^/FOLDER\^\//g'


Last edited by RudiC; 10-17-2017 at 02:27 PM..
Sponsored Links
    #2  
Old Unix and Linux 10-17-2017   -   Original Discussion by mehimadri12
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 November 2017, 2:48 PM EST
Location: Aachen, Germany
Posts: 11,652
Thanks: 323
Thanked 3,620 Times in 3,325 Posts
Try

Code:
sed 's/Folder\^\/*/Folder\^\//g'

or e.g.

Code:
sed 's#Folder\^/*#Folder^/#g'

Sponsored Links
    #3  
Old Unix and Linux 10-17-2017   -   Original Discussion by mehimadri12
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 23 November 2017, 3:53 PM EST
Location: Amsterdam
Posts: 11,626
Thanks: 518
Thanked 3,382 Times in 2,981 Posts
Or

Code:
sed 's|\(Folder^\)/*|\1/|g'

Sponsored Links
Reply

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
Search a string in a file which is also present in another file in UNIX raj028 Shell Programming and Scripting 6 07-22-2016 01:24 PM
How write the ignore dupkey command in UNIX? punticci Shell Programming and Scripting 3 06-13-2013 10:31 AM
Search several string and convert into a single line for each search string using awk command AIX?. laknar Shell Programming and Scripting 5 12-09-2012 07:33 PM
Unix find command to print directory and search string princein UNIX for Dummies Questions & Answers 4 03-06-2007 08:46 AM
replacing a string in a file with command line parameter chiru_h Shell Programming and Scripting 5 08-03-2006 10:34 AM



All times are GMT -4. The time now is 07:49 PM.