2013-10-20 56 views
1

我正在使用shell/perl搜索一个不应该包含该字符串“section”的文件。如何测试字符串是否在文件中不存在

请帮我找到方法grep -vl命令返回一个文件名,如果字符串存在。

+2

可能重复:http://stackoverflow.com/questions/4749330/how-to-test-if-string-exists-in-file-with-bash-外壳 –

回答

5

你可以试试这样: -

grep -Fxq "$MYFILENAME" file.txt 

或者可能是这样的: -

if grep -q SearchString "$File"; then 
    Do Some Actions 
fi 
+3

你刚刚从副本复制答案,并把它放在这里? – beroe

+0

即使在查看重复问题之前,if语句也是我的答案。这只是标准的方式。但是,我会将'-F'和'-w'参数添加到我的'grep'中。 '-F'关闭正则表达式,'-w'只选择一个固定的词。 –

0

很多时候,我会做这种方式:

for f in <your_files>; do grep -q "section" $f && echo $f || continue; done 

这应该打印出包含单词“部分”的文件列表。

4

perl的

open(FILE,"<your file>"); 
    if (grep{/<your keyword>/} <FILE>){ 
     print "found\n"; 
    }else{ 
     print "word not found\n"; 
    } 
    close FILE; 
相关问题