shell 字符串包含关系
2013-05-23 12:04
211 查看
# 方法1 ―― 字符比较[html]view plaincopyprint?#!/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匹配
[html]view plaincopyprint?#!/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 : includeecho "hello world" | grep -q "world " && echo "include" || echo "not include" # result : not include
#方法4
[html]view plaincopyprint?#!/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
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 : includeawk '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.txtbird
birds
angrybird
angrybirds
angry bird
angry birds
angry birds war
grep.sh
[html]view plaincopyprint?#!/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
原文链接:http://blog.csdn.net/sunboy_2050/article/details/8448871
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匹配
[html]view plaincopyprint?#!/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 : includeecho "hello world" | grep -q "world " && echo "include" || echo "not include" # result : not include
#方法4
[html]view plaincopyprint?#!/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
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 : includeawk '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.txtbird
birds
angrybird
angrybirds
angry bird
angry birds
angry birds war
grep.sh
[html]view plaincopyprint?#!/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
原文链接:http://blog.csdn.net/sunboy_2050/article/details/8448871
相关文章推荐
- shell 字符串包含关系
- shell 判断字符串是否存在包含关系
- shell 判断字符串是否存在包含关系
- Shell判断字符串包含关系的几种方法
- Shell判断字符串包含关系的几种方法
- Shell判断字符串包含关系的几种方法
- Shell判断字符串包含关系的几种方法
- Shell判断字符串包含关系的几种方法
- 用Shell判断字符串包含关系的方法小结
- shell判断字符串包含关系的三种方法
- 用 Shell 判断字符串包含关系的方法小结
- Shell判断字符串包含关系的几种方法
- shell 字符串包含关系
- 用Shell判断字符串包含关系的方法小结
- 用Shell判断字符串包含关系的方法小结
- shell下判断两个字符串是否有包含关系
- Shell判断字符串包含关系的方法小结
- bzoj1559(AC自动机+状压(字符串包含关系判断))
- Shell循环列出目录下所有文件或查找目录下包含指定字符串的文件
- 【脚本】shell 判断变量中是否包含某个字符串