Shell判断一段文本里是否包含指定文本

2019年11月24日

关键词:shell判断文本内容 shell是否包含关键字

在Shell代码编辑的时候,遇到文本判断的时候我们遇常会用到文本判断,假设,我们给出了指定的关键字,想在某个文件里去搜索看是否可以找到,这样就好进行下一步的判断。在这里我们可以使用grep来进行查找。

grep (缩写来自Globally search a Regular Expression and Print)是一种强大的文本搜索工具,它能使用特定模式匹配(包括正则表达式)搜索文本,并默认输出匹配行。Unix的grep家族包括grep、egrep和fgrep。

egrep和fgrep的命令只跟grep有很小不同。egrep和fgrep都是grep的扩展,支持更多的re元字符,fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。linux使用GNU版本的grep。它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能

在这里以我之前写的一个Sitemap的脚本来演示,相关内容链接:https://www.myzhenai.com.cn/post/2871.html

  if [ ! -f "${phat}" ]; then
    echo -e "${xml}\n${st}" > ${phat}
  fi
  if [ ! -n "${1}" ]; then
    echo "error! Please enter the domain name you want to enumerate"
  else
    sed -i "/<\/urlset>/d" ${phat}
    page=$(curl -s "${1}/archives.html"|grep -e "href="|awk -F "href=\"" '{ print $2 }'|awk -F "\"" '{ print $1 }')
#echo ${page} >> di.html
    arr=(${page})
      for i in ${arr[@]}
      do
          loc=$(echo ${i}|grep -E 'admin|.css|.js|.ico|Mirages|java|browse|xmlrpc')
         if [[ ${i} != ${loc} ]]; then
                 #在这里对所获取的页面里的网址与sitemap.xml里的网址进行对比,如果没有再写入,如果存在就抛弃
           pan=$(cat ${phat}|grep "${i}")
           if [[ ${pan} == "" ]]; then
           echo -e "${ul}\n${lo}${i}${lc}\n${mod}${ni}${tmod}\n${req}${dai}${freq}\n${pri}\n${ur}" >> ${phat}
           fi
         fi
      done
      echo "${se}" >> ${phat}
  fi

 

page=$(curl -s "${1}/archives.html"|grep -e "href="|awk -F "href=\"" '{ print $2 }'|awk -F "\"" '{ print $1 }')
 loc=$(echo ${i}|grep -E 'admin|.css|.js|.ico|Mirages|java|browse|xmlrpc')

 
以上这两行就是对一段文本进行查找,第一行是读取一个网页源码,找出所有url链接,第二行是识别获取到的链接里是否包含给出的关键字,grep -E 其实代替的就是egrep,是多项选择。我们可以看一看grep的详细用法。如果看不懂可以加上参数搜索一下网上的教程。

[RucLinux@localhost ~]$ grep --help
用法: grep [选项]... PATTERN [FILE]...
在每个 FILE 或是标准输入中查找 PATTERN。
默认的 PATTERN 是一个基本正则表达式(缩写为 BRE)。
例如: grep -i 'hello world' menu.h main.c

正则表达式选择与解释:
  -E, --extended-regexp     PATTERN 是一个可扩展的正则表达式(缩写为 ERE)
  -F, --fixed-strings       PATTERN 是一组由断行符分隔的定长字符串。
  -G, --basic-regexp        PATTERN 是一个基本正则表达式(缩写为 BRE)
  -P, --perl-regexp         PATTERN 是一个 Perl 正则表达式
  -e, --regexp=PATTERN      用 PATTERN 来进行匹配操作
  -f, --file=FILE           从 FILE 中取得 PATTERN
  -i, --ignore-case         忽略大小写
  -w, --word-regexp         强制 PATTERN 仅完全匹配字词
  -x, --line-regexp         强制 PATTERN 仅完全匹配一行
  -z, --null-data           一个 0 字节的数据行,但不是空行

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             display version information and exit
      --help                display this help text and exit

输出控制:
  -m, --max-count=NUM       NUM 次匹配后停止
  -b, --byte-offset         输出的同时打印字节偏移
  -n, --line-number         输出的同时打印行号
      --line-buffered       每行输出清空
  -H, --with-filename       为每一匹配项打印文件名
  -h, --no-filename         输出时不显示文件名前缀
      --label=LABEL         将LABEL 作为标准输入文件名前缀
  -o, --only-matching       show only the part of a line matching PATTERN
  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is 'binary', 'text', or 'without-match'
  -a, --text                equivalent to --binary-files=text
  -I                        equivalent to --binary-files=without-match
  -d, --directories=ACTION  how to handle directories;
                            ACTION is 'read', 'recurse', or 'skip'
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                            ACTION is 'read' or 'skip'
  -r, --recursive           like --directories=recurse
  -R, --dereference-recursive
                            likewise, but follow all symlinks
      --include=FILE_PATTERN
                            search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN
                            skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN directories that match PATTERN will be skipped.
  -L, --files-without-match print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

文件控制:
  -B, --before-context=NUM  打印以文本起始的NUM 行
  -A, --after-context=NUM   打印以文本结尾的NUM 行
  -C, --context=NUM         打印输出文本NUM 行
  -NUM                      same as --context=NUM
      --group-separator=SEP use SEP as a group separator
      --no-group-separator  use empty string as a group separator
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is 'always', 'never', or 'auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS/Windows)
  -u, --unix-byte-offsets   report offsets as if CRs were not there
                            (MSDOS/Windows)

‘egrep’即‘grep -E’。‘fgrep’即‘grep -F’。
直接使用‘egrep’或是‘fgrep’均已不可行了。
若FILE 为 -,将读取标准输入。不带FILE,读取当前目录,除非命令行中指定了-r 选项。
如果少于两个FILE 参数,就要默认使用-h 参数。
如果有任意行被匹配,那退出状态为 0,否则为 1;
如果有错误产生,且未指定 -q 参数,那退出状态为 2。

请将错误报告给: bug-grep@gnu.org
GNU Grep 主页: 
GNU 软件的通用帮助: 

 


sicnature ---------------------------------------------------------------------
Your current IP address is: 54.226.126.38
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source http://myzhenai.com.cn/post/2984.html

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注