Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 变量可以是';不能用于grep模式匹配_Shell_Grep - Fatal编程技术网

Shell 变量可以是';不能用于grep模式匹配

Shell 变量可以是';不能用于grep模式匹配,shell,grep,Shell,Grep,我试图使用shell脚本从一个文件中获取字符串,然后使用它获取匹配的句子 脚本如下所示: function find_str (){ echo $1 grep -e "\/$1" info.txt } for word in $(<./name.TXT); do #egrep -w "$word" info.txt #this can't work either find_str $word done 函数find_str(){ 回声1美元 grep-e

我试图使用shell脚本从一个文件中获取字符串,然后使用它获取匹配的句子

脚本如下所示:

function find_str (){
    echo $1
    grep -e "\/$1" info.txt
}

for word in $(<./name.TXT); do
   #egrep -w "$word" info.txt  #this can't work either
   find_str $word
done 
函数find_str(){
回声1美元
grep-e“\/$1”info.txt
}
对于$(中的word,“/$1”部分是关于什么的?您是否在寻找文本斜杠?您是否需要该函数

$ cat > info.txt
testing 123 dog
nothing matches a catalyst
doggerel is not poetry
my cat is a maine coone

$ cat > name.txt
dog cat
对于-w和gnu grep,仅列出具有匹配整字的行:

$ for word in $(<name.txt); do grep -w "$word" info.txt; done
testing 123 dog
my cat is a maine coone
$ for word in $(<name.txt); do grep "$word" info.txt; done
testing 123 dog
doggerel is not poetry
nothing matches a catalyst
my cat is a maine coone

$用于$中的word(此代码段中没有问题(grep示例)。您收到任何错误消息了吗?没有任何错误消息。我只收到了回显的单词,但没有任何填空的句子。我应该使用dos2unix来命名。TXT哦,是的--您应该。请始终检查,例如
file name.TXT
,以确定您正在处理的文本文件的类型。(尤其是如果该文件是在windows上创建的)谢谢!我的问题解决了:我应该先使用dos2unix name.txt:)