ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
# $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"'
cat original-file.txt | this-script.sh "from" "to" >modified-file.txt
Last edited by theYinYeti; 12-02-2004 at 07:45 AM.