初始实验脚本如下:

#!/bin/bashtmp_license="GPL-2"
concluded_license="LGPL-2.1"echo "1111111111111111111111111111111111111"
echo $concluded_license | grep "$tmp_license"echo "2222222222222222222222222222222222222"
`echo $concluded_license | grep "$tmp_license"`

运行以上脚本会出现如下结果:

./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
LGPL-2.1
2222222222222222222222222222222222222
./test_subscript/full_match_test.sh: 行 11: LGPL-2.1:未找到命令

此时修改脚本,加入ret=,修改后的脚本如下:

#!/bin/bashtmp_license="GPL-2"
concluded_license="LGPL-2.1"echo "1111111111111111111111111111111111111"
echo $concluded_license | grep "$tmp_license"echo "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep "$tmp_license"`
echo "$ret"

运行修改后的脚本结果如下:

./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
LGPL-2.1
2222222222222222222222222222222222222
LGPL-2.1

可见,在shell中不能直接调用``,必须有变量来接收才可以。

另外可以看到,"LGPL-2.1"中是包含"GPL-2"的,如果想要收集所有的协议,则会产生问题,正常应该是"LGPL-2.1"和"GPL-2",但是按照以上的方法,"GPL-2"会“湮灭”在"LGPL-2,1"中,从而导致只有"LGPL-2.1"一个协议被收集到。

如何解决?继续修改脚本,加入Fx选项实现完全匹配,已解决这一问题。脚本如下:

#!/bin/bashtmp_license="GPL-2"
concluded_license="LGPL-2.1"echo "1111111111111111111111111111111111111"
echo $concluded_license | grep -Fx "$tmp_license"echo "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep -Fx "$tmp_license"`
echo "$ret"

脚本运行结果如下:

$ ./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
2222222222222222222222222222222222222

这里有一个“小手段”要注意:grep本来是用于在文件中查找字符串。那么如何将其用在字符串中查找字符串?使用echo <string1> | grep <string2>的方式即可实现。

可以看到,加入-Fx选项后,会进行全字匹配,而不会进行误判断了。

完整的实验脚本如下:

#!/bin/bashtmp_license="GPL-2"
concluded_license="LGPL-2.1"echo "1111111111111111111111111111111111111"
if [ `echo $concluded_license | grep "$tmp_license"` ]; thenecho "exist"
elseecho "not exist"
fiecho "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep -Fx "$tmp_license"`
echo "$ret"

在此基础上在进行进一步实验,这次将concluded_license进行修改,脚本如下:

#!/bin/bashtmp_license="GPL-2"
concluded_license="LGPL-2.1 or GPL-2"echo "1111111111111111111111111111111111111"
if [ `echo $concluded_license | grep "$tmp_license"` ]; thenecho "exist"
elseecho "not exist"
fiecho "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep -Fx "$tmp_license"`
echo "$ret"

此时运行脚本,会出现以下错误:

$ ./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
./test_subscript/full_match_test.sh: 第 27 行: [: or:需要二元表达式
not exist
2222222222222222222222222222222222222

解决方法是先用一个变量接收一下,也就是说不直接将echo带入到if中,再进行判断。修改后的脚本如下所示:

#!/bin/bashtmp_license="GPL-2"
concluded_license="LGPL-2.1 or GPL-2"echo "1111111111111111111111111111111111111"
ret=`echo "$concluded_license" | grep "$tmp_license"`
#echo "ret: $ret"
if [ -n "$ret" ]; then
#if [ `echo $concluded_license | grep "$tmp_license"` ]; thenecho "exist"
elseecho "not exist"
fiecho "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep -Fx "$tmp_license"`
echo "$ret"

运行脚本,结果如下所示:

$ ./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
not exist
2222222222222222222222222222222222222

虽然脚本错误问题解决了,但是逻辑上出现了新问题。这次"LGPL-2.1 or GPL-2中"明显已经包含了"GPL-2",应该能够搜索到,但是反倒找不到了,为什么呢?原因是-Fx实现了完全匹配,条件太强了,必须完全相同,差一点都不行。我们只需要精确匹配就可以了,将-Fx参数改为-w,脚本如下:

#!/bin/bashtmp_license="GPL-2"
concluded_license="LGPL-2.1 or GPL-2"echo "1111111111111111111111111111111111111"
ret=`echo "$concluded_license" | grep -w "$tmp_license"`
#echo "ret: $ret"
if [ -n "$ret" ]; then
#if [ `echo $concluded_license | grep "$tmp_license"` ]; thenecho "exist"
elseecho "not exist"
fiecho "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep -Fx "$tmp_license"`
echo "$ret"

运行结果如下:

$ ./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
exist
2222222222222222222222222222222222222

此时还不算完,还要确保-w参数能够在concluded_license只为"LGPL-2.1"时,不会“湮灭"GPL",修改脚本,恢复concluded_license为老的值,修改后脚本如下所示:

#!/bin/bashtmp_license="GPL-2"
concluded_license="LGPL-2.1"echo "1111111111111111111111111111111111111"
ret=`echo "$concluded_license" | grep -w "$tmp_license"`
#echo "ret: $ret"
if [ -n "$ret" ]; then
#if [ `echo $concluded_license | grep "$tmp_license"` ]; thenecho "exist"
elseecho "not exist"
fiecho "2222222222222222222222222222222222222"
ret=`echo $concluded_license | grep -Fx "$tmp_license"`
echo "$ret"

脚本运行结果如下:

$ ./test_subscript/full_match_test.sh ./sources/glibc-2.35
1111111111111111111111111111111111111
not exist
2222222222222222222222222222222222222

可以看到,使用-w参数进行grep,不会发生“湮灭”的情况,同时也不会过度严格,将真正包含的文本认为不存在。

grep完全匹配及精确匹配实验相关推荐

  1. Linux系列:grep模糊匹配、精确匹配、正则表达式

    模糊匹配 grep "abc" 精确匹配 grep -w "abc" 忽略大小写 grep -i  "abc" 正则表达式(a-z开头) g ...

  2. 广义精确匹配-Coarsened Exact Matching (CEM)

    严格来说,即使发表的论文,协变量在匹配后也不一定更加平衡,往往是某些变量的平衡性得到提升,而另一部分变量的平衡性有所下降.因此,我们介绍一种无需检查协变量平衡性.模型依赖度更低的匹配方法 --Coar ...

  3. grep精确匹配与完全匹配

    [root@testdb2 ~]# grep write_enable=YES /etc/vsftpd/vsftpd.conf write_enable=YES #anon_mkdir_write_e ...

  4. grep精确匹配--w参数的使用

    关于grep的精确匹配的方法 file的内容: 483 aa 48 484 如果 grep '48' file,结果是三行全部都被显示 如果想精确匹配48的那一行 grep -w '48' file  ...

  5. Linux命令grep实现精确匹配

    Linux命令grep实现精确匹配 举例: 精确过滤含abc的行 用grep –w "abc" 或者是grep "\<abc\>"都可以实现 -w, ...

  6. 使用grep精确匹配一个单词

    使用grep精确匹配一个单词 2012-05-24 15:35:09 标签:的 abc 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http ...

  7. 字符串多模式精确匹配(脏字/敏感词汇搜索算法)——TTMP算法 之理论(转)...

    什么是TTMP算法?不好意思,我发布这篇文章之前,估摸是没有其他地方能找着该算法的,因为那是俺生造的.  TTMP是啥意思呢?是Terminator Triggered Multi-Pattern 的 ...

  8. 模糊匹配 读音_onenote搜索机制详解②:两种搜索模式,模糊与精确匹配

    先从纯文本搜索讲起,这是最基本也是最重要的. 从这篇开始,以及接下来连续几篇文章,都会介绍搜索的基础功能.注意,这几篇文章中谈论的都是基本的.正常的搜索功能,暂时不考虑Bug等因素. 在很多软件(例如 ...

  9. EXCEL中VLOOKUP做精确匹配匹配后日期显示为00-Jan-00,教你如何解决

    前言 博文分为两个部分来解决VLOOKUP做精确匹配匹配后日期为空显示为1/0/1900的问题 VLOOKUP匹配问题 VLOOKUP函数使用方法 VLOOKUP函数可以用来核对数据,多个表格之间快速 ...

最新文章

  1. python 二维 排序
  2. mysql 硬盘空间不够_mysql磁盘空间不足的查排
  3. kylin备份元数据(用于清除不用的数据方式)
  4. shell中函数返回值
  5. left join on用法_MySQL 多表查询 quot;Joinquot;+“case when”语句总结
  6. mapreduce之partition分区
  7. 算法:前K个最大的元素
  8. sentinel 不显示项目_Sentinel+Nacos实现资源流控、降级、热点、授权
  9. 第二次作业+105032014098
  10. Javascript实现导出word - jquery jquery.wordexport.js 实现导出word
  11. 7-12 两个数的简单计算器 (10 分)
  12. linux下使用wget下载jdk
  13. Servlet 中的out.print()与out.writer()的区别
  14. 简单计算器 -python
  15. Atitit.收银机小票打印功能的设计  java php c#.net版本
  16. 清华大学计算机学教授招,清华大学计算机系网络所刘斌教授招收2015年入学博士生...
  17. RTP/RTCP/RTSP/SIP/SDP 关系
  18. 网页设计_导航条_下拉菜单
  19. css3动画停留在最后一帧
  20. S7-PLCSIM Advance ,解决 Error Code: -30,LicenseNotFound问题解决和期限21天限制的问题,对版本V1.0 V2.0 V3.0有效

热门文章

  1. 2022年各省春晚收视率如何?地图分析帮你清楚展现
  2. Checkstyle检查规则
  3. DVWA通关攻略零到一【全】
  4. 其它服务器(ThinkPHP5)与Discuz3.3自带的UCenter实现同步(一) - 通信成功
  5. Python Selenium 破解极验(GeeTest)滑动验证
  6. activiti flowable 开源工作流引擎项目整合开发实施实践总结
  7. 平板DR按其探测材料分类
  8. 约瑟夫问题(算法设计与C代码实现)
  9. 2022京东618预售定金怎么退?京东618定金能退吗?
  10. 网络工程师--网络规划和设计案例分析(2)