awk? extract quoted "" strings from multiple lines.

I am trying to extract multiple strings from snmp-mib files like below.

-----

$ cat IF-MIB.mib
<snip>
linkDown NOTIFICATION-TYPE
    OBJECTS { ifIndex, ifAdminStatus, ifOperStatus }
    STATUS  current
    DESCRIPTION
            "A linkDown trap signifies that the SNMP entity, acting in
            an agent role, has detected that the ifOperStatus object for
            one of its communication links is about to enter the down
            state from some other state (but not from the notPresent
            state).  This other state is indicated by the included value
            of ifOperStatus."
    ::= { snmpTraps 3 }

linkUp NOTIFICATION-TYPE
    OBJECTS { ifIndex, ifAdminStatus, ifOperStatus }
    STATUS  current
    DESCRIPTION
            "A linkUp trap signifies that the SNMP entity, acting in an
            agent role, has detected that the ifOperStatus object for
            one of its communication links left the down state and
            transitioned into some other state (but not into the
            notPresent state).  This other state is indicated by the
            included value of ifOperStatus."
    ::= { snmpTraps 4 }
<snip>

-----

I want to make a simple description summary for each items like this..

linkDown  "A linkDown trap signifies that...(snip)"
linkUp    "A linkUp trap signifies that.....(snip)"

Does anyone know how to achieve this?

Hi, try this:

awk '
  /NOTIFICATION-TYPE/{
    if(s)print s
    s=sprintf("%10-s",$1)
  } 
  f>0{
    $1=$1
    s=s OFS $0
  }
  /"/{
    f--
  } 
  /DESCRIPTION/{
    f=2
  } 
  END{
    if(s)print s
  }
' infile
2 Likes

Thanks a lot! It worked for my example above.:slight_smile:
However, It failed when I tried to use this for whole IF-MIB content.(attached)

-----

[user@host MIB]$ awk '
  /NOTIFICATION-TYPE/{
    if(s)print s
    s=sprintf("%10-s",$1)
  }
  f>0{
    $1=$1
    s=s OFS $0
  }
  /"/{
    f--
  }
  /DESCRIPTION/{
    f=2
  }
  END{
    if(s)print s
  }
' ./IF-MIB.mib
 continue to exist after the next restart."ther they willlid: for network
 of ifOperStatus."r state is indicated by the included valuercting in
 END { ifCompliances 2 }quired."nor is support for the value0ing in an
[user@host MIB]

-----

My goal is to extract the description field of "NOTIFICATION-TYPE" events only, though there is some odd strings in the mib file.... Ummm:(

OK, try:

awk '$2=="NOTIFICATION-TYPE"{split($0,F,/"/); printf "%10-s\"%s\"\n",$1,F[2]}' RS= infile

--
Make sure the file is in Unix format and not in DOS format. You can use

tr -d '\r' < file.dos > file.unix

to convert

1 Like

Hi Scrutinizer,

Thanks a million ! It worked perfectly !:wink:

---------- Post updated 06-25-12 at 01:26 AM ---------- Previous update was 06-24-12 at 07:14 PM ----------

Please let me ask one more question.

----
$ cat ./tmp/*.unix | awk '$2=="NOTIFICATION-TYPE"{split($0,F,/"/); printf "%10-s: \"%s\"\n",$1,F[2]}' RS=

linkUp : "A linkUp trap signifies that the SNMP entity, acting in an
<snip>
included value of ifOperStatus."

----

I am trying to remove "NEW LINES" within the "%s" as my goal is to have the strings like below. (single line per item)

----
linkUp : "A linkUp trap signifies that the SNMP entity, acting in an <snip> included value of ifOperStatus."
----

I think I need to add some knob in order to remove "\n" and while spaces within %s. :confused:

Hi, try:

awk '$2=="NOTIFICATION-TYPE"{$1=$1; split($0,F,/"/); printf "%10-s\"%s\"\n",$1,F[2]}' RS= infile*
1 Like