shell 字符串包含关系

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

# 方法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" : '. .∗'         # 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 比较运算符


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值