Grep / Sed / Awk一个块并搜索模式

时间:2021-02-26 16:51:42

I'm trying to grep blocks from a database with pattern.
After that I wanna grep from every block again with some pattern.
But I need only one match for every pattern of every block.

The file look so:

我正在尝试使用模式从数据库中grep块。在那之后,我想用一些模式再次从每个街区grep。但是我只需要为每个块的每个模式匹配一​​个匹配项。该文件看起来如此:

inetnum:        *.*.*.* - *.*.*.*
netname:        Ch-123
descr:          companyname
descr:          address 1
descr:          address 2
descr:          address 3
country:        FR
admin-c:        DUMY
tech-c:         DUMY
status:         DUMY
mnt-by:         mnt
changed:        nothing@nothing.net
created:        1970-01-01T00:00:00Z
last-modified:  1970-01-01T00:00:00Z
source:         RIPE
remarks:        ****************************
remarks:        ****************************
remarks:        ****************************
remarks:        ****************************
remarks:        ****************************
remarks:        ****************************

% Tags relating to '*.*.*.* - *.*.*.*'
% RIPE-REGISTRY-RESOURCE

inetnum:        *.*.*.* - *.*.*.*
netname:        Ch-123
descr:          companyname
descr:          address 1
descr:          address 2
descr:          address 3
country:        FR
admin-c:        DUMY
tech-c:         DUMY
status:         DUMY
mnt-by:         mnt
changed:        nothing@nothing.net
created:        1970-01-01T00:00:00Z
last-modified:  1970-01-01T00:00:00Z
source:         RIPE
remarks:        ****************************
remarks:        ****************************
remarks:        ****************************
remarks:        ****************************
remarks:        ****************************
remarks:        ****************************

% Tags relating to '*.*.*.* - *.*.*.*'
% RIPE-REGISTRY-RESOURCE

So I have to get each block where "country: CH" is. After that I need for example of each block only ONE Result, like only ONE 'descr', ONE 'netname', even there are more, I need the first one for the companyname.

所以我必须得到“country:CH”所在的每个区块。之后我需要每个块只有一个结果的例子,比如只有一个'descr',一个'网名',即使还有更多,我需要第一个用于公司名称。

Requirement:

需求:

  1. Get blocks of the file, which have "country: CH"
  2. 获取文件的块,其中包含“country:CH”
  3. Then grep/awk/sed whatever only one of this "inetnum,netname,descr"
  4. 然后grep / awk / sed只是其中一个“inetnum,netname,descr”

Output should look like so of each block, where "country: CH" is.

输出应该看起来像每个块,其中“country:CH”是。

*.*.*.* - *.*.*.*
Ch-123
companyname

And NOT:

并不是:

Input:

输入:

grep -E "inetnum:        (.*)\n|netname:        (.*)\|descr:          (.*)\n" file

Output:

输出:

*.*.*.* - *.*.*.*
Ch-123
companyname
address 1
address 2
address 3

I get every time only that. I'm trying it with regular expression, but never used it before.

我每次只得到那个。我正在尝试使用正则表达式,但之前从未使用它。

I hope someone can help me.

我希望有一个人可以帮助我。

AND: Why does : sed -n '/inetnum/!b;:a;/% Tags relating to/!{$!{N;ba}};{/country: ch/p}' file never end?

AND:为什么:sed -n'/ inetnum /!b;:a; /%与/!{$!{N; ba}}; {/ country:ch / p}'文件相关的标签永远不会结束?

1 个解决方案

#1


0  

awk to the rescue!

拯救!

If your structure is fixed and blocks are separated by an empty line this should work

如果您的结构是固定的,并且块由空行分隔,这应该有效

awk -F: -vRS= -vOFS=, '$16~/IT|DE/{print $2,$4,$6}'

#1


0  

awk to the rescue!

拯救!

If your structure is fixed and blocks are separated by an empty line this should work

如果您的结构是固定的,并且块由空行分隔,这应该有效

awk -F: -vRS= -vOFS=, '$16~/IT|DE/{print $2,$4,$6}'