문자열에 Bash의 하위 문자열이 포함되어 있는지 확인하는 방법

StackOverflow https://stackoverflow.com/questions/229551

  •  04-07-2019
  •  | 
  •  

문제

Bash에 문자열이 있습니다.

string="My string"

다른 문자열이 포함되어 있는지 어떻게 테스트 할 수 있습니까?

if [ $string ?? 'foo' ]; then
  echo "It's there!"
fi

어디에 ?? 내 알려지지 않은 연산자입니다. 에코를 사용합니까? grep?

if echo "$string" | grep 'foo'; then
  echo "It's there!"
fi

그것은 약간 서투른 것처럼 보입니다.

도움이 되었습니까?

해결책

당신이 사용할 수있는 마커스의 대답 (* 와일드 카드) 이중 괄호를 사용하는 경우 사례 명세서 외부에서도 다음과 같습니다.

string='My long string'
if [[ $string == *"My long"* ]]; then
  echo "It's there!"
fi

바늘 줄의 공간은 이중 인용문과 * 와일드 카드는 밖에 있어야합니다.

다른 팁

REGEX 접근법을 선호하는 경우 :

string='My string';

if [[ $string =~ "My" ]]
then
   echo "It's there!"
fi

IF 문을 사용하는 것에 대해서는 확실하지 않지만 사례 문을 사용하여 비슷한 효과를 얻을 수 있습니다.

case "$string" in 
  *foo*)
    # Do stuff
    ;;
esac

호환 답변

Bash 특정 기능을 사용하여 이미 많은 답변이 있으므로, 가난한 껍질 아래에서 작동하는 방법이 있습니다. :

[ -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

이것은 아래에서 테스트되었습니다 , , 그리고 (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이 요청한 바와 같이 여기에는 동일한 쉘에서 테스트 된 동일한 데모 버전이 있습니다.

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
}

그 다음에:

$ 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'.

간단한 기능

이것은 아래에서 테스트되었습니다 , 그리고 물론 :

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" ]; }

또는 제안한대로 Adrian Gü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

쉘 스크립팅은 언어가 적고 명령 모음이 더 많다는 것을 기억해야합니다. 본능적으로 당신은이 "언어"가 당신이 if a [ 또는 a [[. 두 가지 모두 성공 또는 실패를 나타내는 종료 상태를 반환하는 명령입니다 (다른 모든 명령과 마찬가지로). 그런 이유로 내가 사용합니다 grep, 그리고 [ 명령.

그냥 해:

if grep -q foo <<<"$string"; then
    echo "It's there"
fi

이제 당신이 생각하고 있습니다 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
#...

그만큼 -q 옵션은 리턴 코드 만 원하기 때문에 Grep이 아무것도 출력하지 않습니다. <<< 쉘이 다음 단어를 확장하고 명령에 대한 입력으로 사용합니다. << 여기서 문서 (이것이 표준인지, bashism인지 확실하지 않음).

허용 된 대답은 가장 좋지만,이를 수행하는 방법이 둘 이상이기 때문에 다음은 다음과 같습니다.

if [ "$string" != "${string/foo/}" ]; then
    echo "It's there!"
fi

${var/search/replace} ~이다 $var 첫 번째 인스턴스와 함께 search ~로 교체되다 replace, 발견되면 (변경되지 않습니다 $var). 교체하려고한다면 foo 아무것도없고 문자열이 바뀌었고 분명히 foo 발견.

따라서이 질문에는 유용한 솔루션이 많이 있지만 가장 빠르거나 가장 적은 리소스를 사용하는 것은 무엇입니까?

이 프레임을 사용한 반복 테스트 :

/usr/bin/time bash -c 'a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do TEST ; x=$(($x-1)); done'

매번 테스트 교체 :

[[ $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!

따라서 간단한 대체 옵션은 연장 테스트 또는 사례에 관계없이 우선합니다. 케이스는 휴대용입니다.

10 만 그레프로 배관하는 것은 예상되는 고통 스럽습니다! 필요없이 외부 유틸리티를 사용하는 것에 대한 오래된 규칙은 사실입니다.

이것은 또한 작동합니다 :

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.

참고 : 단단한 루프 에서이 코드는 많이 파이프를 통해 별도의 프로세스가 생성되고 연결되므로 내부 배쉬 쉘 기능을 사용하는 것보다 느리게.

이건 어때:

text="   <tag>bmnmn</tag>  "
if [[ "$text" =~ "<tag>" ]]; then
   echo "matched"
else
   echo "not matched"
fi

bash4+ 예. 참고 : 인용문을 사용하지 않으면 단어에 공백이 포함되어있을 때 문제가 발생합니다. 항상 Bash IMO에서 인용하십시오.

다음은 Bash4+의 몇 가지 예입니다.

예 1, 문자열에서 '예'를 확인하십시오 (케이스 둔감) :

    if [[ "${str,,}" == *"yes"* ]] ;then

예 2, 문자열에서 '예'를 확인하십시오 (Case Insensentitive) :

    if [[ "$(echo "$str" | tr '[:upper:]' '[:lower:]')" == *"yes"* ]] ;then

예 3, 문자열에서 '예'를 확인하십시오 (Case Sensitive) :

     if [[ "${str}" == *"yes"* ]] ;then

예 4, 문자열에서 '예'를 확인하십시오 (Case Sensitive) :

     if [[ "${str}" =~ "yes" ]] ;then

예 5, 정확한 일치 (케이스 민감성) :

     if [[ "${str}" == "yes" ]] ;then

예 6, 정확한 일치 (케이스 둔감) :

     if [[ "${str,,}" == "yes" ]] ;then

예 7, 정확한 일치 :

     if [ "$a" = "$b" ] ;then

예 8, WildCard Match .Ext (Case Insensitive) :

     if echo "$a" | egrep -iq "\.(mp[3-4]|txt|css|jpg|png)" ; then

즐겨.

Paul은 그의 성과 비교에서 언급했듯이 :

if echo "abcdefg" | grep -q "bcdef"; then
    echo "String contains is true."
else
    echo "String contains is not true."
fi

이것은 Marcus가 제공 한 '대답의'$ string "과 같은 Posix를 준수하지만 Case Statement 답변보다 읽기가 약간 쉽습니다. 또한 Paul이 지적했듯이 이것은 사례 진술을 사용하는 것보다 훨씬 느리게 진행됩니다.

이 스택 오버플로 답변 공간과 대시 숯을 가두는 유일한 사람이었습니다.

# For null cmd arguments checking   
to_check=' -t'
space_n_dash_chars=' -'
[[ $to_check == *"$space_n_dash_chars"* ]] && echo found

하나는:

[ $(expr $mystring : ".*${search}.*") -ne 0 ] && echo 'yes' ||  echo 'no'
[[ $string == *foo* ]] && echo "It's there" || echo "Couldn't find"

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

나는 SED를 좋아한다.

substr="foo"
nonsub="$(echo "$string" | sed "s/$substr//")"
hassub=0 ; [ "$string" != "$nonsub" ] && hassub=1

편집, 논리 :

  • sed를 사용하여 문자열에서 서브 스트링 인스턴스를 제거하십시오

  • 새 문자열이 이전 문자열과 다르면 하위 문자열이 존재합니다.

내 .bash_profile과 경로에 2 개의 bin dirs가 포함 된 경우 Grep을 어떻게 사용했는지, 추가하지 마십시오.

# .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

이 기능이 매우 자주 필요하다는 것을 알았으므로 내에서 집에서 만든 쉘 기능을 사용하고 있습니다. .bashrc 이처럼 기억하기 쉬운 이름으로 필요한만큼 자주 재사용 할 수 있습니다.

function stringinstring()
{
    case "$2" in 
       *"$1"*)
          return 0
       ;;
    esac   
    return 1
}

테스트하려면 $string1 (말하다, 알파벳)에 포함되어 있습니다 $string2 (말하다, 123ABCABC) 그냥 달리기 만하면됩니다 stringinstring "$string1" "$string2" 예를 들어 반환 값을 확인하십시오

stringinstring "$str1" "$str2"  &&  echo YES  ||  echo NO

OOBASH를 사용해보십시오. Bash 4를위한 OO 스타일의 문자열 라이브러리입니다. 독일 UMLAUTS를 지원합니다. Bash로 작성되었습니다. 많은 기능을 사용할 수 있습니다. -base64Decode, -base64Encode, -capitalize, -center, -charAt, -concat, -contains, -count, -endsWith, -equals, -equalsIgnoreCase, -reverse, -hashCode, -indexOf, -isAlnum, -isAlpha, -isAscii, -isDigit, -isEmpty, -isHexDigit, -isLowerCase, -isSpace, -isPrintable, -isUpperCase, -isVisible, -lastIndexOf, -length, -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      

Oobash는 Sourceforge.net에서 제공됩니다.

정확한 단어 일치 :

string='My long string'
exactSearch='long'

if grep -E -q "\b${exactSearch}\b" <<<${string} >/dev/null 2>&1
  then
    echo "It's there"
  fi

질문의 확장이 여기에서 답변되었습니다 https://stackoverflow.com/a/8811800/712666

이 솔루션은 특수 문자와 함께 작동합니다.

# 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*"

이 기능을 사용합니다 (하나의 종속성이 포함되어 있지 않지만 명백합니다). 아래에 표시된 테스트를 통과합니다. 함수가 값> 0을 반환하면 문자열이 발견되었습니다. 대신 1 또는 0을 쉽게 반환 할 수 있습니다.

function str_instr {
   # Return position of ```str``` within ```string```.
   # >>> str_instr "str" "string"
   # str: String to search for.
   # string: String to search.
   typeset str string x
   # Behavior here is not the same in bash vs ksh unless we escape special characters.
   str="$(str_escape_special_characters "${1}")"
   string="${2}"
   x="${string%%$str*}"
   if [[ "${x}" != "${string}" ]]; then
      echo "${#x} + 1" | bc -l
   else
      echo 0
   fi
}

function test_str_instr {
   str_instr "(" "'foo@host (dev,web)'" | assert_eq 11
   str_instr ")" "'foo@host (dev,web)'" | assert_eq 19
   str_instr "[" "'foo@host [dev,web]'" | assert_eq 11
   str_instr "]" "'foo@host [dev,web]'" | assert_eq 19
   str_instr "a" "abc" | assert_eq 1
   str_instr "z" "abc" | assert_eq 0
   str_instr "Eggs" "Green Eggs And Ham" | assert_eq 7
   str_instr "a" "" | assert_eq 0
   str_instr "" "" | assert_eq 0
   str_instr " " "Green Eggs" | assert_eq 6
   str_instr " " " Green "  | assert_eq 1
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top