?
Solved

grep help with escaping characters in a bash function

Posted on 2005-05-15
3
Medium Priority
?
648 Views
Last Modified: 2010-04-21
hiya guys,

I have a small bash function, that searches through a range of files looking for strings, and it works great, with one problem, when the string it's looking has characters (like [] characters, the function doesn't work properly, because those characters need to be escaped before sending them to the grep function:

needwm () {
  if ( grep -qc1 "$1" $WMP/*.edit ) ; then
    return -1
  else
    return 0
  fi
}

so, how can I escape any problematic characters like []{}, before passing them to grep?
0
Comment
Question by:HiT5698
  • 2
3 Comments
 

Author Comment

by:HiT5698
ID: 14006781
I know one way is to use sed, but is there a better way?

needwm () {
  ESC=$(echo $1|sed 's/\[/\\[/g;s/\]/\\]/g;s/\{/\\{/g;s/\}/\\}/g')
  if ( grep -qc1 "$ESC" $WMP/*.edit ) ; then
    return -1
  else
    return 0
  fi
}
0
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 2000 total points
ID: 14006818
If the patterns you are searching for are not actually regular expressions, use -F grep option:

grep -qc1 -F "$1" $WMP/*.edit

From the gnu grep man page:

       -F, --fixed-strings
              Interpret  PATTERN as a list of fixed strings, separated by new-
              lines, any of which is to be matched.
0
 

Author Comment

by:HiT5698
ID: 14006860
perfect! and much cleaner than my sed approach ;)

maybe one of these days I'll RTFM!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question