selective printing of lines

This is a discussion on selective printing of lines within the awk forums in Programming Languages category; Greets all, I have the following output in a file and I need an awk or sed script to look at this format, find out if SNMP is open, then report the host back a-la awk '/open/{print x};{x=$0}' filename However, this would print: PORT STATE SERVICE I need to go up three lines... Anyone? Host 10.10.11.2 is running Solaris 5.8 production PORT STATE SERVICE 161/udp open|filtered snmp Host 10.10.11.3 is running Linux 2.4 kernel production PORT STATE SERVICE 161/udp closed snmp Host 10.10.11.4 is running Solaris 5.9 production PORT STATE SERVICE 161/udp open|filtered snmp Host 10.10.11.5 is running FreeBSD 6.0 ...

Go Back   Application Development Forum > Programming Languages > awk

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-15-2008, 10:47 AM
sil
Guest
 
Default selective printing of lines

Greets all, I have the following output in a file and I need an awk or
sed script to look at this format, find out if SNMP is open, then
report the host back a-la

awk '/open/{print x};{x=$0}' filename

However, this would print:
PORT STATE SERVICE

I need to go up three lines... Anyone?


Host 10.10.11.2 is running
Solaris 5.8 production
PORT STATE SERVICE
161/udp open|filtered snmp

Host 10.10.11.3 is running
Linux 2.4 kernel production
PORT STATE SERVICE
161/udp closed snmp

Host 10.10.11.4 is running
Solaris 5.9 production
PORT STATE SERVICE
161/udp open|filtered snmp

Host 10.10.11.5 is running
FreeBSD 6.0 kernel production
PORT STATE SERVICE
161/udp closed snmp
Reply With Quote
  #2  
Old 08-15-2008, 10:59 AM
Kenny McCormack
Guest
 
Default Re: selective printing of lines

In article <422bb1e3-f181-4733-bdcf-ec6116e38ddc@l64g2000hse.googlegroups.com>,
sil <dsphunxion@gmail.com> wrote:
>Greets all, I have the following output in a file and I need an awk or
>sed script to look at this format, find out if SNMP is open, then
>report the host back a-la
>
>awk '/open/{print x};{x=$0}' filename
>
>However, this would print:
>PORT STATE SERVICE
>
>I need to go up three lines... Anyone?
>
>
>Host 10.10.11.2 is running
>Solaris 5.8 production
>PORT STATE SERVICE
>161/udp open|filtered snmp


How about:

/^Host/ { host = $0 } # Or maybe $2 (for just the hostname/IP)
/open/ { print host }

Reply With Quote
  #3  
Old 08-15-2008, 02:34 PM
Janis Papanagnou
Guest
 
Default Re: selective printing of lines

sil wrote:
> Greets all, I have the following output in a file and I need an awk or
> sed script to look at this format, find out if SNMP is open, then
> report the host back a-la
>
> awk '/open/{print x};{x=$0}' filename
>
> However, this would print:
> PORT STATE SERVICE
>
> I need to go up three lines... Anyone?


The following program will print the complete first line of every
data block that has the open word in the forth line of the block...

BEGIN {RS=""; FS="\n"} $4 ~ / open/ {print $1}

In case you just want the IP number you may use, e.g., split() to
extract the address part.

Janis

>
>
> Host 10.10.11.2 is running
> Solaris 5.8 production
> PORT STATE SERVICE
> 161/udp open|filtered snmp
>
> Host 10.10.11.3 is running
> Linux 2.4 kernel production
> PORT STATE SERVICE
> 161/udp closed snmp
>
> Host 10.10.11.4 is running
> Solaris 5.9 production
> PORT STATE SERVICE
> 161/udp open|filtered snmp
>
> Host 10.10.11.5 is running
> FreeBSD 6.0 kernel production
> PORT STATE SERVICE
> 161/udp closed snmp

Reply With Quote
  #4  
Old 08-15-2008, 07:23 PM
JMH
Guest
 
Default Re: selective printing of lines

On 2008-08-15, Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
> sil wrote:
>> Greets all, I have the following output in a file and I need an awk or
>> sed script to look at this format, find out if SNMP is open, then
>> report the host back a-la
>>
>> awk '/open/{print x};{x=$0}' filename
>>
>> However, this would print:
>> PORT STATE SERVICE
>>
>> I need to go up three lines... Anyone?

>
> The following program will print the complete first line of every
> data block that has the open word in the forth line of the block...
>
> BEGIN {RS=""; FS="\n"} $4 ~ / open/ {print $1}
>
> In case you just want the IP number you may use, e.g., split() to
> extract the address part.


Do you need to use the new line as FS?

Seems to me that you could just put a test
in for $NR = snmp and if true print $2.

being a noob I won't try producing the
full code line ;-)

>> Host 10.10.11.2 is running
>> Solaris 5.8 production
>> PORT STATE SERVICE
>> 161/udp open|filtered snmp
>>
>> Host 10.10.11.3 is running
>> Linux 2.4 kernel production
>> PORT STATE SERVICE
>> 161/udp closed snmp


jmh

Reply With Quote
  #5  
Old 08-15-2008, 07:27 PM
JMH
Guest
 
Default Re: selective printing of lines

On 2008-08-15, JMH <jmhall@apex.local.loc> wrote:
> On 2008-08-15, Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
>> sil wrote:
>>> Greets all, I have the following output in a file and I need an awk or
>>> sed script to look at this format, find out if SNMP is open, then
>>> report the host back a-la
>>>
>>> awk '/open/{print x};{x=$0}' filename
>>>
>>> However, this would print:
>>> PORT STATE SERVICE
>>>
>>> I need to go up three lines... Anyone?

>>
>> The following program will print the complete first line of every
>> data block that has the open word in the forth line of the block...
>>
>> BEGIN {RS=""; FS="\n"} $4 ~ / open/ {print $1}
>>
>> In case you just want the IP number you may use, e.g., split() to
>> extract the address part.

>
> Do you need to use the new line as FS?
>
> Seems to me that you could just put a test
> in for $NR = snmp and if true print $2.


oops -- maybe $NR-1 ~ / open/ ...

jmh

Reply With Quote
  #6  
Old 08-15-2008, 10:13 PM
Janis Papanagnou
Guest
 
Default Re: selective printing of lines

JMH wrote:
> On 2008-08-15, Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
>
>>sil wrote:
>>
>>>Greets all, I have the following output in a file and I need an awk or
>>>sed script to look at this format, find out if SNMP is open, then
>>>report the host back a-la
>>>
>>>awk '/open/{print x};{x=$0}' filename
>>>
>>>However, this would print:
>>>PORT STATE SERVICE
>>>
>>>I need to go up three lines... Anyone?

>>
>>The following program will print the complete first line of every
>>data block that has the open word in the forth line of the block...
>>
>> BEGIN {RS=""; FS="\n"} $4 ~ / open/ {print $1}
>>
>>In case you just want the IP number you may use, e.g., split() to
>>extract the address part.

>
>
> Do you need to use the new line as FS?


"need"? - You don't even "need" the RS; have a look at Kenny's proposal.

>
> Seems to me that you could just put a test
> in for $NR = snmp and if true print $2.


If you just want the IP number that's fine, if you want the whole line
the other solution might be advantageous.

Generally I avoid to linearize tabulateded data or line oriented formats
(as in this case) to be able to extend the logic without rewrite.

>
> being a noob I won't try producing the
> full code line ;-)
>
>
>>>Host 10.10.11.2 is running
>>>Solaris 5.8 production
>>>PORT STATE SERVICE
>>>161/udp open|filtered snmp
>>>
>>>Host 10.10.11.3 is running
>>>Linux 2.4 kernel production
>>>PORT STATE SERVICE
>>>161/udp closed snmp

>
>
> jmh
>

Reply With Quote
  #7  
Old 08-16-2008, 09:31 AM
JMH
Guest
 
Default Re: selective printing of lines

On 2008-08-16, Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
> JMH wrote:
>
>> Do you need to use the new line as FS?

>
> "need"? - You don't even "need" the RS; have a look at Kenny's proposal.


A bad word choice on my part. Thanks for
explaining you rationale for using the RS.

jmh

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:24 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.