How to test if string exists in file with Bash?
我有一个包含目录名称的文件:
1 2 | /tmp /var/tmp |
我想在我添加目录名之前检查Bash,如果该名称已存在于文件中。
1 | grep -Fxq"$FILENAME" my_list.txt |
如果找到名称,则退出状态为0(true),否则为1(false),因此:
1 2 3 4 5 6 | if grep -Fxq"$FILENAME" my_list.txt then # code if found else # code if not found fi |
以下是
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 | grep -Fxq"$FILENAME" my_list.txt |
如果您想(正如我所知)
-
F :影响如何解释PATTERN(固定字符串而不是正则表达式) -
x :匹配整条线 -
q :Shhhhh ......打印量极小
从man文件:
1 2 3 4 5 6 7 8 9 | -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.) |
我心中有三种方法:
1)对路径中的名称进行简短测试(我不确定这可能是您的情况)
1 | ls -a"path" | grep"name" |
2)对文件中的字符串进行简短测试
1 | grep -R"string""filepath" |
3)使用正则表达式的更长的bash脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #!/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上的正则表达式,这应该更快。
更简单的方法:
1 2 3 4 5 6 | if grep"$filename" my_list.txt > /dev/null then ... found else ... not found fi |
提示:如果您想要命令的退出状态,请发送到
最简单最简单的方法是:
1 2 3 4 5 6 7 8 | 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将返回字符串在文件中出现次数的计数。
如果我理解你的问题,这应该做你需要的。
在一行中:
1 | grep -E"(string)" /path/to/file || echo"no match found" |
-E选项使grep使用正则表达式
我的版本使用fgrep
1 2 3 4 5 6 | FOUND=`fgrep -c"FOUND" $VALIDATION_FILE` if [ $FOUND -eq 0 ]; then echo"Not able to find" else echo"able to find" fi |
如果您只想检查是否存在一行,则无需创建文件。例如。,
1 2 3 4 5 | 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 | grep -Fxq"String to be found" | ls -a |
- grep将帮助您检查内容
- ls将列出所有文件
一个无grep的解决方案,适合我:
1 2 3 4 5 6 7 8 9 | 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 2 3 4 5 6 | if grep -q"$Filename$" my_list.txt then echo"exist" else echo"not exist" fi |