shell 字串內含項目關聯性

來源:互聯網
上載者:User

# 方法1 —— 字元比較

#!/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

運行結果:

hello include he
hello not include lo

# 方法2 —— grep匹配

#!/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

運行結果:

hello world include he
hello world not include world 

#方法3 —— 由方法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



#方法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

運行結果:

include
hello world include he

#方法5 —— expr 命令

expr有模式比對功能,可以通過指定冒號選項計算字串中字元數,.* 即任何字元重複0次或多次

expr 計算字元數:

expr  "accounts.doc" : '.*'          # result : 12

expr 截取字串

expr "accounts.doc" : '\(.*\).doc'         # result : accounts

expr substr "hello world" 1 7              # result : hello w

expr index "hello world" w                 # result : 7

expr 截取數字

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

substr 和 index 配合使用:

expr substr "hello world" 1 $(expr index "hello world" w)            # result : hello w

#方法6 —— awk的index函數

awk 'BEGIN{info="this is hello world"; print index(info, "hello") ? "include" : "not include";}'            # result : include

awk 'BEGIN{info="this is hello world"; print index(info, "helo") ? "include" : "not include";}'             # result : not include

${var#...}                  
${var%...}
${var/.../...}

grep 精確匹配

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 "\<hell\>"      # result  :  hello    hellos   hell

1) 模糊比對; 2) 單詞匹配; 3) 正則域匹配; 推薦方式3)

完整樣本:

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

運行結果:

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

參考推薦:

shell 判斷字串是否存在內含項目關聯性

Shell expr的用法

awk 執行個體

linux awk 內建函數詳細介紹(推薦)

Linux 之 shell 比較子

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.