LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-29-2004, 11:25 PM   #1
imppayel
LQ Newbie
 
Registered: Nov 2004
Location: kolkata
Posts: 5

Rep: Reputation: 0
Question grep problem in extracting special characters


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?
 
Old 11-30-2004, 12:51 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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).


Cheers,
Tink
 
Old 11-30-2004, 01:48 AM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
grep -F will search normal strings instead of regexp.

Yves.
 
Old 12-01-2004, 02:50 AM   #4
imppayel
LQ Newbie
 
Registered: Nov 2004
Location: kolkata
Posts: 5

Original Poster
Rep: Reputation: 0
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 ?
 
Old 12-01-2004, 03:27 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you've been told.
grep -F disables special characters
 
Old 12-01-2004, 05:15 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
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.

Yves.
 
Old 12-02-2004, 05:56 AM   #7
imppayel
LQ Newbie
 
Registered: Nov 2004
Location: kolkata
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 12-02-2004, 05:59 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
unclear...

we need an example.
what you get.
what you want to replace
what you expect.
etc.

regards billy
 
Old 12-02-2004, 07:28 AM   #9
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Code:
#!/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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Special characters problem MartinZ SUSE / openSUSE 5 07-12-2011 09:21 PM
Suse 9.1 prof, special characters problem klodoma Linux - Newbie 1 09-25-2005 10:01 AM
Extracting RAR - Japanese characters Danodare Linux - Software 1 07-14-2005 05:33 AM
Extracting text with grep or awk? UrbanDruid Linux - Newbie 5 04-07-2005 02:57 PM
cups problem - special characters kentri9 Linux - Software 2 02-23-2005 04:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration