# Method 1-- Character comparison
#!/bin/bashstr1="hello"str2="he"str3="lo"if [ ${str1:0:2} = $str2 ]; then echo "$str1 include $str2"fiif [ ${str1:2:4} = $str3 ]then echo "$str1 include $str3"else echo "$str1 not include $str3"fi
Running result:
Hello include he
Hello not include Lo
# Method 2-- Grep match
#!/bin/bashstr1="hello world"str2="he"str3="world "echo "$str1" | grep -q "$str2"if [ $? -eq 0 ]; then echo "$str1 include $str2"fiecho "$str1" | grep -q "$str3"if [ $? -eq 0 ]; then echo "$str1 include $str3"else echo "$str1 not include $str3"fi
Running result:
Hello world include he
Hello world not include World
# Method 3-- Evolved from method 2
Echo "Hello World" | grep-Q "he" & Echo "include" | echo "not include" # result: Include
Echo "Hello World" | grep-Q "world" & Echo "include" | echo "not include" # result: Not include
# Method 4
#!/bin/bashstr1="hello world"str2="he"str3="world "[[ "${str1/$str2/}" != "$str2" ]] && echo "include" || echo "not include"[[ "${str1/$str2/}" != "$str2" ]]if [ $? -eq 0 ]; then echo "$str1 include $str2"fi
Running result:
Include
Hello world include he
# Method 5-- Expr command
Expr supports pattern matching. You can specify the colon option to calculate the number of characters in the string. * that is, any character is repeated 0 or multiple times.
Number of expr characters:
Expr "accounts.doc": '. *' # result: 12
Expr truncated string
Expr "accounts.doc": '\ (. * \ mongo.doc' # result: Accounts
Expr substr "Hello World" 1 7 # result: Hello W
Expr Index "Hello World" W # result: 7
Expr truncation number
Expr "string in 123 line": '. * In \ (. * \)' # result: 123 line
Expr "string in 123 line": '. * In \ (. * \) line' # result: 123
Expr "http: // 192.168.1.100/platform_example/branch/demo_platform is at revision 81": '. * at \ revision \ (. * \)' # result: 81
Use substr and index together:
Expr substr "Hello World" 1 $ (expr Index "Hello World" W) # result: Hello W
# Method 6-- Awk index function
Awk 'in in {info = "this is Hello World"; print index (Info, "hello ")? "Include": "Not include";} '# result: Include
Awk 'in in {info = "this is Hello World"; print index (Info, "HELO ")? "Include": "Not include";} '# result: Not include
$ {Var #...}
$ {Var % ...}
$ {Var /.../...}
Grep exact match
1) echo "Hello hellos hell" | grep hell # result: Hello hellos hell
2) echo "Hello hellos hell" | grep-W hell # result: Hello hellos hell
3) echo "Hello hellos hell" | grep "\
1) fuzzy match; 2) word match; 3) Regular domain match; recommended method 3)
Complete example:
Test.txt
Bird
Birds
Angrybird
Angrybirds
Angry Bird
Angry Birds
Angry Birds war
Grep. Sh
#!/bin/bashcat test.txtechoecho "grep bird test.txt..."grep birds test.txtechoecho "grep -w bird test.txt..."grep -w birds test.txtechoecho "grep "\<birds\>" test.txt..."grep "\<birds\>" test.txt
Running result:
Bird
Birds
Angrybird
Angrybirds
Angry Bird
Angry Birds
Angry Birds war
Grep bird test.txt...
Birds
Angrybirds
Angry Birds
Angry Birds war
Grep-W bird test.txt...
Birds
Angry Birds
Angry Birds war
Grep <birds> test.txt...
Birds
Angry Birds
Angry Birds war
Reference recommendations:
Shell determines whether a string contains a link
Usage of shell expr
Awk instance
Linux awk built-in functions (recommended)
Shell comparison operators in Linux