嵌套的awk从第二个匹配项中显示特定的字符串并显示 [英] Nested awk grab specific string from second match and display

查看:227
本文介绍了嵌套的awk从第二个匹配项中显示特定的字符串并显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件内容为

0::chkconfig --list autofs::
 autofs                 0:off   1:off   2:on    3:on    4:on    5:on    6:off

1::grep "^PROMPT=" /etc/sysconfig/init::
 PROMPT=yes

2::rpm -q prelink::
 prelink-0.4.0-2.el5

3::sysctl fs.suid_dumpable::
 fs.suid_dumpable = 0

4::stat /etc/motd::
   File: `/etc/motd'
  Size: 17              Blocks: 16         IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 10125343    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-04-08 07:54:03.000000000 +0500
Modify: 2019-03-30 19:22:13.000000000 +0500
Change: 2019-03-30 19:22:13.000000000 +0500

5::stat /etc/issue::
   File: `/etc/issue'
  Size: 52              Blocks: 16         IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 10125494    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-03-30 19:12:13.000000000 +0500
Modify: 2012-02-25 22:01:14.000000000 +0500
Change: 2019-03-30 23:54:57.000000000 +0500

我希望第一场比赛能在之间抢到everything:: <everything>\d::

I want first match to grab everything me between :: <everything>\d::

注意: ::后跟\ n新行(想先跳过::)

note: :: is followed up with \n new line (want to skip first ::)

\ d可以是3个地方999(最大).

\d for regex can be 3 places 999 (max).

第二个匹配项是在第一个匹配项中搜索 例如5::

The second match is to search within the first match for e.g for 5::

Access: (0644/-rw-r--r--)

获取权限0644

第二个匹配规则的标准不是固定的,并且会根据要求进行更改,但是第一个匹配规则是相同的.

The second match rule criteria is not fixed and will change depending upon requirement, but the first match rule is same.

最终匹配的输出只是匹配的字符串,而不是整个记录或结果.

The output of final match be just the matched string and not the whole record or result.

到目前为止,我已经尝试过 cat org_op.2019.04.08-12.49.38 | awk 'f{print;f=0} /^3::/{f=1}'

So, far i have tried with cat org_op.2019.04.08-12.49.38 | awk 'f{print;f=0} /^3::/{f=1}'

这会给我

` fs.suid_dumpable = 0`

但不能缩放为多行匹配,只能在匹配下方提供1行

but its not scale to multi-lines matches only gives 1 line below the match

我也在尝试使用awk -F [::,\d""],但是我没有在括号之间得到\ d正则表达式匹配.

I'm also trying with awk -F [::,\d""] but I don't to get \d regex match between bracket.

最终输出

4::stat /etc/motd::
   File: `/etc/motd'
  Size: 17              Blocks: 16         IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 10125343    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-04-08 07:54:03.000000000 +0500
Modify: 2019-03-30 19:22:13.000000000 +0500
Change: 2019-03-30 19:22:13.000000000 +0500

条件#1 用户要输入哪个块,例如开始标记\ d ::,其中"\ d"是用户提供的,并且将在上述新块开始之前结束.

CONDITION # 1 Which block to get, is user input for e.g the start tag \d:: where '\d' is user supplied and it will end before start of new block explained above also.

条件#2 参见fd00h/64768d,但是此条件将被更改,并对其变量匹配的每个块进行唯一写入,我希望我可以根据这种格式要求来增加awk.

CONDITION # 2 See fd00h/64768d , but this condition will be changed and be written unique to each block its variable match, I want an awk which I can grow based upon this formatting requirements.

注意:从用户提供的信息中,我的意思是将其作为变量提供,例如$ var

NOTE: from user-supplied i mean it be given as a variable e.g $var

测试

cat org_op.2019.04.08-12.49.38 | awk -v id=4 -v RS= -F ':' '($1==id) && $17~/\(([0-9]+)\// { print $17}'

(0644/-rw-r--r--) Uid

实际上,我希望此正则表达式匹配/\([0-9][0-9][0-9][0-9]\//) 0644

Where-infact i want this regex to match /\([0-9][0-9][0-9][0-9]\//) 0644

推荐答案

这就是您要尝试做的所有事情吗?

Is this all you're trying to do?

$ awk -v id=4 -v str='Access: (0644/-rw-r--r--)' -v RS= -F':' '($1==id) && index($0,str)' file
4::stat /etc/motd::
   File: `/etc/motd'
  Size: 17              Blocks: 16         IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 10125343    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-04-08 07:54:03.000000000 +0500
Modify: 2019-03-30 19:22:13.000000000 +0500
Change: 2019-03-30 19:22:13.000000000 +0500

这篇关于嵌套的awk从第二个匹配项中显示特定的字符串并显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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