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

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

    解析使用substr截取UTF-8中文字符串出现乱码的问题

    在处理中文字符串时,理解字符编码和字节序列之间的关系至关重要,以避免出现乱码问题。 总之,处理UTF-8编码的中文字符串时,必须考虑到字符编码的特点,使用适当的方法来计算字符串长度和进行截取。`mb_strlen`和...

    shell编程.pptx

    在Shell中,字符串可以用单引号或双引号包围。单引号内的内容会原样输出,不进行变量替换,而双引号内则允许变量和转义字符。字符串操作包括拼接、获取长度和截取子字符串。例如,`echo ${#string}`可以输出字符串的...

    Shell编程课件

    - `字符串1=字符串2`:当两字符串相等时为真。 - `字符串1!=字符串2`:当两字符串不等时为真。 - `-n 字符串`:当字符串非空时为真。 - `-z 字符串`:当字符串为空时为真。 ##### 7. **整数关系测试** - `-eq`:两...

    shell.docx

    接着,我们讨论Shell字符串。字符串可以包含字母、数字和其他字符。单引号和双引号在字符串处理中有不同的作用: 1. 单引号字符串不会解析内部的变量,例如`'hello $world'`会原样输出`hello $world`。 2. 双引号...

    shell脚本初学基础

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

    Shell 十三问(SHELL 13Q)

    - **单引号**:保护字符串不被解析,即在单引号内的任何内容都被视为字面量,不会进行变量替换或命令替换。例如:`echo 'Hello, $USER'` 会输出`Hello, $USER`。 #### 五、`var=value`与`export`的区别 - **局部...

    shell程序设计的PPT

    赋值时,等号两边不能有空格,如果字符串包含空格,应使用引号将其括起来。例如: ``` Count=5 string="Helloworld!" ``` 读取变量值使用`$`符号,可以通过`echo`命令显示变量内容: ``` echo $Count ``` 若需要通过...

    shell脚本编程.pdf

    Shell脚本中的运算符主要包含整数测试运算、字符串测试运算、文件测试运算和逻辑运算等。整数测试可以判断两个整数的相等、不等、大于、大于等于、小于或小于等于关系。字符串测试可以进行空字符串测试或模式匹配。...

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

    5. **Shell运算符**:涵盖了Shell的各种运算符,包括算术、关系、布尔、逻辑和字符串运算符,以及文件测试运算符,并通过一个简单的计算器案例来实践这些运算符。 6. **Shell命令输出**:讲解了echo和printf命令...

    Shell入门.zip

    - `07运算符_字符串运算符.sh`:涉及到字符串的连接(`+`)、比较(`=`, `!=`, `-z`, `-n`)等操作。 3. **条件判断**:`10条件判断.sh`可能包含了`if-else`结构、`case`语句等,它们用于根据不同的条件执行不同的...

    Shell十三问.pdf

    - **`$@`**:保存命令行参数列表中的每个参数作为一个单独的字符串,即使参数中包含空格也不会合并。 - **`$*`**:保存所有命令行参数为一个字符串,如果参数之间有空格,会被合并成一个字符串。 #### 十、逻辑操作...

    学习shell的13个难点

    3. Echo命令的使用:Echo是Shell中用来显示指定字符串或变量值的命令。比如“echo hello”就会在命令行输出“hello”。echo命令也可以用于显示变量的值,例如“echo $PATH”会显示环境变量PATH的值。Echo命令是Shell...

    C语言实现的一个shell程序源码以及PDF版报告

    解析命令行字符串涉及到字符串处理技巧,如使用`strtok()`函数分隔命令和参数。执行命令时,可能需要利用`fork()`创建子进程,再用`execve()`来执行目标程序。 接下来是`builtin_command.c`,这个文件实现了shell...

    Shell开发教程.docx

    Shell字符串 **字符串操作**: - 字符串连接:`str1="$var1 $var2"` - 子串提取:`substring="${string:position:length}"` - 字符串替换:`new_str="${old_str//pattern/replacement}"` #### 12. Shell数组:...

Global site tag (gtag.js) - Google Analytics