我有一个包含目录名称的文件:
my_list.txt
:
/tmp
/var/tmp
我想在我添加目录名之前检查Bash,如果该名称已存在于该文件中。
答案 0 :(得分:559)
grep -Fxq "$FILENAME" my_list.txt
如果找到名称,则退出状态为0(true),否则为1(false),因此:
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 :(得分:83)
关于以下解决方案:
grep -Fxq "$FILENAME" my_list.txt
如果您想(正如我所知)-Fxq
用简单的英语表示:
F
:影响PATTERN的解释方式(固定字符串而不是正则表达式)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.)
答案 2 :(得分:34)
我心中有三种方法:
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的正则表达式,那么这应该更快。
答案 3 :(得分:16)
更简单的方法:
if grep "$filename" my_list.txt > /dev/null
then
... found
else
... not found
fi
提示:如果您想要命令的退出状态,请发送到/dev/null
,而不是输出。
答案 4 :(得分:7)
最简单最简单的方法是:
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将返回字符串在文件中出现次数的计数。
答案 5 :(得分:5)
如果我理解你的问题,这应该做你需要的。
一行:check="/tmp/newdirectory"; [[ -n $(grep "^$check\$" my_list.txt) ]] && echo "dir already listed" || echo "$check" >> my_list.txt
答案 6 :(得分:2)
如果您只想检查是否存在一行,则无需创建文件。例如,
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
答案 7 :(得分:2)
我的版本使用fgrep
undefined method `time_zone'
答案 8 :(得分:2)
grep -E "(string)" /path/to/file || echo "no match found"
-E选项使grep使用正则表达式
答案 9 :(得分:2)
由于某些原因,@ Thomas的解决方案对我不起作用,但是我的字符串较长,带有特殊字符和空格,因此我只是这样更改了参数:
if grep -Fxq 'string you want to find' "/path/to/file"; then
echo "Found"
else
echo "Not found"
fi
希望它对某人有帮助
答案 10 :(得分:1)
我一直在寻找一种在常规“ grep行为”中的终端和过滤器行中执行此操作的方法。将您的字符串存储在文件strings.txt
中:
string1
string2
...
然后,您可以构建诸如(string1|string2|...)
之类的正则表达式并将其用于过滤:
cmd1 | grep -P "($(cat strings.txt | tr '\n' '|' | head -c -1))" | cmd2
编辑:仅当您不使用任何正则表达式字符时,以上方法才有效,如果需要转义,可以这样进行:
cat strings.txt | python3 -c "import re, sys; [sys.stdout.write(re.escape(line[:-1]) + '\n') for line in sys.stdin]" | ...
答案 11 :(得分:1)
与其他答案略相似,但不包含cat
,并且条目可以包含空格
contains() {
[[ " ${list[@]} " =~ " ${1} " ]] && echo 'contains' || echo 'does not contain'
}
IFS=$'\r\n' list=($(<my_list.txt))
所以,对于my_list.txt
这样的
/tmp
/var/tmp
/Users/usr/dir with spaces
这些测试
contains '/tmp'
contains '/bin'
contains '/var/tmp'
contains '/Users/usr/dir with spaces'
contains 'dir with spaces'
返回
exists
does not exist
exists
exists
does not exist
答案 12 :(得分:1)
这是搜索和评估字符串或部分字符串的快速方法:
if grep -R "my-search-string" /my/file.ext
then
# string exists
else
# string not found
fi
如果命令仅通过运行返回任何结果,您也可以先进行测试:
grep -R "my-search-string" /my/file.ext
答案 13 :(得分:0)
无油脂的解决方案对我有用:
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
答案 14 :(得分:0)
grep -Fxq "String to be found" | ls -a
答案 15 :(得分:-1)
if grep -q "$Filename$" my_list.txt
then
echo "exist"
else
echo "not exist"
fi