dav*_*don 2332 string bash substring
我在Bash中有一个字符串:
string="My string"
Run Code Online (Sandbox Code Playgroud)
如何测试它是否包含另一个字符串?
if [ $string ?? 'foo' ]; then
echo "It's there!"
fi
Run Code Online (Sandbox Code Playgroud)
??
我的未知运营商在哪里?我是否使用echo grep
?
if echo "$string" | grep 'foo'; then
echo "It's there!"
fi
Run Code Online (Sandbox Code Playgroud)
这看起来有点笨拙.
Ada*_*ire 3322
如果使用双括号,您也可以在案例陈述之外使用Marcus的答案(*通配符):
string='My long string'
if [[ $string == *"My long"* ]]; then
echo "It's there!"
fi
Run Code Online (Sandbox Code Playgroud)
请注意,针串中的空格需要放在双引号之间,*
通配符应该在外面.
Mat*_*iff 538
如果您更喜欢正则表达式方法:
string='My string';
if [[ $string =~ "My" ]]
then
echo "It's there!"
fi
Run Code Online (Sandbox Code Playgroud)
Mar*_*iep 326
我不确定使用if语句,但是你可以使用case语句获得类似的效果:
case "$string" in
*foo*)
# Do stuff
;;
esac
Run Code Online (Sandbox Code Playgroud)
F. *_*uri 213
由于已经有很多使用Bash特定功能的答案,因此有一种方法可以在较差的shell(如busybox)下工作:
[ -z "${string##*$reqsubstr*}" ]
Run Code Online (Sandbox Code Playgroud)
在实践中,这可能会给:
string='echo "My string"'
for reqsubstr in 'o "M' 'alt' 'str';do
if [ -z "${string##*$reqsubstr*}" ] ;then
echo "String '$string' contain substring: '$reqsubstr'."
else
echo "String '$string' don't contain substring: '$reqsubstr'."
fi
done
Run Code Online (Sandbox Code Playgroud)
这是在bash,dash,ksh和ash(busybox)下测试的,结果总是如下:
String 'echo "My string"' contain substring: 'o "M'.
String 'echo "My string"' don't contain substring: 'alt'.
String 'echo "My string"' contain substring: 'str'.
Run Code Online (Sandbox Code Playgroud)
正如@EeroAaltonen所说,这是一个相同的演示版本,在相同的shell下测试:
myfunc() {
reqsubstr="$1"
shift
string="$@"
if [ -z "${string##*$reqsubstr*}" ] ;then
echo "String '$string' contain substring: '$reqsubstr'.";
else
echo "String '$string' don't contain substring: '$reqsubstr'."
fi
}
Run Code Online (Sandbox Code Playgroud)
然后:
$ myfunc 'o "M' 'echo "My String"'
String 'echo "My String"' contain substring 'o "M'.
$ myfunc 'alt' 'echo "My String"'
String 'echo "My String"' don't contain substring 'alt'.
Run Code Online (Sandbox Code Playgroud)
注意:您必须转义或双重引号和/或双引号:
$ myfunc 'o "M' echo "My String"
String 'echo My String' don't contain substring: 'o "M'.
$ myfunc 'o "M' echo \"My String\"
String 'echo "My String"' contain substring: 'o "M'.
Run Code Online (Sandbox Code Playgroud)
stringContain() { [ -z "${2##*$1*}" ]; }
Run Code Online (Sandbox Code Playgroud)
这就是所有人!
那么现在:
$ if stringContain 'o "M3' 'echo "My String"';then echo yes;else echo no;fi
no
$ if stringContain 'o "M' 'echo "My String"';then echo yes;else echo no;fi
yes
Run Code Online (Sandbox Code Playgroud)
...或者,如果提交的字符串可能为空,如@Sjlver指出的那样,该函数将变为:
stringContain() { [ -z "${2##*$1*}" ] && [ -z "$1" -o -n "$2" ]; }
Run Code Online (Sandbox Code Playgroud)
或者按照AdrianGünter的评论建议,避免-o
切换:
stringContain() { [ -z "${2##*$1*}" ] && { [ -z "$1" ] || [ -n "$2" ];};}
Run Code Online (Sandbox Code Playgroud)
使用空字符串:
stringContain() { [ -z "$1" ] || { [ -z "${2##*$1*}" ] && [ -n "$2" ];};}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ker 143
您应该记住,shell脚本不是一种语言,而是一组命令.你本能地认为这种"语言"要求你遵循if
a [
或a [[
.这两个都只是返回退出状态的命令,指示成功或失败(就像所有其他命令一样).因为这个原因我会使用grep
,而不是[
命令.
做就是了:
if grep -q foo <<<"$string"; then
echo "It's there"
fi
Run Code Online (Sandbox Code Playgroud)
现在您正在考虑if
测试其后面的命令的退出状态(以分号结尾).为什么不重新考虑你正在测试的字符串的来源?
## Instead of this
filetype="$(file -b "$1")"
if grep -q "tar archive" <<<"$filetype"; then
#...
## Simply do this
if file -b "$1" | grep -q "tar archive"; then
#...
Run Code Online (Sandbox Code Playgroud)
该-q
选项使grep不输出任何内容,因为我们只需要返回代码.<<<
使shell扩展下一个单词并将其用作命令的输入,这是本<<
文档的单行版本(我不确定这是标准还是基础).
eph*_*ent 89
接受的答案是最好的,但由于有多种方法可以做到这一点,这是另一种解决方案:
if [ "$string" != "${string/foo/}" ]; then
echo "It's there!"
fi
Run Code Online (Sandbox Code Playgroud)
${var/search/replace}
是$var
第一个search
被替换的实例replace
,如果找到它(它没有改变$var
).如果你试图替换foo
什么,并且字符串已经改变,那么显然foo
是找到了.
Pau*_*rly 51
所以这个问题有很多有用的解决方案 - 但哪个最快/使用最少的资源?
使用此框架重复测试:
/usr/bin/time bash -c 'a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do TEST ; x=$(($x-1)); done'
Run Code Online (Sandbox Code Playgroud)
每次更换TEST:
[[ $b =~ $a ]] 2.92user 0.06system 0:02.99elapsed 99%CPU
[ "${b/$a//}" = "$b" ] 3.16user 0.07system 0:03.25elapsed 99%CPU
[[ $b == *$a* ]] 1.85user 0.04system 0:01.90elapsed 99%CPU
case $b in *$a):;;esac 1.80user 0.02system 0:01.83elapsed 99%CPU
doContain $a $b 4.27user 0.11system 0:04.41elapsed 99%CPU
Run Code Online (Sandbox Code Playgroud)
(doContain在F. Houri的回答中)
对于咯咯地笑:
echo $b|grep -q $a 12.68user 30.86system 3:42.40elapsed 19%CPU !ouch!
Run Code Online (Sandbox Code Playgroud)
因此,无论是在扩展测试还是案例中,简单替代选项都可以获得胜利.外壳是便携式的.
管道输出到100000 greps是可以预料的痛苦!关于无需使用外部实用程序的旧规则是正确的.
kev*_*rpe 27
这也有效:
if printf -- '%s' "$haystack" | egrep -q -- "$needle"
then
printf "Found needle in haystack"
fi
Run Code Online (Sandbox Code Playgroud)
负面测试是:
if ! printf -- '%s' "$haystack" | egrep -q -- "$needle"
then
echo "Did not find needle in haystack"
fi
Run Code Online (Sandbox Code Playgroud)
我想这种风格更经典 - 更少依赖于Bash shell的功能.
该--
参数是纯POSIX偏执狂,用于保护输入字符串类似于选项,例如--abc
或-a
.
注意:在一个紧凑的循环这个代码将是多少比使用内部击壳特征慢,作为一个(或两个)独立的过程将被创建并经由配管连接.
Mik*_*e Q 19
Bash4 +示例.注意:当单词包含空格等时,不使用引号会导致问题.总是引用bash IMO.
以下是BASH4 +的一些示例:
例1,检查字符串中的"是"(不区分大小写):
if [[ "${str,,}" == *"yes"* ]] ;then
Run Code Online (Sandbox Code Playgroud)
例2,检查字符串中的"是"(不区分大小写):
if [[ "$(echo "$str" | tr '[:upper:]' '[:lower:]')" == *"yes"* ]] ;then
Run Code Online (Sandbox Code Playgroud)
例3,检查字符串中的"是"(区分大小写):
if [[ "${str}" == *"yes"* ]] ;then
Run Code Online (Sandbox Code Playgroud)
例4,检查字符串中的"是"(区分大小写):
if [[ "${str}" =~ "yes" ]] ;then
Run Code Online (Sandbox Code Playgroud)
例5,完全匹配(区分大小写):
if [[ "${str}" == "yes" ]] ;then
Run Code Online (Sandbox Code Playgroud)
例6,完全匹配(不区分大小写):
if [[ "${str,,}" == "yes" ]] ;then
Run Code Online (Sandbox Code Playgroud)
例7,完全匹配:
if [ "$a" = "$b" ] ;then
Run Code Online (Sandbox Code Playgroud)
例8,通配符匹配.ext(不区分大小写):
if echo "$a" | egrep -iq "\.(mp[3-4]|txt|css|jpg|png)" ; then
Run Code Online (Sandbox Code Playgroud)
请享用.
Pio*_*ski 19
接受的答案是正确的,但很难阅读和理解。
对于与搜索相关的问题,您应该始终使用$haystack习语中的 $needle。
由于其建议的编辑队列已满,我发布此内容:
haystack='There are needles here.'
if [[ "$haystack" == *"needle"* ]]; then
echo "It's there!"
fi
Run Code Online (Sandbox Code Playgroud)
小智 17
这个怎么样:
text=" <tag>bmnmn</tag> "
if [[ "$text" =~ "<tag>" ]]; then
echo "matched"
else
echo "not matched"
fi
Run Code Online (Sandbox Code Playgroud)
Sam*_*uel 12
保罗在他的表现比较中提到:
if echo "abcdefg" | grep -q "bcdef"; then
echo "String contains is true."
else
echo "String contains is not true."
fi
Run Code Online (Sandbox Code Playgroud)
这与POSIX兼容,就像Marcus提供的'case'$ string"in'答案一样,但比case语句答案稍微容易阅读.另请注意,这将比使用case语句慢得多,正如Paul指出的那样,不要在循环中使用它.
Yor*_*iev 11
此Stack Overflow答案是唯一一个捕获空格和破折号的答案:
# For null cmd arguments checking
to_check=' -t'
space_n_dash_chars=' -'
[[ $to_check == *"$space_n_dash_chars"* ]] && echo found
Run Code Online (Sandbox Code Playgroud)
一个是:
[ $(expr $mystring : ".*${search}.*") -ne 0 ] && echo 'yes' || echo 'no'
Run Code Online (Sandbox Code Playgroud)
[[ $string == *foo* ]] && echo "It's there" || echo "Couldn't find"
Run Code Online (Sandbox Code Playgroud)
我喜欢sed.
substr="foo"
nonsub="$(echo "$string" | sed "s/$substr//")"
hassub=0 ; [ "$string" != "$nonsub" ] && hassub=1
Run Code Online (Sandbox Code Playgroud)
编辑,逻辑:
使用sed从string中删除子串的实例
如果新字符串与旧字符串不同,则存在子字符串
此处回答的问题的扩展如何判断一个字符串是否包含 POSIX sh 中的另一个字符串?:
此解决方案适用于特殊字符:
# contains(string, substring)
#
# Returns 0 if the specified string contains the specified substring,
# otherwise returns 1.
contains() {
string="$1"
substring="$2"
if echo "$string" | $(type -p ggrep grep | head -1) -F -- "$substring" >/dev/null; then
return 0 # $substring is in $string
else
return 1 # $substring is not in $string
fi
}
contains "abcd" "e" || echo "abcd does not contain e"
contains "abcd" "ab" && echo "abcd contains ab"
contains "abcd" "bc" && echo "abcd contains bc"
contains "abcd" "cd" && echo "abcd contains cd"
contains "abcd" "abcd" && echo "abcd contains abcd"
contains "" "" && echo "empty string contains empty string"
contains "a" "" && echo "a contains empty string"
contains "" "a" || echo "empty string does not contain a"
contains "abcd efgh" "cd ef" && echo "abcd efgh contains cd ef"
contains "abcd efgh" " " && echo "abcd efgh contains a space"
contains "abcd [efg] hij" "[efg]" && echo "abcd [efg] hij contains [efg]"
contains "abcd [efg] hij" "[effg]" || echo "abcd [efg] hij does not contain [effg]"
contains "abcd *efg* hij" "*efg*" && echo "abcd *efg* hij contains *efg*"
contains "abcd *efg* hij" "d *efg* h" && echo "abcd *efg* hij contains d *efg* h"
contains "abcd *efg* hij" "*effg*" || echo "abcd *efg* hij does not contain *effg*"
Run Code Online (Sandbox Code Playgroud)
由于POSIX /BusyBox 问题在没有提供正确答案的情况下已关闭(恕我直言),我将在此处发布答案。
最短的答案是:
[ ${_string_##*$_substring_*} ] || echo Substring found!
Run Code Online (Sandbox Code Playgroud)
或者
[ "${_string_##*$_substring_*}" ] || echo 'Substring found!'
Run Code Online (Sandbox Code Playgroud)
需要注意的是,双哈希是强制性的一些贝壳(ash
)。以上将[ stringvalue ]
在未找到子字符串时进行评估。它没有返回错误。当找到子字符串时,结果为空并计算[ ]
。这将抛出错误代码 1,因为字符串已被完全替换(由于*
)。
最短更常见的语法:
[ -z "${_string_##*$_substring_*}" ] && echo 'Substring found!'
Run Code Online (Sandbox Code Playgroud)
或者
[ -n "${_string_##*$_substring_*}" ] || echo 'Substring found!'
Run Code Online (Sandbox Code Playgroud)
另一个:
[ "${_string_##$_substring_}" != "$_string_" ] && echo 'Substring found!'
Run Code Online (Sandbox Code Playgroud)
或者
[ "${_string_##$_substring_}" = "$_string_" ] || echo 'Substring found!'
Run Code Online (Sandbox Code Playgroud)
注意单个等号!
通用针干草堆示例如下,带有变量
#!/bin/bash
needle="a_needle"
haystack="a_needle another_needle a_third_needle"
if [[ $haystack == *"$needle"* ]]; then
echo "needle found"
else
echo "needle NOT found"
fi
Run Code Online (Sandbox Code Playgroud)
小智 5
grep -q
对此有用.
同样使用awk
:
string="unix-bash 2389"
character="@"
printf '%s' "$string" | awk -vc="$character" '{ if (gsub(c, "")) { print "Found" } else { print "Not Found" } }'
Run Code Online (Sandbox Code Playgroud)
输出:
未找到
string="unix-bash 2389"
character="-"
printf '%s' "$string" | awk -vc="$character" '{ if (gsub(c, "")) { print "Found" } else { print "Not Found" } }'
Run Code Online (Sandbox Code Playgroud)
输出:
发现
原始来源:http://unstableme.blogspot.com/2008/06/bash-search-letter-in-string-awk.html
我发现经常需要此功能,因此我以这种方式使用自制的shell函数,.bashrc
该函数使我可以按需要多次重复使用它,并且名称易于记忆:
function stringinstring()
{
case "$2" in
*"$1"*)
return 0
;;
esac
return 1
}
Run Code Online (Sandbox Code Playgroud)
要测试$string1
(比如123abcABC)中是否包含(比如abc)$string2
,我只需要运行并检查返回值,例如stringinstring "$string1" "$string2"
stringinstring "$str1" "$str2" && echo YES || echo NO
Run Code Online (Sandbox Code Playgroud)
我的.bash_profile以及我如何使用grep如果PATH包含我的2个bin目录,请不要附加它们
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
U=~/.local.bin:~/bin
if ! echo "$PATH" | grep -q "home"; then
export PATH=$PATH:${U}
fi
Run Code Online (Sandbox Code Playgroud)
case $string in (*foo*)
# Do stuff
esac
Run Code Online (Sandbox Code Playgroud)
这与/sf/answers/16070981/的答案相同。但风格简单且符合 POSIX 标准。
归档时间: |
|
查看次数: |
1891291 次 |
最近记录: |