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

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、文件测试...

    shell脚本初学基础

    Shell脚本中的数据类型包括数字和字符串。字符串可以用单引号或双引号包围。单引号内的内容原样输出,不解析变量;双引号内可以包含变量和转义字符。要获取字符串长度,可以使用`${#string}`。获取子字符串,使用`${...

    新版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...

    Linux从入门到实战学习教程-9Shell脚本编程基础.pptx

    - **关系运算符**:`-z`(字符串长度为零),`-n`(字符串长度不为零),`-f`(文件存在且为普通文件),`-d`(文件存在且为目录)等。 - **布尔运算符**:`!`(非),`&&`(与),`||`(或)。 - **逻辑运算符**:`...

    藏经阁-Shell 脚本速查手册-29.pdf

    2. **字符串处理**:Bash中的字符串可以用单引号或双引号包围。单引号内的内容会原样输出,不支持变量解析,而双引号内则可以包含变量并支持转义字符。 3. **参数传递**:Bash脚本可以接收参数,参数按顺序编号,从...

    Python程序设计-试题库.docx

    知识点:可以使用 运算符测试集合的包含关系。 16. del 命令既可以删除列表中的一个元素,也可以删除整个列表。 知识点:del 命令可以删除列表中的元素或整个列表。 17. 表达式 int('123', 16) 的值为 291。 ...

    C++面试宝典【包含C++常考、计算机网络常考、操作系统常考、数据结构、Linux、算法、数据库、HR面全程面试知识点】

    C++支持多种数据类型,如整数、浮点数、字符、字符串、布尔值等。了解不同的数据类型及其存储方式是C++基础知识的重要组成部分。 2. 指针和引用 指针和引用是C++中重要的概念。指针是指向内存中某个位置的变量,而...

    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多任务环境下...

    BitBake User Manual.pdf

    1. 基本变量:字符串、数字、布尔等。 2. 继承变量:从父类继承的变量。 3. 函数变量:函数返回的变量。 Conditional Syntax BitBake 支持条件语法,用于定义条件元数据和键扩展。条件语法包括: 1. 条件元数据:...

    大数据实验 实验三:熟悉常用的HBase操作

    `TableDescriptorBuilder`和`ColumnFamilyDescriptorBuilder`用于构建表描述符和列族描述符,`Bytes.toBytes`用于将字符串转换为字节数组以适应HBase的字节操作。 四、HBase操作实例 1. 创建表:根据关系型数据库...

    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

Global site tag (gtag.js) - Google Analytics