如何在字符串中找到子字符串(或如何grep变量)? [重复]

时间:2021-10-05 19:11:47

This question already has an answer here:

这个问题在这里已有答案:

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 $4}'`
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

8 个解决方案

#1


43  

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

#2


24  

You can also compare with wildcards:

您还可以与通配符进行比较:

if [[ "$LIST" == *"$SOURCE"* ]]

如果[[“$ LIST”== *“$ SOURCE”*]]

#3


5  

If you're using bash you can just say

如果你正在使用bash,你可以说

if grep -q "$SOURCE" <<< "$LIST" ; then
    ...
fi

#4


4  

This works in Bash without forking external commands:

这适用于Bash而不需要外部命令:

function has_substring() {
   [[ "$1" != "${2/$1/}" ]]
}

Example usage:

用法示例:

name="hello/world"
if has_substring "$name" "/"
then
   echo "Indeed, $name contains a slash!"
fi

#5


1  

You can use "index" if you only want to find a single character, e.g.:

如果你只想找到一个字符,你可以使用“index”,例如:

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

#6


1  

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之后的位置,如果找到其他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索引,所以非常有吸引力。但索引搜索不是子字符串,只有第一个字符。

#7


0  

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”。那是你要的吗?

#8


0  

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

#1


43  

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

#2


24  

You can also compare with wildcards:

您还可以与通配符进行比较:

if [[ "$LIST" == *"$SOURCE"* ]]

如果[[“$ LIST”== *“$ SOURCE”*]]

#3


5  

If you're using bash you can just say

如果你正在使用bash,你可以说

if grep -q "$SOURCE" <<< "$LIST" ; then
    ...
fi

#4


4  

This works in Bash without forking external commands:

这适用于Bash而不需要外部命令:

function has_substring() {
   [[ "$1" != "${2/$1/}" ]]
}

Example usage:

用法示例:

name="hello/world"
if has_substring "$name" "/"
then
   echo "Indeed, $name contains a slash!"
fi

#5


1  

You can use "index" if you only want to find a single character, e.g.:

如果你只想找到一个字符,你可以使用“index”,例如:

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

#6


1  

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之后的位置,如果找到其他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索引,所以非常有吸引力。但索引搜索不是子字符串,只有第一个字符。

#7


0  

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”。那是你要的吗?

#8


0  

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