带有多行和操作的GREP模式

发布于 2025-01-29 03:49:52 字数 394 浏览 3 评论 0原文

我该如何确定与GREP在多条线上存在的模式?以下是我需要检查的多行模式

 状态:正确
 类型:主
 

我尝试了以下命令,但它检查了一行上的多个字符串,但是在多行文件上,字符串模式匹配失败

  if cat file.txt  | grep -P '^(?=.*Status:.*True)(?=.*Type:.*Master)'; then echo "Present"; else echo "NOT FOUND"; fi

.txt

 接口:VLAN
状态:正确
类型:主
ID:104
 

How can I determine a pattern exists over multiple lines with grep? below is a multiline pattern I need to check is present in the file

 Status:    True
 Type:   Master

I tried the below command but it checks multiple strings on a single line but fails for strings pattern match on multiple lines

  if cat file.txt  | grep -P '^(?=.*Status:.*True)(?=.*Type:.*Master)'; then echo "Present"; else echo "NOT FOUND"; fi

file.txt

Interface:                      vlan
Status:                         True
Type:                           Master
ID:                             104

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

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

发布评论

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

评论(4

苏璃陌 2025-02-05 03:49:52

使用gnu-grep您可以执行此操作:

grep -zoP '(?m)^\s*Status:\s+True\s+Type:\s+Master\s*' file

Status:                         True
Type:                           Master

说明:

  • p:启用PCRE REGEX模式
  • -Z:读取Multiline输入
  • -o:仅打印匹配的数据
  • (?m)启用多行模式,以便我们可以在每行^之前使用
  • ^ /code>:启动一行

Using gnu-grep you can do this:

grep -zoP '(?m)^\s*Status:\s+True\s+Type:\s+Master\s*' file

Status:                         True
Type:                           Master

Explanation:

  • P: Enabled PCRE regex mode
  • -z: Reads multiline input
  • -o: Prints only matched data
  • (?m) Enables MULTILINE mode so that we can use ^ before each line
  • ^: Start a line
也只是曾经 2025-02-05 03:49:52

使用您显示的样本,请尝试以下内容AWK程序。在gnu awk中编写和测试。

awk -v RS='(^|\n)Status:[[:space:]]+True\nType:[[:space:]]+Master' '
RT{
  sub(/^\n/,"",RT)
  print RT
}
'  Input_file

说明: 简单的说明将设置rs(记录awk)为Regex (^|) \ n)状态:[[:space:]]+true \ ntype:[:space:]]+Master(下面说明),在主程序中检查rt是否不是然后用rt中删除新线(启动一个),并使用rt的null和打印值,以获取OP显示的预期输出。

With your shown samples, please try following awk program. Written and tested in GNU awk.

awk -v RS='(^|\n)Status:[[:space:]]+True\nType:[[:space:]]+Master' '
RT{
  sub(/^\n/,"",RT)
  print RT
}
'  Input_file

Explanation: Simple explanation would be setting RS(Record separator of awk) as regex (^|\n)Status:[[:space:]]+True\nType:[[:space:]]+Master(explained below) and in main program checking if RT is NOT NULL then remove new line(starting one) in RT with NULL and print value of RT to get expected output shown by OP.

屌丝范 2025-02-05 03:49:52

我做的如下:

grep -A 1 "^.*Status:.*True" test.txt | grep -B 1 "^Type:.*Master"

-a x表示“还显示x lines a a 找到一个。
-b y的意思是“还显示y lines b 在找到一个

。一个(“类型”一个),然后与上一个(“状态”)一起显示“类型”行。

I did it as follows:

grep -A 1 "^.*Status:.*True" test.txt | grep -B 1 "^Type:.*Master"

The -A x means "also show the x lines After the found one.
The -B y means "also show the y lines Before the found one.

So: show the "Status" line together with the next one (the "Type" one), and then show the "Type" line together with the previous one (the "Status" one).

烟酉 2025-02-05 03:49:52

您还可以在End Prev = $ 0的每条线路中跟踪上一行的设置,并使用模式匹配上一行和当前行。

awk  '
prev ~ /^[[:space:]]*Status:[[:space:]]*True$/ && $0 ~ /^[[:space:]]*Type:[[:space:]]*Master$/{
    printf "%s\n%s", prev, $0
}
{prev = $0}
' file.txt

输出

Status:                         True
Type:                           Master

You could also keep track of the previous line setting in every line at the end prev = $0 and use a pattern to match the previous and the current line.

awk  '
prev ~ /^[[:space:]]*Status:[[:space:]]*True$/ && $0 ~ /^[[:space:]]*Type:[[:space:]]*Master$/{
    printf "%s\n%s", prev, $0
}
{prev = $0}
' file.txt

Output

Status:                         True
Type:                           Master
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文