`
nanjingjiangbiao_T
  • 浏览: 2605265 次
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

shell 字符串包含关系

 
阅读更多

# 方法1 —— 字符比较

#!/bin/bash

str1="hello"
str2="he"
str3="lo"

if [ ${str1:0:2} = $str2 ]; then
    echo "$str1 include $str2"
fi

if [ ${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/bash

str1="hello world"
str2="he"
str3="world "

echo "$str1" | grep -q "$str2"
if [ $? -eq 0 ]; then
    echo "$str1 include $str2"
fi


echo "$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/bash

str1="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


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/bash

cat test.txt

echo
echo "grep bird test.txt..."
grep birds test.txt

echo
echo "grep -w bird test.txt..."
grep -w birds test.txt

echo
echo "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 比较运算符


分享到:
评论

相关推荐

    用Shell判断字符串包含关系的方法小结

    以下给出一些shell中判断字符串包含的方法,来源程序员问答网站 stackoverflow 以及segmentfault。 方法一:利用grep查找 strA=long string strB=string result=$(echo $strA | grep ${strB}) if [[ $result != ]] ...

    Shell脚本计算字符串长度和判断字符串为空小技巧

    一些需要注意的脚本问题 计算字符串长度可用的三种方法: 代码如下: echo “$str”|awk ‘{print length($0)}’ expr length “$str” ... 您可能感兴趣的文章:用Shell判断字符串包含关系的方法小结Shel

    shell脚本学习手册

    5、shell字符串 6 6、Shell数组 7 7、Shell注释 9 Shell传递参数 9 1、实例 9 Shell数组 11 Shell运算符 12 1、算术运算符 13 2、关系运算符 14 3、布尔运算符 15 4、逻辑运算符 15 5、字符串运算符 16 6、文件测试...

    新版Linux Shell编程实训(全)20170518.docx

    5.1.6 字符串运算符 104 5.1.7 文件测试运算符 106 任务5.2 Shell案例:计算器 109 练习(每题25分,共计100分) 111 项目六 Shell命令输出 112 [学习目标] 112 任务6.1 Shell echo命令 113 任务6.2 Shell printf...

    NoSQL云数据库mongoDB的C#示例(vs2005)

    键用于唯一标识一个文档,为字符串类型,而值则可以是各中复杂的文件类型。我们称这种存储形式为BSON(Binary Serialized dOcument Format)。  MongoDB服务端可运行在Linux、Windows或OS X平台,支持32位和64位...

    UNIX操作系统教程 张红光

    2.1.2用户的注册与注销11 2.1.3账户的管理12 2.1.4用户口令的管理12 2.1.5...shell程序的调试方法86 5.10本章小结87 习题88 第6章UNIX系统编程基础89 6.1程序设计环境89 6.1.1理想中的程序设计环境89 6.1.2多任务环境下...

    autoenv:基于目录的环境。 [作者@ ken-reitz]

    启用后(将AUTOENV_ENABLE_LEAVE设置为非空字符串),如果目录包含.env.leave文件,则在离开目录时将自动执行该文件。 这非常适合... 自动激活虚拟环境 自动停用虚拟环境 项目特定的环境变量 赚百万 您也可以相互...

    UbuntuChina12

    7.4.2 字符串测试运算符 187 7.4.3 整数值测试运算符 188 7.4.4 逻辑运算符 189 7.5 命令行的解释执行过程 190 7.5.1 读取命令行 191 7.5.2 命令历史替换 191 7.5.3 别名替换 192 7.5.4 花括号扩展 192 7.5.5 波浪号...

    Ubuntu权威指南(2/2)

    7.4.2 字符串测试运算符 187 7.4.3 整数值测试运算符 188 7.4.4 逻辑运算符 189 7.5 命令行的解释执行过程 190 7.5.1 读取命令行 191 7.5.2 命令历史替换 191 7.5.3 别名替换 192 7.5.4 花括号扩展 192 7.5.5 波浪号...

    Ubuntu权威指南(1/2)

    7.4.2 字符串测试运算符 187 7.4.3 整数值测试运算符 188 7.4.4 逻辑运算符 189 7.5 命令行的解释执行过程 190 7.5.1 读取命令行 191 7.5.2 命令历史替换 191 7.5.3 别名替换 192 7.5.4 花括号扩展 192 7.5.5 波浪号...

    bitnami-docker-redis-cluster

    它通常被称为数据结构服务器,因为键可以包含字符串,哈希,列表,集合,排序集合,位图和超级日志。 TL; DR $ docker run --name redis-cluster -e ALLOW_EMPTY_PASSWORD=yes bitnami/redis-cluster:latest ...

    shadergraph:用于ClojureScript的WebGLGLSL着色器库和依赖项框架

    可选的基本着色器缩小器(无名称修改) GLSL源代码可以指定为字符串或从文件/资源​​中读取已从文献和其他项目中收集了一些着色器功能,并将其部分重构为纯函数。 莱宁根坐标 最新稳定[thi.ng

    Perl 实例精解(第三版).pdf

    5.3.11 特殊字符串运算符和函数 5.3.12 生成随机数 5.3.13 rand/srand函数 练习4 运算符 第6章 条件 6.1 控制结构,块和复合语句 6.2 决策--条件结构 6.2.1 if和unless语句 6.2.2 unless语句...

    新版Android开发教程.rar

    ----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...

    精通Windows.API-函数、接口、编程实例.pdf

    15.2.5 字符串表 469 15.3 安装程序setup.exe的编号 469 15.4 使用msi文件进行安装 472 15.4.1 Windows Installer Service 472 15.4.2 msi文件的创建与修改工具orca.exe 474 15.4.3 准备工作 475 15.4.4...

    最全的oracle常用命令大全.txt

    查看名称包含log字符的表 SQL&gt;select object_name,object_id from user_objects where instr(object_name,'LOG')&gt;0; 查看某表的创建时间 SQL&gt;select object_name,created from user_objects where object_name...

    UNIX高级编程 计算机科学丛书

    另外,标准C库提供了大量C程序广泛使用的函数(格式化输入变量的值、比较两个字符串等)。 系统调用和库函数系统上由Unix程序员手册中的第2,3部分说明。本书不是这些内容的重复。该手册没有给出实例,也不说明...

    嵌入式Linux C编程入门(第2版) PPT

    6.1.2 字符串 172 6.1.3 二维数组 174 6.2 指针 175 6.2.1 指针的概念 175 6.2.2 指针变量的操作 177 6.2.3 指针和数组 184 6.2.4 指针高级议题 191 6.3 结构体与联合 196 6.3.1 结构体 ...

Global site tag (gtag.js) - Google Analytics