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.
i am writing a program that will search for a pattern in all the file in a particular directory and replace the pattern with a given pattern.
but the problem is this pattern i am searching may contain special characters like $,#,&,* etc.
but when the user is entering the pattern i dont want her to give the trouble of writing a backslash everytime he writes a special character. how can i modify my program so that i can trace the pattern with or without special characters without writing ' \' before every special character?
Hmmm ... if the user knows what a regex is you
can expect him/her to use escapes. If he/she
doesn't, just intercept anything they type and
you escape it programmatically.
If that doesn't sound right rethink your approach :)
You can't really mix regex with "normal" chars
and make the decision what the user is really
after (well, I suppose you could present them
with a question for each special character you
find and ask as what they want it treated :D).
what is regex?
suppose the user will input the password which may contain special characters and my programe will enable the user to search the file with the old password and then replace it with a new one.
so how can i do it ?
the problem is that grep does not do any replacements. It is just a search tool. The replacement tools (awk and sed), AFAIK don't have an option such as grep's -F... I don't see how replacing arbitrary strings can be done, except by programatically quoting special characters, as someone already suggested.
grep -F is working fine
but the problem is while replacing the old string with new one with the help of SED i am unable to replace the special characters with new strings. how can i do that?
i dont want the user to be given the trouble to write '\' before every special characters like * , . , \ , $ , &.
sed s/'oldpattern_with_!@#'/new_pattern_with$#@!%/g filename is working.
trouble is with other characters as mentioned above.
#!/bin/bash
# $1 is the string to find
# $2 is the string that will replace it
eval sed '"s/'"$(sed -e 's/\([]\/*^$[]\)/\\\\\1/g' -e 's/"/\\"/g' <<<"$1")"'/'"$(sed -e 's/\([\/]\)/\\\\\1/g' -e 's/"/\\"/g' <<<"$2")"'/g"'
Example use:
cat original-file.txt | this-script.sh "from" "to" >modified-file.txt
Yves.
Last edited by theYinYeti; 12-02-2004 at 07:45 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.