0

I have file with this output, and I am trying to collect useful data from my file.

R1#show ip route 192.168.5.130
Routing Descriptor Blocks:

  * 192.168.5.128, from 192.168.5.162, 00:20:16 ago, via Serial0/0/0.2

      Route metric is 2172416, traffic share count is 1

      Total delay is 20100 microseconds, minimum bandwidth is 1544 Kbit/sec

      Reliability 255/255, minimum MTU 1500 bytes

      Loading 1/255, Hops 1

I want to grep match if my above paragraph have word "metric" then it should display the whole paragraph not just that line.

Also is there a way I can check condition that if metric==2172416 then return the whole paragraph.

I would like to know the simplest and easiest way to do it, since I am going to apply that in different scenarios.

Also If I have this in my file, how can fetch just the lines from Apr 11? Can I use wildcard here?

CPU0:Apr 11 05:22:04.768 UTC: pim[1182]: %ROUTING-IPV4_PIM-5-INTCHG : 

CPU0:Apr 11 05:22:04.769 UTC: pim[1182]: %ROUTING-IPV4_PIM-5-NBRCHG : 

CPU0:Apr 11 05:22:04.769 UTC: pim[1182]: %ROUTING-IPV4_PIM-5-NBRCHG : 

CPU0:Apr 11 06:09:53.066 UTC: pim[1182]: %ROUTING-IPV4_PIM-5-INTCHG : 

CPU0:Apr 11 06:09:53.066 UTC: pim[1182]: %ROUTING-IPV4_PIM-5-NBRCHG : 

CPU0:Apr 11 06:09:56.707 UTC: pim[1182]: %ROUTING-IPV4_PIM-5-NBRCHG : 

2 Answers 2

0
awk '/Route metric is 2172416/,/Loading/' file.txt

above command prints the line starts from Route metric is 2172416 and ends with Loading line

awk '/Apr 11/' second_file.txt
grep 'Apr 11' second_file.txt

above command will print the lines having Apr 11. you can use grep as well

0

Assuming the data in the first file is exactly as you have shown it, with blank lines, you may use GNU grep (or a few others) and ask for surrounding context:

grep -B 5 -A 6 -F 'Route metric is 2172416' file

This will return 5 lines of context before the match, and 6 lines of context after the match, along with the line containing the fixed string Route metric is 2172416.

To get all lines mentioning Apr 11:

grep -F 'Apr 11' file

or, if you need a more specific matching criteria

grep '^CPU0:Apr 11' file

... which requires that the given pattern matches at the start of the line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .