string - substitute - shell字符串截取
字符串包含在Bash中 (14)
兼容答案
由于已经有很多使用Bash特定功能的答案,所以在busybox等功能较差的shell中有一种方法:
[ -z "${string##*$reqsubstr*}" ]
在实践中,这可以给出:
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
这是在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'.
进入一个功能
正如@EeroAaltonen所问,这里是相同演示的一个版本,在相同的shell下测试:
myfunc() {
reqsubstr="$1"
shift
string="[email protected]"
if [ -z "${string##*$reqsubstr*}" ] ;then
echo "String '$string' contain substring: '$reqsubstr'.";
else
echo "String '$string' don't contain substring: '$reqsubstr'."
fi
}
然后:
$ 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'.
注意:您必须转义或加双引号和/或双引号:
$ 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'.
简单的功能
这是在busybox , dash和当然bash下测试的:
stringContain() { [ -z "${2##*$1*}" ]; }
这就是所有人!
那么现在:
$ 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
...或者,如果提交的字符串可能为空,就像@Sjlver指出的那样,该函数将变为:
stringContain() { [ -z "${2##*$1*}" ] && [ -z "$1" -o -n "$2" ]; }
或者如AdrianGünter的评论所暗示的那样,避免-o
:
stringContain() { [ -z "${2##*$1*}" ] && { [ -z "$1" ] || [ -n "$2" ] ;} ; }
空串:
$ if stringContain '' ''; then echo yes; else echo no; fi
yes
$ if stringContain 'o "M' ''; then echo yes; else echo no; fi
no
我在Bash中有一个字符串:
string="My string"
我如何测试它是否包含另一个字符串?
if [ $string ?? 'foo' ]; then
echo "It's there!"
fi
哪里??
是我未知的运营商。 我使用echo和grep
?
if echo "$string" | grep 'foo'; then
echo "It's there!"
fi
这看起来有点笨拙。
grep -q
对此很有用。
同样使用awk
:
string="unix-bash 2389"
character="@"
printf '%s' "$string" | awk -vc="$character" '{ if (gsub(c, "")) { print "Found" } else { print "Not Found" } }'
输出:
未找到
string="unix-bash 2389"
character="-"
printf '%s' "$string" | awk -vc="$character" '{ if (gsub(c, "")) { print "Found" } else { print "Not Found" } }'
输出:
发现
原文出处: http://unstableme.blogspot.com/2008/06/bash-search-letter-in-string-awk.html : http://unstableme.blogspot.com/2008/06/bash-search-letter-in-string-awk.html
你应该记住,shell脚本不是一门语言,更多的是一组命令。 本能地,你认为这个“语言”要求你跟随一个if
[
或一个[[
。 这两者都只是返回退出状态的命令,指示成功或失败(就像其他命令一样)。 出于这个原因,我会使用grep
,而不是[
命令。
做就是了:
if grep -q foo <<<"$string"; then
echo "It's there"
fi
现在,您正在考虑是否正在测试后续命令的退出状态(以分号结尾)。 为什么不重新考虑你正在测试的字符串的来源?
## 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
#...
-q
选项使grep不输出任何内容,因为我们只需要返回代码。 <<<
使shell扩展下一个单词,并将其用作命令的输入,这是<<
here文档的单行版本(我不确定这是标准还是双击)。
准确的单词匹配:
string='My long string'
exactSearch='long'
if grep -E -q "\b${exactSearch}\b" <<<${string} >/dev/null 2>&1
then
echo "It's there"
fi
如果使用双括号,你也可以在case语句之外使用Marcus的答案(*通配符) :
string='My long string'
if [[ $string = *"My long"* ]]; then
echo "It's there!"
fi
请注意,针串中的空格需要放在双引号之间,并且*
通配符应该放在外面。
我不确定使用if语句,但是您可以通过case语句获得类似的效果:
case "$string" in
*foo*)
# Do stuff
;;
esac
我发现需要这种功能的频率非常高,所以我在我的.bashrc
使用了一个自制的shell函数,这样我就可以根据需要随时重新使用它,并带有一个易于记忆的名称:
function stringinstring()
{
case "$2" in
*"$1"*)
return 0
;;
esac
return 1
}
为了测试$string1
(比如说abc )是否包含在$string2
(比如123abcABC )中,我只需要运行stringinstring stringinstring "$string1" "$string2"
并检查返回值,例如
stringinstring "$str1" "$str2" && echo YES || echo NO
我喜欢sed。
substr="foo"
nonsub="$(echo "$string" | sed "s/$substr//")"
hassub=0 ; [ "$string" != "$nonsub" ] && hassub=1
编辑,逻辑:
使用sed从字符串中删除子串的实例
如果新字符串与旧字符串不同,则存在子字符串
所以这个问题有很多有用的解决方案 - 但最快/最少的资源?
使用此框架重复测试:
/usr/bin/time bash -c 'a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do TEST ; x=$(($x-1)); done'
每次更换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
(doContain在F. Houri的回答中)
而对于咯咯笑声:
echo $b|grep -q $a 12.68user 30.86system 3:42.40elapsed 19%CPU !ouch!
所以简单的替代选项可能会在扩展测试或案例中获胜。 这个箱子是便携式的。
管道出去100000 greps是可预见的痛苦! 关于无需使用外部实用程序的旧规则是成立的。
正如Paul在他的表现比较中提到的:
if echo "abcdefg" | grep -q "bcdef"; then
echo "String contains is true."
else
echo "String contains is not true."
fi
这是符合POSIX标准的,就像Marcus提供的'case'$ string'in'答案一样,但比case statement的答案稍微容易阅读。 另外请注意,这将比使用case语句慢得多,正如Paul指出的,不要在循环中使用它。
试试oobash它是一个用于bash 4的面向对象的字符串库。它支持德语变音符号。 它是用bash写的。 许多函数可用: -base64Decode
, -base64Encode
, -base64Decode
, -base64Encode
, -capitalize
, -charAt
, -concat
, -count
, -endsWith
, -equals
, -equalsIgnoreCase
, -reverse
, -hashCode
, -indexOf
, -isAlnum
, -isAlpha
, -isAlpha
, -isAscii
, -isDigit
, -isEmpty
, -isHexDigit
, -isLowerCase
, -isSpace
, -isPrintable
, -isUpperCase
, -isVisible
, -lastIndexOf
, -matches
, -replaceAll
, -replaceFirst
, -startsWith
, -substring
, -swapCase
, -toLowerCase
, -toString
, -toUpperCase
, -trim
和-zfill
。
看看包含的例子:
[Desktop]$ String a testXccc
[Desktop]$ a.contains tX
true
[Desktop]$ a.contains XtX
false
这个堆栈溢出的答案是唯一一个陷阱空间和破折号的字符:
# For null cmd arguments checking
to_check=' -t'
space_n_dash_chars=' -'
[[ $to_check == *"$space_n_dash_chars"* ]] && echo found
这也适用于:
if printf -- '%s' "$haystack" | egrep -q -- "$needle"
then
printf "Found needle in haystack"
fi
而负面测试是:
if ! printf -- '%s' "$haystack" | egrep -q -- "$needle"
then
echo "Did not find needle in haystack"
fi
我认为这种风格更具经典 - 不太依赖于Bash shell的特性。
--
参数是纯粹的POSIX偏执狂,用于防止类似于选项的输入字符串,例如--abc
或-a
。
注意:在严格的循环中,这个代码将比使用内部Bash shell特性慢得多,因为一个(或两个)单独的进程将通过管道创建并连接。
[[ $string == *foo* ]] && echo "It's there" || echo "Couldn't find"