Linux 如何在字符串中查找子字符串(或如何 grep 变量)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4467735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find substring inside a string (or how to grep a variable)?
提问by edumike
I'm using BASH, and I don't know how to find a substring. It keeps failing, I've got a string (should this be an array?)
我正在使用 BASH,但我不知道如何查找子字符串。它一直失败,我有一个字符串(这应该是一个数组吗?)
Below, LIST
is a string list of database names, SOURCE
is the reply, one of those databases. The following still doesn't work:
下面LIST
是一个数据库名称的字符串列表,SOURCE
是回复,这些数据库之一。以下仍然不起作用:
echo "******************************************************************"
echo "* DB2 Offline Backup Script *"
echo "******************************************************************"
echo "What's the name of of the database you would like to backup?"
echo "It will be named one in this list:"
echo ""
LIST=`db2 list database directory | grep "Database alias" | awk '{print }'`
echo $LIST
echo ""
echo "******************************************************************"
echo -n ">>> "
read -e SOURCE
if expr match "$LIST" "$SOURCE"; then
echo "match"
exit -1
else
echo "no match"
fi
exit -1
I've also tried this but doesn't work:
我也试过这个,但不起作用:
if [ `expr match "$LIST" '$SOURCE'` ]; then
采纳答案by dietbuddha
LIST="some string with a substring you want to match"
SOURCE="substring"
if echo "$LIST" | grep -q "$SOURCE"; then
echo "matched";
else
echo "no match";
fi
回答by Mikel
expr is used instead of[ rather than inside it, and variables are only expanded inside double quotes, so try this:
使用 expr而不是[ 而不是在里面,变量只在双引号内展开,所以试试这个:
if expr match "$LIST" "$SOURCE"; then
But I'm not really clear what SOURCE is supposed to represent.
但我不太清楚 SOURCE 应该代表什么。
It looks like your code will read in a pattern from standard input, and exit if it matches a database alias, otherwise it will echo "ok". Is that what you want?
看起来您的代码将从标准输入中读取模式,如果匹配数据库别名则退出,否则将回显“ok”。那是你要的吗?
回答by Diego Torres Milano
Well, what about something like this:
那么,这样的事情呢:
PS3="Select database or <Q> to quit: "
select DB in db1 db2 db3; do
[ "${REPLY^*}" = 'Q' ] && break
echo "Should backup $DB..."
done
回答by sorpigal
If you're using bash you can just say
如果你使用 bash 你可以说
if grep -q "$SOURCE" <<< "$LIST" ; then
...
fi
回答by sreimer
You can also compare with wildcards:
您还可以与通配符进行比较:
if [[ "$LIST" == *"$SOURCE"* ]]
if [[ "$LIST" == *"$SOURCE"* ]]
回答by Angelo Babudro
You can use "index" if you only want to find a single character, e.g.:
如果您只想查找单个字符,则可以使用“索引”,例如:
LIST="server1 server2 server3 server4 server5"
SOURCE="3"
if expr index "$LIST" "$SOURCE"; then
echo "match"
exit -1
else
echo "no match"
fi
Output is:
输出是:
23
match
回答by Hisham H M
This works in Bash without forking external commands:
这适用于 Bash,无需分叉外部命令:
function has_substring() {
[[ "" != "${2//}" ]]
}
Example usage:
用法示例:
name="hello/world"
if has_substring "$name" "/"
then
echo "Indeed, $name contains a slash!"
fi
回答by Anatoly
expr match "$LIST" '$SOURCE'
don't work because of this function search $SOURCE from begin of the string and return the position just after pattern $SOURCE if found else 0. So you must write another code:
不工作,因为这个函数从字符串的开头搜索 $SOURCE 并返回模式 $SOURCE 之后的位置,如果找到 else 0。所以你必须写另一个代码:
expr match "$LIST" '.*'"$SOURCE" or expr "$LIST" : '.*'"$SOURCE"
The expression $SOURCE must be double quoted so as a parser may set substitution. Single quoted not substitute and the code above will search textual string $SOURCE from the beginning of the $LIST. If you need the beginning of the string subtract the length $SOURCE e.g ${#SOURCE}. You may write also
表达式 $SOURCE 必须双引号,以便解析器可以设置替换。单引号不可替代,上面的代码将从 $LIST 的开头搜索文本字符串 $SOURCE。如果您需要字符串的开头减去长度 $SOURCE,例如 ${#SOURCE}。你也可以写
expr "$LIST" : ".*\($SOURCE\)"
This function just extract $SOURCE from $LIST and return it. You'll get empty string else. But they problem with double double quote. I don't know how it resolve without using additional variable. It's light solution. So you may write in C. There is ready function strstr. Don't use expr index, So is very attractive. But index search not substring and only first char.
这个函数只是从 $LIST 中提取 $SOURCE 并返回它。否则你会得到空字符串。但是他们有双双引号的问题。我不知道如何在不使用其他变量的情况下解决它。这是轻量级的解决方案。所以你可以用C写。有现成的函数strstr。不要使用 expr 索引,所以很有吸引力。但索引搜索不是子字符串,只有第一个字符。