awk? extract quoted "" strings from multiple lines.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk? extract quoted "" strings from multiple lines.
# 1  
Old 06-24-2012
awk? extract quoted "" strings from multiple lines.

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

-----
Code:
$ 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..

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

Does anyone know how to achieve this?

Last edited by Scrutinizer; 06-24-2012 at 02:39 AM.. Reason: code tags
# 2  
Old 06-24-2012
Hi, try this:
Code:
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

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-24-2012
Thanks a lot! It worked for my example above.Smilie
However, It failed when I tried to use this for whole IF-MIB content.(attached)

-----
Code:
[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.... UmmmSmilie

Last edited by Scrutinizer; 06-24-2012 at 06:45 AM.. Reason: code tags
# 4  
Old 06-24-2012
OK, try:
Code:
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
Code:
tr -d '\r' < file.dos > file.unix

to convert
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 06-24-2012
Hi Scrutinizer,

Thanks a million ! It worked perfectly !Smilie

---------- 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. Smilie
# 6  
Old 06-24-2012
Hi, try:
Code:
awk '$2=="NOTIFICATION-TYPE"{$1=$1; split($0,F,/"/); printf "%10-s\"%s\"\n",$1,F[2]}' RS= infile*

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract delta records using with "comm" and "sort" commands combination

Hi All, I have 2 pipe delimited files viz., file_old and file_new. I'm trying to compare these 2 files, and extract all the different rows between them into a new_file. comm -3 < sort file_old < sort file_new > new_file I am getting the below error: -ksh: sort: cannot open But if I do... (7 Replies)
Discussion started by: njny
7 Replies

2. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

3. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

4. Shell Programming and Scripting

AWK for multiple line records RS="^" FS="#"

I have to pull multiple line records with ^ as the record separator(RS)... # should be my field separator (FS)... Sample record is: ^-60#ORA-00060: deadlock detected while waiting for resource ORA-00001: unique constraint (SARADM.TCKNUM_PK) violated#PROC:AVAILABLE_FOR_GETNXTTIC#02/27/2012... (7 Replies)
Discussion started by: Vidhyaprakash
7 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

Extract text between two specified "constant" texts using awk

Hi All, From the title you may know that this question has been asked several times and I have done lot of Googling on this. I have a Wikipedia dump file in XML format. All the contents are in one XML file i.e. all different topics have been put in one XML file. Now I need to separate them and... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. Shell Programming and Scripting

Extract Part of a "Word", using AWK or SED????

I have been lurking on this forum for some time now and appreciate Everyone's help. I need to find a way to get the SystemID from this XML file. The file is much larger than just this one line but I can grep and get this line Printed. But really just need the "systemid". <test123: prefintem... (9 Replies)
Discussion started by: elbombillo
9 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question