如何在ksh中grep精确匹配字符串中的点 [英] how to grep exact match of a string with dots in it in a ksh

查看:397
本文介绍了如何在ksh中grep精确匹配字符串中的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:我无法获得完全匹配的字符串作为输出,同时尝试grep找到包含点的字符串。 grepAPPLICATION.REFERENCE.LOCAL< FILENAME>



我甚至试过:

grep -F'APPLICATION.REFERENCE.LOCAL'< FILENAME> 它也显示类似匹配的行,如 APPLICATION.REFERENCE.LOCAL1 。



请帮助我。

解决方案

由于您使用 -F 来将模式解释为固定字符串,因此您需要提供 -x 选项完全匹配整行。

  grep -Fx'APPLICATION。 REFERENCE.LOCAL'filename 

man grep

  -F,--fixed-strings 
将PATTERN解释为由$ b分隔的固定字符串列表$ b换行符,任何whi ch将被匹配。 (-F由
POSIX指定。)

-x,--line-regexp
只选择那些与整行完全匹配的匹配项。
(-x由POSIX指定)。

示例:

  $ cat file 
APPLICATION.REFERENCE.LOCAL
1APPLICATION.REFERENCE.LOCAL
APPLICATION.REFERENCE.LOCAL1
APPLICATION.REFERENCE0LOCAL
$ grep -Fx'APPLICATION.REFERENCE.LOCAL'文件
APPLICATION.REFERENCE.LOCAL


I am unable to get the exact matching string as output while trying to grep for a string which has dot in it.

eg: grep "APPLICATION.REFERENCE.LOCAL" <FILENAME>

I have even tried:

grep -F 'APPLICATION.REFERENCE.LOCAL' <FILENAME> which also displays lines with similar matches like APPLICATION.REFERENCE.LOCAL1.

Please, help me.

解决方案

Since you're using -F which would interpret the pattern as a fixed string, you'd need to supply the -x option to match exactly the whole line.

grep -Fx 'APPLICATION.REFERENCE.LOCAL' filename

From man grep:

   -F, --fixed-strings
          Interpret PATTERN as a  list  of  fixed  strings,  separated  by
          newlines,  any  of  which is to be matched.  (-F is specified by
          POSIX.)

   -x, --line-regexp
          Select  only  those  matches  that exactly match the whole line.
          (-x is specified by POSIX.)

Example:

$ cat file
APPLICATION.REFERENCE.LOCAL
1APPLICATION.REFERENCE.LOCAL
APPLICATION.REFERENCE.LOCAL1
APPLICATION.REFERENCE0LOCAL
$ grep -Fx 'APPLICATION.REFERENCE.LOCAL' file
APPLICATION.REFERENCE.LOCAL

这篇关于如何在ksh中grep精确匹配字符串中的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆