#!/bin/sh # # backslashify # # AUTHOR: # Dan Harkless # # COPYRIGHT: # This file is Copyright (C) 1998 by Dan Harkless, and is released under the # GNU General Public License . # # USAGE: # % backslashify [-basic] [] # # DESCRIPTION: # This filter puts backslashes in front of characters that are special to # commands that process regular expressions. # # By default, all extended regexp characters, [$()*+.?[\^|], are backslashed. # This is especially useful when you have a script that's 'egrep'ping for an # expression that includes a variable, the contents of which are unpredictable # and may include special characters (e.g. "c++filt.c"). # # On the other hand, if your script is using 'sed' on a variable, you only # want basic (not extended) regexp characters to be backslashed: [$*.[\^]. # This is accomplished by using the -basic option. # # If you put anything else on the commandline, it will be backslashified. # Otherwise, stdin will be backslashified. # # DATE MODIFICATION # ========== ================================================================== # 1998-03-04 Added -basic for use with non-extended regexp commands like sed. # 1997-06-04 Original. basic_regexp=0 if [ "$1" = "-basic" ]; then basic_regexp=1 shift fi if [ $# = 0 ]; then # Backslashify stdin. if [ $basic_regexp = 1 ]; then sed 's/[$*.[\^]/\\&/g' else # extended regexp sed 's/[$()*+.?[\^|]/\\&/g' fi else # Backslashify arguments. if [ $basic_regexp = 1 ]; then echo "$*" | sed 's/[$*.[\^]/\\&/g' else # extended regexp echo "$*" | sed 's/[$()*+.?[\^|]/\\&/g' fi fi