grep 从第一个单词周围的行中查找第二个单词

发布于 2024-10-20 01:00:40 字数 372 浏览 1 评论 0原文

我有一个这样的文件:

ABC1 对象类型
...
...
键 {XYZ1}
...
...

ABC2 对象类型
...
...
ABC3 对象类型
...
...
键 { XYZ3 }
...
...

我的第一个搜索词是 KEY(因为它在文件中出现较少),第二个搜索词是 OBJECT-TYPE。 OBJECT-TYPE 可以出现在 KEY 行上方几行(可能是 5 或 10 行)。如果在文件中找到键,我需要具有键值和相应对象类型值的输出。

完全就像:

ABC1 KEY1

ABC2 KEY2

I have a file like this:

ABC1  OBJECT-TYPE
...
...
KEY { XYZ1 }
...
...

ABC2  OBJECT-TYPE
...
...
ABC3  OBJECT-TYPE
...
...
KEY { XYZ3 }
...
...

My first search word is KEY (as its occurs less in a file) and second search word is OBJECT-TYPE. OBJECT-TYPE can occur few lines (may be 5 or 10) above the line with KEY. If a KEY is found in a file, I need output that has the key-value and corresponding object-type-value.

Exactly like:

ABC1 KEY1

ABC2 KEY2

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

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

发布评论

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

评论(4

指尖微凉心微凉 2024-10-27 01:00:40

假设您想要所有对象的所有键,并且键列在对象下面,您可以这样做:

awk '/OBJECT-TYPE/ { obj= $1 } /KEY { .* }/ { print obj, $3 }' file.txt

输出:

ABC1 XYZ1
ABC3 XYZ3

如果您只想要每个对象树中第一个对象的键,其中对象树由一个空行你应该尝试这个:

awk 'BEGIN { nl=1 } /^$/ { nl=1 }  /OBJECT-TYPE/ && nl { obj= $1; nl=0; } /KEY { .* }/ { print obj, $3 }' file.txt

输出:

ABC1 XYZ1
ABC2 XYZ3

Assuming that you want all keys for all objects, and keys are listed below objects, this is how you might do it:

awk '/OBJECT-TYPE/ { obj= $1 } /KEY { .* }/ { print obj, $3 }' file.txt

output:

ABC1 XYZ1
ABC3 XYZ3

In case you only want the keys of the first object in each tree of objects, where object trees are separated by an empty line you should try this:

awk 'BEGIN { nl=1 } /^$/ { nl=1 }  /OBJECT-TYPE/ && nl { obj= $1; nl=0; } /KEY { .* }/ { print obj, $3 }' file.txt

output:

ABC1 XYZ1
ABC2 XYZ3
久隐师 2024-10-27 01:00:40

很难说出你真正想做什么。

awk '/OBJECT-TYPE/ {a[$1]=$1} /KEY/ { print a["ABC" substr($3,length($3))], $1; split("",a) }'

It's hard to tell what you're really trying to do.

awk '/OBJECT-TYPE/ {a[$1]=$1} /KEY/ { print a["ABC" substr($3,length($3))], $1; split("",a) }'
予囚 2024-10-27 01:00:40

以下内容可能很接近。它适用于给定的输入,但它做出了我不知道是否正确的假设。例如,它假设大括号和键值之间有空格。

#!/usr/bin/awk -f

/OBJECT-TYPE/ {
  for(i=2; i<=NF; i++) {
     if( $i ~ /OBJECT-TYPE/ )
        key = $(i-1);
  }
}

/KEY +\{.*\}/ {
   for(i=1; i < NF; i++) {
      if ( $i == "KEY" ) {
         print key, $(i+2);
      }
   }
}

The following may be close. It works for the given input, but it makes assumptions that I do not know are true. For example, it assumes that the curly brackets have spaces between them and the key value.

#!/usr/bin/awk -f

/OBJECT-TYPE/ {
  for(i=2; i<=NF; i++) {
     if( $i ~ /OBJECT-TYPE/ )
        key = $(i-1);
  }
}

/KEY +\{.*\}/ {
   for(i=1; i < NF; i++) {
      if ( $i == "KEY" ) {
         print key, $(i+2);
      }
   }
}
梦一生花开无言 2024-10-27 01:00:40

向后迭代文件,找到 KEY 以及该键后面的所有对象类型可能会更容易。

tac filename | awk '/KEY/ {key = $3} /OBJECT-TYPE/ {print $1, key}'

鉴于上面的示例,此输出

ABC3 XYZ3
ABC2 XYZ3
ABC1 XYZ1

如果您需要输出以与原始文件相同的顺序出现,请将 |tac 添加到管道。

It might be easier to iterate through the file backwards, find the KEY and the all the Object-Types following the key.

tac filename | awk '/KEY/ {key = $3} /OBJECT-TYPE/ {print $1, key}'

Given your example above, this outputs

ABC3 XYZ3
ABC2 XYZ3
ABC1 XYZ1

If you need the output to appear in the same order as the original file, add |tac to the pipeline.

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