LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using grep for formatting. (https://www.linuxquestions.org/questions/linux-newbie-8/using-grep-for-formatting-858412/)

krist_m 01-24-2011 02:28 PM

using grep for formatting.
 
Hi,

I have a raw-output.log file as follows:

spawn ssh xxxxxxxx@1.1.1.1
1.1.1.1
=============

*******************************************************************
* This is test *
* *
* Contact IT *
* *
*******************************************************************

Router-101#sh run | i _email_to
event manager environment _email_to noc-group-101@yyy.com
Router-101#exit
Connection to 1.1.1.1 closed by remote host.
Connection to 1.1.1.1 closed.
spawn ssh xxxxxxxx@2.2.2.2
2.2.2.2
=============

*******************************************************************
* This is test *
* *
* Contact IT *
* *
*******************************************************************

Router-202#sh run | i _email_to
event manager environment _email_to noc-group-202@yyy.com
Router-202#exit
Connection to 2.2.2.2 closed by remote host.
Connection to 2.2.2.2 closed.

I need the exact following output:

1.1.1.1
=============
event manager environment _email_to noc-group-101@yyy.com

2.2.2.2
=============
event manager environment _email_to noc-group-202@yyy.com

How do i get this using grep or other methods. Any help will be appreciate.

Thank you.
KM

mad4linux 01-24-2011 03:59 PM

You could use this sed line:

Code:

sed -n -e /^[0-9].[0-9].[0-9].[0-9]/,/===/p -e /event/p yourfile.txt
This would give you the following output:
Code:

1.1.1.1
=============
event manager environment _email_to noc-group-101@yyy.com
2.2.2.2
=============
event manager environment _email_to noc-group-202@yyy.com

You can redirect this to a file using > like this
Code:

sed -n -e /^[0-9].[0-9].[0-9].[0-9]/,/===/p -e /event/p yourfile.txt > newfile.txt

Tinkster 01-24-2011 04:04 PM

Hi, something like this?

Code:

awk 'BEGIN{FS="\n";RS="closed.\n"} {print $2;print $3;for (i=3;i<=NF;i++){if($i~/event manager/){print $i}};print ""}'  ~/tmp4/routers.
1.1.1.1
=============
event manager environment _email_to noc-group-101@yyy.com

2.2.2.2
=============
event manager environment _email_to noc-group-202@yyy.com


Cheers,
Tink


P.S.: Too slow :} ... and the sed one is prettier.


All times are GMT -5. The time now is 11:36 AM.