使用GREP -V排除字符串

发布于 2025-01-24 05:26:34 字数 293 浏览 0 评论 0原文

我需要排除选定的字符串。我正在尝试使用grep -v选项。

输入:

AM2RGHK
AM2RGHK-JO
AM2RGHK-FN

输出应为:

AM2RGHK-JO
AM2RGHK-FN

从输入列表中,如果我只想排除第一行,我正在使用grep -v am2rghk 但是我没有得到任何输出。 grep -v在同一序列中排除所有字符串。有线索吗?

I have a requirement to exclude a selected string. I am trying to use grep -v option.

Input:

AM2RGHK
AM2RGHK-JO
AM2RGHK-FN

Output should be:

AM2RGHK-JO
AM2RGHK-FN

From the input list, If I want to exclude only first line, I am using grep -v AM2RGHK
But I am not getting any output. grep -v excludes all the strings in the same sequence. Any clue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逐鹿 2025-01-31 05:26:34

GREP与所有输入行匹配,因为它的默认行为是匹配包含给定模式的行。它不必完全等于它。

您可以告诉GREP,必须使用选项-X- line-regexp)找到确切的匹配。

GREP -V -X AM2RGHK执行您想要的工作。


旁注:

由于您似乎不使用实际的正则拨号,但您只需要简单的文本匹配,因此您可以考虑选项-f- filex-sinstrings) 。它告诉grep不要为模式中的任何字符赋予特殊意义。

此外,将shell字符串包裹在'''中始终是一个好习惯。这样可以确保外壳不会尝试解释任何字符,例如空格。它可以使您感到很多头痛。

结果命令将是:

grep -vxF 'AM2RGHK'

grep is matching all the input lines because it's default behavior is to match line that contains the given pattern. It doesn't have to be exactly equal to it.

You can tell grep that it has to find an exact match by using the option -x (--line-regexp).

grep -v -x AM2RGHK does what you want.


Side notes:

Since you don't seem to use an actual regex but you just need simple text match, you may consider the option -F (--fixed-strings). It tells grep to not give special meaning to any character in the pattern.

Moreover, it's always a good practice to encase shell strings in ''. This ensures that the shell doesn't try to interpret any characters, like whitespaces. It can spare you a lot of headaches.

The resulting command would be:

grep -vxF 'AM2RGHK'
我也只是我 2025-01-31 05:26:34
grep -v '^AM2RGHK

Input.txt:

AM2RGHK
AM2RGHK-JO
AM2RGHK-FN

标准输出:

AM2RGHK-JO
AM2RGHK-FN
input.txt

Input.txt:

标准输出:

grep -v '^AM2RGHK

input.txt:

AM2RGHK
AM2RGHK-JO
AM2RGHK-FN

standard output:

AM2RGHK-JO
AM2RGHK-FN
input.txt

input.txt:

standard output:

你好,陌生人 2025-01-31 05:26:34

考虑一下您的控制权,在这种情况下,您可以与所讨论的字符串进行比较,但是我们可以在每一端添加一个垫子字符并查找它。

#!/bin/bash
read_file="/tmp/list.txt"
exclude="AM2RGHK"
while IFS= read -r line ;do
   if ! [[ "Z${line}Z" = "Z${exclude}Z" ]] ;then
      echo "$line"
   fi
done < "${read_file}"

我们说的是,如果zam2rghkz!=电流然后打印它,则比较如下:

ZAM2RGHKZ  = ZAM2RGHKZ  do nothing
ZAM2RGHKZ != ZAM2RGHK-JOZ print because they don't match
ZAM2RGHKZ != ZAM2RGHK-FNZ print because they don't match

因此,输出变为:

AM2RGHK-JO
AM2RGHK-FN

注意:还有更多简洁的方法可以做到这一点,但这也是一个很好的方法。

Think about what you have control over, in this case you can compare against the string in question but instead of just the string we can add a pad character on each end and look for that.

#!/bin/bash
read_file="/tmp/list.txt"
exclude="AM2RGHK"
while IFS= read -r line ;do
   if ! [[ "Z${line}Z" = "Z${exclude}Z" ]] ;then
      echo "$line"
   fi
done < "${read_file}"

This case we are saying if ZAM2RGHKZ != current then print it, the comparison would be as follows:

ZAM2RGHKZ  = ZAM2RGHKZ  do nothing
ZAM2RGHKZ != ZAM2RGHK-JOZ print because they don't match
ZAM2RGHKZ != ZAM2RGHK-FNZ print because they don't match

Hence the output becomes:

AM2RGHK-JO
AM2RGHK-FN

Note: there are more succinct ways to do this but this is a good way as well.

七婞 2025-01-31 05:26:34

GREP有选项-x

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

例如:

[dachnik@test]$ cat > greptest
AM2RGHK
AM2RGHK-JO
AM2RGHK-FN[dachnik@test]$ cat greptest
AM2RGHK
AM2RGHK-JO
AM2RGHK-FN[dachnik@test]$ grep -v -x AM2RGHK greptest
AM2RGHK-JO
AM2RGHK-FN

grep has the option -x

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

for example:

[dachnik@test]$ cat > greptest
AM2RGHK
AM2RGHK-JO
AM2RGHK-FN[dachnik@test]$ cat greptest
AM2RGHK
AM2RGHK-JO
AM2RGHK-FN[dachnik@test]$ grep -v -x AM2RGHK greptest
AM2RGHK-JO
AM2RGHK-FN
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文