Display a matching line and the closest preceding line matching another pattern

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I have this requirement where show mac address table of a switch will list 1000 entries .
file1:
switch_1#show mac address table
AA:AA:AA:AA:AA:AA port 47
BB:BB:BB:BB:BB:BB port 48
.
.
.
DD:DD:DD:DD:DD:DD port 50
file2:
switch_2#show mac address table
AA:AA:AA:AA:AA:AA port 47
BB:BB:BB:BB:BB:BB port 48
.
.
.
DD:DD:DD:DD:DD:DD port 45
I need to find a way to display the line containing DD:DD:DD:DD:DD:DD and once this occurs, it should search few lines above and print the hostname containing line as well
So the output should look like:
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
file1, file2are stored in the same directory
(these are just examples, there is like 100+ files/switches and MAC needs to be filtered along with the switch names)
text-processing awk sed
add a comment |Â
up vote
2
down vote
favorite
I have this requirement where show mac address table of a switch will list 1000 entries .
file1:
switch_1#show mac address table
AA:AA:AA:AA:AA:AA port 47
BB:BB:BB:BB:BB:BB port 48
.
.
.
DD:DD:DD:DD:DD:DD port 50
file2:
switch_2#show mac address table
AA:AA:AA:AA:AA:AA port 47
BB:BB:BB:BB:BB:BB port 48
.
.
.
DD:DD:DD:DD:DD:DD port 45
I need to find a way to display the line containing DD:DD:DD:DD:DD:DD and once this occurs, it should search few lines above and print the hostname containing line as well
So the output should look like:
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
file1, file2are stored in the same directory
(these are just examples, there is like 100+ files/switches and MAC needs to be filtered along with the switch names)
text-processing awk sed
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have this requirement where show mac address table of a switch will list 1000 entries .
file1:
switch_1#show mac address table
AA:AA:AA:AA:AA:AA port 47
BB:BB:BB:BB:BB:BB port 48
.
.
.
DD:DD:DD:DD:DD:DD port 50
file2:
switch_2#show mac address table
AA:AA:AA:AA:AA:AA port 47
BB:BB:BB:BB:BB:BB port 48
.
.
.
DD:DD:DD:DD:DD:DD port 45
I need to find a way to display the line containing DD:DD:DD:DD:DD:DD and once this occurs, it should search few lines above and print the hostname containing line as well
So the output should look like:
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
file1, file2are stored in the same directory
(these are just examples, there is like 100+ files/switches and MAC needs to be filtered along with the switch names)
text-processing awk sed
I have this requirement where show mac address table of a switch will list 1000 entries .
file1:
switch_1#show mac address table
AA:AA:AA:AA:AA:AA port 47
BB:BB:BB:BB:BB:BB port 48
.
.
.
DD:DD:DD:DD:DD:DD port 50
file2:
switch_2#show mac address table
AA:AA:AA:AA:AA:AA port 47
BB:BB:BB:BB:BB:BB port 48
.
.
.
DD:DD:DD:DD:DD:DD port 45
I need to find a way to display the line containing DD:DD:DD:DD:DD:DD and once this occurs, it should search few lines above and print the hostname containing line as well
So the output should look like:
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
file1, file2are stored in the same directory
(these are just examples, there is like 100+ files/switches and MAC needs to be filtered along with the switch names)
text-processing awk sed
text-processing awk sed
edited Aug 8 at 22:11
don_crissti
47k15124154
47k15124154
asked Aug 8 at 21:54
Maran Ganesh
204
204
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
awk solution
If we get a line with a hash, store it in variable a.
If we get a line with a matching MAC address, print variable a, along with the current line.
$ awk '/#/a=$0/^DD:DD:DD:DD:DD:DD/print a"n"$0' file1 file2
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
$
sed solution
If we get a line with a hash, place it in the hold space (h).
If we get a line with a matching MAC address, append to the pattern space (H), copy hold space to pattern space (g), print pattern space (p)
$ sed -n '/#/h;/^DD:DD:DD:DD:DD:DD/H;g;p' file1 file2
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
$
1
orsed '/ðÂÂÂ/h;/ð©/!d;H;xif you're a golfer...
â don_crissti
Aug 8 at 22:36
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
awk solution
If we get a line with a hash, store it in variable a.
If we get a line with a matching MAC address, print variable a, along with the current line.
$ awk '/#/a=$0/^DD:DD:DD:DD:DD:DD/print a"n"$0' file1 file2
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
$
sed solution
If we get a line with a hash, place it in the hold space (h).
If we get a line with a matching MAC address, append to the pattern space (H), copy hold space to pattern space (g), print pattern space (p)
$ sed -n '/#/h;/^DD:DD:DD:DD:DD:DD/H;g;p' file1 file2
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
$
1
orsed '/ðÂÂÂ/h;/ð©/!d;H;xif you're a golfer...
â don_crissti
Aug 8 at 22:36
add a comment |Â
up vote
3
down vote
accepted
awk solution
If we get a line with a hash, store it in variable a.
If we get a line with a matching MAC address, print variable a, along with the current line.
$ awk '/#/a=$0/^DD:DD:DD:DD:DD:DD/print a"n"$0' file1 file2
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
$
sed solution
If we get a line with a hash, place it in the hold space (h).
If we get a line with a matching MAC address, append to the pattern space (H), copy hold space to pattern space (g), print pattern space (p)
$ sed -n '/#/h;/^DD:DD:DD:DD:DD:DD/H;g;p' file1 file2
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
$
1
orsed '/ðÂÂÂ/h;/ð©/!d;H;xif you're a golfer...
â don_crissti
Aug 8 at 22:36
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
awk solution
If we get a line with a hash, store it in variable a.
If we get a line with a matching MAC address, print variable a, along with the current line.
$ awk '/#/a=$0/^DD:DD:DD:DD:DD:DD/print a"n"$0' file1 file2
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
$
sed solution
If we get a line with a hash, place it in the hold space (h).
If we get a line with a matching MAC address, append to the pattern space (H), copy hold space to pattern space (g), print pattern space (p)
$ sed -n '/#/h;/^DD:DD:DD:DD:DD:DD/H;g;p' file1 file2
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
$
awk solution
If we get a line with a hash, store it in variable a.
If we get a line with a matching MAC address, print variable a, along with the current line.
$ awk '/#/a=$0/^DD:DD:DD:DD:DD:DD/print a"n"$0' file1 file2
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
$
sed solution
If we get a line with a hash, place it in the hold space (h).
If we get a line with a matching MAC address, append to the pattern space (H), copy hold space to pattern space (g), print pattern space (p)
$ sed -n '/#/h;/^DD:DD:DD:DD:DD:DD/H;g;p' file1 file2
switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45
$
edited Aug 8 at 22:28
answered Aug 8 at 22:01
steve
12.9k22149
12.9k22149
1
orsed '/ðÂÂÂ/h;/ð©/!d;H;xif you're a golfer...
â don_crissti
Aug 8 at 22:36
add a comment |Â
1
orsed '/ðÂÂÂ/h;/ð©/!d;H;xif you're a golfer...
â don_crissti
Aug 8 at 22:36
1
1
or
sed '/ðÂÂÂ/h;/ð©/!d;H;x if you're a golfer...â don_crissti
Aug 8 at 22:36
or
sed '/ðÂÂÂ/h;/ð©/!d;H;x if you're a golfer...â don_crissti
Aug 8 at 22:36
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461377%2fdisplay-a-matching-line-and-the-closest-preceding-line-matching-another-pattern%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password