2011-01-20 27 views

回答

454
grep -Fxq "$FILENAME" my_list.txt 

退出状态是0(真),如果该名称被发现,1(假)如果没有,那么:

if grep -Fxq "$FILENAME" my_list.txt 
then 
    # code if found 
else 
    # code if not found 
fi 

这里是the man page for grep相关章节:

grep [options] PATTERN [FILE...] 

-F, --fixed-strings 
     Interpret PATTERN as a list of fixed strings, separated by new- 
     lines, any of which is to be matched. 

-x, --line-regexp 
     Select only those matches that exactly match the whole line. 

-q, --quiet, --silent 
     Quiet; do not write anything to standard output. Exit immedi- 
     ately with zero status if any match is found, even if an error 
     was detected. Also see the -s or --no-messages option. 
+1

如果我从bash脚本执行这个命令如何将0或1变成一个变量? – Toren 2011-01-20 16:06:19

+3

@Toren最近的退出状态可以使用`$?`来访问。您还可以在`if`语句旁边使用grep命令(如更新后的答案所示)。 – 2011-01-20 16:08:18

+0

用希望更接近该商标的东西进行更新。 – Thomas 2011-01-20 16:08:29

5

如果我正确理解你的问题,这应该做你需要的。

  1. 里,可以指定你想通过$检查参数
  2. 如果该目录已经在列表中添加的目录,输出为“DIR已经上市的”
  3. 如果该目录尚未在列表中,它被添加到my_list.txt

在一行:在我的脑海check="/tmp/newdirectory"; [[ -n $(grep "^$check\$" my_list.txt) ]] && echo "dir already listed" || echo "$check" >> my_list.txt

22

三种方法:

1)短期测试中的路径名(我不知道这可能是你的情况)

ls -a "path" | grep "name" 


2)短测试文件中的字符串

grep -R "string" "filepath" 


3)更长的bash脚本使用正则表达式:

#!/bin/bash 

declare file="content.txt" 
declare regex="\s+string\s+" 

declare file_content=$(cat "${file}") 
if [[ " $file_content " =~ $regex ]] # please note the space before and after the file content 
    then 
     echo "found" 
    else 
     echo "not found" 
fi 

exit 

这应该是更快如果您有测试多个字符串文件内容使用循环例如更改任何cicle正则表达式。

73

对于以下解决方案:

grep -Fxq "$FILENAME" my_list.txt 

如果你想知道(像我一样),用简单的英语F什么-Fxq意味着影响模式是如何解释(固定字符串,而不是一个正则表达式),x比赛全线,q shhhhh ...最小的印刷

从man文件:

-F, --fixed-strings 

      Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. 
      (-F is specified by POSIX.) 
-x, --line-regexp 
      Select only those matches that exactly match the whole line. (-x is specified by POSIX.) 
-q, --quiet, --silent 
      Quiet; do not write anything to standard output. Exit immediately with zero status if any match is 
      found, even if an error was detected. Also see the -s or --no-messages option. (-q is specified by 
      POSIX.) 
14

更简单的办法:

if grep "$filename" my_list.txt > /dev/null 
then 
    ... found 
else 
    ... not found 
fi 

提示:如果你想命令的退出状态发送到/dev/null,但不输出。

-1
if grep -q "$Filename$" my_list.txt 
    then 
    echo "exist" 
else 
    echo "not exist" 
fi 
1

如果您只想检查是否存在一行,则不需要创建文件。例如,

if grep -xq "LINE_TO_BE_MATCHED" FILE_TO_LOOK_IN ; then 
    # code for if it exists 
else 
    # code for if it does not exist 
fi 
1

我的版本使用fgrep一样

FOUND=`fgrep -c "FOUND" $VALIDATION_FILE` 
    if [ $FOUND -eq 0 ]; then 
    echo "Not able to find" 
    else 
    echo "able to find"  
    fi 
0
grep -E "(string)" /path/to/file || echo "no match found" 

-E选项使得grep的使用正则表达式

0

一个grep的少的解决方案,为我工作:

MY_LIST=$(cat /path/to/my_list.txt) 



if [[ "${MY_LIST}" == *"${NEW_DIRECTORY_NAME}"* ]]; then 
    echo "It's there!" 
else 
echo "its not there" 
fi 

根据: https://stackoverflow.com/a/229606/3306354

1

最简单的做法是:

isInFile=$(cat file.txt | grep -c "string") 


if [ $isInFile -eq 0 ]; then 
    #string not contained in file 
else 
    #string is in file at least once 
fi 

grep的-c将返回的字符串的文件中出现的次数计数。

相关问题