在处理搜索字符串元字符(Handling metacharacters in search stri

2019-11-02 20:03发布

我会在搜索字符串中使用的用户输入可能包含一个字元

为例如C#或C ++

在功能我grep命令是:

grep -E "$1|$2" test.txt

下直接更换:

grep -E "C\+\+|testWord" test.txt
grep -E "C\#|testWord" test.txt

第一抓线不错,但没有第二个。 奇怪的是,在#完全忽略。 没有直接替代,两者分别与追赶C,随后testWord而不是C ++和C#什么

我试着使用SED处理它

$temp = `echo $1 | sed 's/[\#\!\&\;\`\"\'\|\*\?\~\<\>\^\(\)\[\]\{\}\$\+\\]/\\&/g'`

但它不工作的权利。 或者有没有其他的方式来处理与元字符用户输入?

提前致谢

Answer 1:

如果你是通过输入作为参数传递给脚本

#!/bin/bash

input1="$1"
input2="$2"
while read -r line
do
    case "$line" in
        *$input1*|*$input2* ) echo "found: $line";;
    esac
done  <"BooksDB.txt

产量

$ cat file
this is  a line
this line has C++ and C#
this line has only C++ and that's it
this line has only C# and that's it
this is end line Caa

$ ./shell.sh C++ C#
found: this line has C++ and C#
found: this line has only C++ and that's it
found: this line has only C# and that's it

如果你正在读从输入

read -p "Enter input1:" input1
read -p "Enter input2:" input2
while read -r line
do
    case "$line" in
        *$input1|*$input2* ) echo "found: $line";;
    esac
done <"BooksDB.txt"


Answer 2:

这对我的作品:

$ testfun1(){ echo "foo $1" | grep "$1"; }
$ testfun1 C#
foo C#
$ testfun2(){ read a; echo "bar $a" | grep "$a"; }
$ testfun2
C#
bar C#

编辑:

你可以试试这个形式没有-E

$ testfun3(){ grep "$1\|$2" test.txt; }
$ testfun3 C++ awk
something about C++
blah awk blah
$ testfun3 C# sed
blah sed blah
the text containing C#
$ testfun3 C# C++
something about C++
the text containing C#


Answer 3:

只是引用在$ 1和$ 2中的所有的grep元字符将其添加到您的grep表达式之前。

事情是这样的:

quoted1=`echo "$1" | sed -e 's/\([]\.?^${}+*[]\)/\\\\\1/g'`
quoted2=`echo "$2" | sed -e 's/\([]\.?^${}+*[]\)/\\\\\1/g'`
grep -E "$quoted1\|$quoted2" test.txt

应该工作。 调整列表元字符,以适应。 处理| 是有点棘手,因为backslashing 特别之处 ,但由于我们已经backslashing反斜线我认为它是安全的。



文章来源: Handling metacharacters in search strings