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

👤 Login to reply

Old 10-17-2017
mehimadri12 mehimadri12 is offline
Registered User
UNIX command to ignore replacing a search string if it is already present


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^
sed 's/FOLDER\^\//FOLDER\^/g'

2) replace Folder^ by Folder^/
sed 's/FOLDER\^/FOLDER\^\//g'

Last edited by RudiC; 10-17-2017 at 01:27 PM..
Sponsored Links
Old 10-17-2017
RudiC RudiC is offline Forum Staff  
sed 's/Folder\^\/*/Folder\^\//g'

or e.g.
sed 's#Folder\^/*#Folder^/#g'

Sponsored Links
Old 10-17-2017
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
sed 's|\(Folder^\)/*|\1/|g'

Sponsored Links
👤 Login to reply

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

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

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
Show Password

Not a Forum Member?
Forgot Password?