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

这篇文章主要给大家介绍了关于用Shell判断字符串包含关系的几种方法,其中包括利用grep查找、利用字符串运算符、利用通配符、利用case in 语句以及利用替换等方法,每个方法都给出了详细的示例代码与介绍,有需要的朋友们可以参考参考借鉴,下面来一起看看吧。

前言

现在每次分析网站日志的时候都需要判断百度蜘蛛是不是真实的蜘蛛,nslookup之后需要判断结果中是否包含“baidu”字符串

以下给出一些shell中判断字符串包含的方法,来源程序员问答网站 stackoverflow 以及segmentfault。

方法一:利用grep查找
strA="long string"
strB="string"
result=$(echo $strA | grep "${strB}")
if [[ "$result" != "" ]]
then
echo "包含"
else
echo "不包含"
fi
先打印长字符串,然后在长字符串中 grep 查找要搜索的字符串,用变量result记录结果

如果结果不为空,说明strA包含strB。如果结果为空,说明不包含。

这个方法充分利用了grep 的特性,最为简洁。

方法二:利用字符串运算符
strA="helloworld"
strB="low"
if [[ $strA =~ $strB ]]
then
echo "包含"
else
echo "不包含"
fi
利用字符串运算符 =~ 直接判断strA是否包含strB。(这不是比第一个方法还要简洁吗!)

方法三:利用通配符
A="helloworld"
B="low"
if [[ $A == *$B* ]]
then
echo "包含"
else
echo "不包含"
fi
这个也很easy,用通配符*号代理strA中非strB的部分,如果结果相等说明包含,反之不包含。

方法四:利用case in 语句
thisString="1 2 3 4 5" # 源字符串
searchString="1 2" # 搜索字符串
case $thisString in
*"$searchString"*) echo Enemy Spot ;;
*) echo nope ;;
esa
这个就比较复杂了,case in 我还没有接触到,不过既然有比较简单的方法何必如此

方法五:利用替换
STRING_A=$1
STRING_B=$2
if [[ ${STRING_A/${STRING_B}//} == $STRING_A ]]
then
## is not substring.
echo N
return 0
else
## is substring.
echo Y
return 1
fi
这个也挺复杂

总结

 

linux shell: 搜索字符串,剔除包含特定字符的行
搜索代码中出现CONFIG_BOOTARGS的行,但又不希望有#define和#undef,

暂时不知带如何直接用一个语句做到,因此用了如下三个语句.

find . -type f | xargs grep CONFIG_BOOTARGS > result.log; sed -i '/#define/d' result.log; sed -i '/#undef/d' result.log;

//找到含CONFIG_BOOTARGS的行

//剔除包含特定字符#define的行

//再剔除包含特定字符#undef的行

-------------------------

如何在 Linux shell 中找出所有包含指定文本的文件
目标:本文提供一些关于如何搜索出指定目录或整个文件系统中那些包含指定单词或字符串的文件。

难度:容易

约定:

# - 需要使用 root 权限来执行指定命令,可以直接使用 root 用户来执行也可以使用 sudo 命令
$ - 可以使用普通用户来执行指定命令
案例
非递归搜索包含指定字符串的文件
第一个例子让我们来搜索 /etc/ 目录下所有包含 stretch 字符串的文件,但不去搜索其中的子目录:

# grep -s stretch /etc/*
/etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
/etc/os-release:VERSION="9 (stretch)"
grep 的 -s 选项会在发现不存在或者不能读取的文件时隐藏报错信息。结果显示除了文件名之外,还有包含请求字符串的行也被一起输出了。

递归地搜索包含指定字符串的文件
上面案例中忽略了所有的子目录。所谓递归搜索就是指同时搜索所有的子目录。

下面的命令会在 /etc/ 及其子目录中搜索包含 stretch 字符串的文件:

# grep -R stretch /etc/*
/etc/apt/sources.list:# deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main
/etc/apt/sources.list:#deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main
/etc/apt/sources.list:deb http://ftp.au.debian.org/debian/ stretch main
/etc/apt/sources.list:deb-src http://ftp.au.debian.org/debian/ stretch main
/etc/apt/sources.list:deb http://security.debian.org/debian-security stretch/updates main
/etc/apt/sources.list:deb-src http://security.debian.org/debian-security stretch/updates main
/etc/dictionaries-common/words:backstretch
/etc/dictionaries-common/words:backstretch's
/etc/dictionaries-common/words:backstretches
/etc/dictionaries-common/words:stretchy
/etc/grub.d/00_header:background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"`
/etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
/etc/os-release:VERSION="9 (stretch)"
搜索所有包含特定单词的文件
上面 grep 命令的案例中列出的是所有包含字符串 stretch 的文件。也就是说包含 stretches , stretched 等内容的行也会被显示。 使用 grep 的 -w 选项会只显示包含特定单词的行:

# grep -Rw stretch /etc/*
/etc/apt/sources.list:# deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main
/etc/apt/sources.list:#deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main
/etc/apt/sources.list:deb http://ftp.au.debian.org/debian/ stretch main
/etc/apt/sources.list:deb-src http://ftp.au.debian.org/debian/ stretch main
/etc/apt/sources.list:deb http://security.debian.org/debian-security stretch/updates main
/etc/apt/sources.list:deb-src http://security.debian.org/debian-security stretch/updates main
/etc/dictionaries-common/words:stretch
/etc/dictionaries-common/words:stretch's
/etc/grub.d/00_header:background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"`
/etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
/etc/os-release:VERSION="9 (stretch)"
显示包含特定文本的文件名
上面的命令都会产生多余的输出。下一个案例则会递归地搜索 etc 目录中包含 stretch 的文件并只输出文件名:

# grep -Rl stretch /etc/*
/etc/apt/sources.list
/etc/dictionaries-common/words
/etc/grub.d/00_header
/etc/os-release
大小写不敏感的搜索
默认情况下搜索是大小写敏感的,也就是说当搜索字符串 stretch 时只会包含大小写一致内容的文件。

通过使用 grep 的 -i 选项,grep 命令还会列出所有包含 Stretch , STRETCH , StReTcH 等内容的文件,也就是说进行的是大小写不敏感的搜索。

# grep -Ril stretch /etc/*
/etc/apt/sources.list
/etc/dictionaries-common/default.hash
/etc/dictionaries-common/words
/etc/grub.d/00_header
/etc/os-release
搜索时包含/排除指定文件
grep 命令也可以只在指定文件中进行搜索。比如,我们可以只在配置文件(扩展名为.conf)中搜索指定的文本/字符串。 下面这个例子就会在 /etc 目录中搜索带字符串 bash 且所有扩展名为 .conf 的文件:

# grep -Ril bash /etc/*.conf
OR
# grep -Ril --include=\*.conf bash /etc/*
/etc/adduser.conf
类似的,也可以使用 --exclude 来排除特定的文件:

# grep -Ril --exclude=\*.conf bash /etc/*
/etc/alternatives/view
/etc/alternatives/vim
/etc/alternatives/vi
/etc/alternatives/vimdiff
/etc/alternatives/rvim
/etc/alternatives/ex
/etc/alternatives/rview
/etc/bash.bashrc
/etc/bash_completion.d/grub
/etc/cron.daily/apt-compat
/etc/cron.daily/exim4-base
/etc/dictionaries-common/default.hash
/etc/dictionaries-common/words
/etc/inputrc
/etc/passwd
/etc/passwd-
/etc/profile
/etc/shells
/etc/skel/.profile
/etc/skel/.bashrc
/etc/skel/.bash_logout
搜索时排除指定目录
跟文件一样,grep 也能在搜索时排除指定目录。 使用 --exclude-dir 选项就行。

下面这个例子会搜索 /etc 目录中搜有包含字符串 stretch 的文件,但不包括 /etc/grub.d 目录下的文件:

# grep --exclude-dir=/etc/grub.d -Rwl stretch /etc/*
/etc/apt/sources.list
/etc/dictionaries-common/words
/etc/os-release
显示包含搜索字符串的行号
-n 选项还会显示指定字符串所在行的行号:

# grep -Rni bash /etc/*.conf
/etc/adduser.conf:6:DSHELL=/bin/bash
寻找不包含指定字符串的文件
最后这个例子使用 -v 来列出所有不包含指定字符串的文件。

例如下面命令会搜索 /etc 目录中不包含 stretch 的所有文件:

# grep -Rlv stretch /etc/*
via: https://linuxconfig.org/how-to-find-all-files-with-a-specific-text-using-linux-shell