简体   繁体   中英

Grep/Sed/Awk a block and search for pattern

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:

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.

Requirement:

  1. Get blocks of the file, which have "country: CH"
  2. Then grep/awk/sed whatever only one of this "inetnum,netname,descr"

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

*.*.*.* - *.*.*.*
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?

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}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM