ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Lets say that you have a file called sample.txt
and inside that file are these texts :
john
paul
#will
smith
Now the normal procedure to replace an exact string with sed is :
Code:
sed 's/\<find\>/replace/g' < oldfile > newfile
Now if i want to change in that text above the #will to will with sed , i am unable to do it the normal way because the # symbol . code that do not work :
Code:
sed 's/\<#will\>/will/g' < sample.txt > sample.tmp
Anyone knows how to bypass this issue ?
You do, since you posted it already. Put a "" in front of the #, just like you did for the <>'s.
eventually it will work if you only got #will in the text .
but if you get in that list also #williams and you only want to change #will then it will not work .
The thing is that "exact match" must be used in this case.
Quote:
sed 's/\<\#will\>/will/g' < sample.txt > sample.tmp
eventually it will work if you only got #will in the text .
but if you get in that list also #williams and you only want to change #will then it will not work .
this one does not change the file
Why not, exactly??? Changing the "will" in william to "will" in william is a zero-sum change. You're replacing the exact string...unless you tell it to replace "william" with "will", it won't look one bit different, but just remove the #. Based on what you posted, you've got your solution.
If you don't...then why don't you post an actual piece of your input file, along with detailed requirements and what you've written so far. We can't guess. If the big issue here is the # sign, just remove it....
sed 's/\<\#will\>/will/g' < sample.txt > sample.tmp
this one does not change the file
No it doesn't, because there is no string matching '<#will>' in the file.
Why are you wrapping '#will' with '<...>'? You seem to think this has some special universal significance other than simply matching those characters - it does not (maybe GNU extension?). It does not mean "exact match" in all regular expressions as you seem to imply.
To match only '#will' and not '#william', use regular expression syntax, this:
Code:
sed 's/^#will$/will/' < sample.txt > sample.tmp
Last edited by astrogeek; 06-25-2017 at 02:35 PM.
Reason: Added exact expression, elaboration
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.