A brief summary of the method of using Shell to judge string inclusion relationship _linux Shell

Source: Internet
Author: User

Objective

Now every time you analyze the site log, you need to judge Baidu Spider is not the real spider, nslookup after the need to determine whether the results include "Baidu" string

Here are some of the methods that are included in the judgment string in the shell, source programmer question and answer website StackOverflow and Segmentfault.

Method one: using grep to find

Stra= "Long string"
strb= "string"
result=$ (echo $strA | grep "${STRB}")
if [["$result"!= ""]]
Then
  echo "contains"
else
  Echo does not contain "
fi"

Print a long string, then grep in a long string to find the string to search for and record results with variable result

If the result is not empty, the stra contains STRB. If the result is empty, the description is not included.

This method leverages the grep's characteristics and is the most concise.

Method two: Using string operators

Stra= "HelloWorld"
strb= "Low"
if [[$strA =~ $strB]]
then
  echo contains "
else
  echo" does not contain "
fi

Use string operator =~ to directly determine whether Stra contains STRB. (This is not more concise than the first method!) )

Method Three: Using wildcard characters

A= "HelloWorld"
b= "Low"
if [[$A = = * $B *]]
then
  echo "contains"
else
  echo "does not contain"
fi

This is also easy, with the wildcard * Number agent stra parts of STRB, if the result is equal to the description contained, otherwise not included.

Method Four: Using the case in statement

Thisstring= "1 2 3 4 5" # source string
searchstring= "1 2" # search string case
$thisString in 
  * "$searchString" *) echo Enemy Spot;;
  *) echo nope;;
Esa

This is more complicated, the case in I have not yet contacted, but since there is a relatively simple way why so

Method V: Using replacement

String_a=$1
string_b=$2
if [[${string_a/${string_b}//} = = $STRING _a]]
  then
    # # is not substring.< C23/>echo N return
    0
  Else
    # # is substring.
    echo Y return
    1
  fi

This is complicated.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.