HiT5698
asked on
grep help with escaping characters in a bash function
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
perfect! and much cleaner than my sed approach ;)
maybe one of these days I'll RTFM!
maybe one of these days I'll RTFM!
ASKER
needwm () {
ESC=$(echo $1|sed 's/\[/\\[/g;s/\]/\\]/g;s/\
if ( grep -qc1 "$ESC" $WMP/*.edit ) ; then
return -1
else
return 0
fi
}