Shell Script Extraction

Hello Users,

I am new to unix. I have a requirement to extract the string in the folder with files names XXXX.sev.xxxxx.lookup (There are some more files which I am not interested in like xxxxx.include.xxx.lookup).

1) I am looking for the file with the name "sev" ending with "lookup"

  For example there will be a file with juniper-MIB.sev.snmptrap.lookup

2) In that file I want to extract a string starting with "SNMP". For example the above file would contain the following information

    {"SNMPTRAP-juniper-MPLS-MIB-mplsLspUp","1","2","0"},
    {"SNMPTRAP-juniper-MPLS-MIB-mplsLspDown","3","1","0"},
    {"SNMPTRAP-juniper-MPLS-MIB-mplsLspChange","2","13","1800"},

The output should be SNMPTRAP-juniper-MPLS-MIB-mplsLspUp . There should not { " etc.

3) The output should be sent to a file.

Can some please help me how I can achieve using shell script.

Your help is highly appreciated.

Thanks.
Regards,
RaviShankar.

Welcome to the forum.

Please try this:

for file in *.sev.*.lookup
do
   awk -F \" '/^\{"SNMPTRAP/{print $2}' $file
done

even try this one liner,

awk -F \" '/^\{"SNMPTRAP/{print $2}' *.sev.*.lookup

Dear Anchar,

Thanks for your reply.

When I run that script, I do not see any output on the terminal screen.
The script terminates without any error. But I am not able to see the output.

Thanks.
Regards,
RaviShankar.

which OS you are using?
If solaris, try nawk.

Are you sure that any of the file contains that pattern?

also try this:

awk -F \" '/^{"SNMPTRAP/{print $2}' *.sev.*.lookup

Both are working for me on linux.

Dear Anchal,

I am also using RHEL version 5.

Below is the file name and its content
File name :
juniper-MPLS-MIB.sev.snmptrap.lookup

 
table juniper-MPLS-MIB_sev =
{
    {"SNMPTRAP-juniper-MPLS-MIB-mplsLspUp","1","2","0"},
    {"SNMPTRAP-juniper-MPLS-MIB-mplsLspDown","3","1","0"},
    {"SNMPTRAP-juniper-MPLS-MIB-mplsLspChange","2","13","1800"},
    {"SNMPTRAP-juniper-MPLS-MIB-mplsLspPathDown","3","1","0"},
    {"SNMPTRAP-juniper-MPLS-MIB-mplsLspPathUp","1","2","0"},
    {"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoUp","1","2","0"},
    {"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoDown","3","1","0"},
    {"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoChange","2","13","1800"},
    {"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathDown","3","1","0"},
    {"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathUp","1","2","0"}
}
default = {"Unknown","Unknown","Unknown"}

But for some reason it is not displaying the output

Thanks.

strange!!
What is the source of the file? did you transfer the file from dos/windows?

re-create the file with the same contents just to test.
if you having dos2unix utility, try that.

what is what I tried with your file,

$ cat juniper-MPLS-MIB.sev.snmptrap.lookup 
table juniper-MPLS-MIB_sev =
{
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspUp","1","2","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspDown","3","1","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspChange","2","13","1800"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspPathDown","3","1","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspPathUp","1","2","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoUp","1","2","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoDown","3","1","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoChange","2","13","1800"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathDown","3","1","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathUp","1","2","0"}
}
default = {"Unknown","Unknown","Unknown"}
 
$ awk -F \" '/^\{"SNMPTRAP/{print $2}' *.sev.*.lookup
SNMPTRAP-juniper-MPLS-MIB-mplsLspUp
SNMPTRAP-juniper-MPLS-MIB-mplsLspDown
SNMPTRAP-juniper-MPLS-MIB-mplsLspChange
SNMPTRAP-juniper-MPLS-MIB-mplsLspPathDown
SNMPTRAP-juniper-MPLS-MIB-mplsLspPathUp
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoUp
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoDown
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoChange
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathDown
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathUp
$ 

A sed version:

 
sed -n '/SNMP/p' input_file | sed 's/{"\(.*[a-zA-Z]\).*/\1/g' > outpt_file
 
# cat infile
------------------------------------
File name :
juniper-MPLS-MIB.sev.snmptrap.lookup
 
table juniper-MPLS-MIB_sev =
{
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspUp","1","2","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspDown","3","1","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspChange","2","13","1800"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspPathDown","3","1","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspPathUp","1","2","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoUp","1","2","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoDown","3","1","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoChange","2","13","1800"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathDown","3","1","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathUp","1","2","0"}
}
default = {"Unknown","Unknown","Unknown"}
 
# sed 's/\",".",".*",".*"\},\{0,1\}//' infile | sed -e '/SNMPTRAP/!d' -e 's/^..//'
SNMPTRAP-juniper-MPLS-MIB-mplsLspUp
SNMPTRAP-juniper-MPLS-MIB-mplsLspDown
SNMPTRAP-juniper-MPLS-MIB-mplsLspChange
SNMPTRAP-juniper-MPLS-MIB-mplsLspPathDown
SNMPTRAP-juniper-MPLS-MIB-mplsLspPathUp
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoUp
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoDown
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoChange
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathDown
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathUp

Hi,

script.pl

#!/usr/bin/perl

while (<>) {
if (/^\{\"SNMPTRAP/) { @array=split /"/ ,$_; print $array[1],"\n"; }
}
 perl script.pl *.sev.*.lookup

Dear anchar, panayam

Thanks for your help. Both seems to be working now. But after digging some what deep, I checked some files having entries like below

# hp-SEMI-MIB.sev.snmptrap.lookup v
SNMPTRAP-hp-SEMI-MIB-hpHttpMgCriticalHealthTrap 1       0       0
SNMPTRAP-hp-SEMI-MIB-hpHttpMgDeviceAddedTrap    1       0       0
SNMPTRAP-hp-SEMI-MIB-hpHttpMgDeviceRemovedTrap  1       0       0
SNMPTRAP-hp-SEMI-MIB-hpHttpMgHealthTrap 1       0       0
SNMPTRAP-hp-SEMI-MIB-hpHttpMgNonRecoverableHealthTrap   1       0       0

The above are tab delimited. How can I incorporate both with tab delimiter and "," seperated in both awk and sed script you have given.

Can you please explain what both awk and sed script is doing. So that I can try some more by myself.

Sorry to bother you and bombard with questions.

Regards,
RaviShankar.

try:

$ cat *.lookup
table juniper-MPLS-MIB_sev =
{
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspUp","1","2","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspDown","3","1","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspChange","2","13","1800"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspPathDown","3","1","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspPathUp","1","2","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoUp","1","2","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoDown","3","1","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoChange","2","13","1800"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathDown","3","1","0"},
{"SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathUp","1","2","0"}
}
default = {"Unknown","Unknown","Unknown"}
 
# hp-SEMI-MIB.sev.snmptrap.lookup v
SNMPTRAP-hp-SEMI-MIB-hpHttpMgCriticalHealthTrap 1 0 0
SNMPTRAP-hp-SEMI-MIB-hpHttpMgDeviceAddedTrap 1 0 0
SNMPTRAP-hp-SEMI-MIB-hpHttpMgDeviceRemovedTrap 1 0 0
SNMPTRAP-hp-SEMI-MIB-hpHttpMgHealthTrap 1 0 0
SNMPTRAP-hp-SEMI-MIB-hpHttpMgNonRecoverableHealthTrap 1 0 0
SNMPTRAP-hp-SEMI-MIB-hpHttpMgNonRecoverableHealthTrap   1       0 0
$ awk -F "\"| |\t" '/^\{"SNMPTRAP/ {print $2} /^SNMPTRAP/ {print $1}' *.sev.*.lookup 
SNMPTRAP-juniper-MPLS-MIB-mplsLspUp
SNMPTRAP-juniper-MPLS-MIB-mplsLspDown
SNMPTRAP-juniper-MPLS-MIB-mplsLspChange
SNMPTRAP-juniper-MPLS-MIB-mplsLspPathDown
SNMPTRAP-juniper-MPLS-MIB-mplsLspPathUp
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoUp
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoDown
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoChange
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathDown
SNMPTRAP-juniper-MPLS-MIB-mplsLspInfoPathUp
SNMPTRAP-hp-SEMI-MIB-hpHttpMgCriticalHealthTrap
SNMPTRAP-hp-SEMI-MIB-hpHttpMgDeviceAddedTrap
SNMPTRAP-hp-SEMI-MIB-hpHttpMgDeviceRemovedTrap
SNMPTRAP-hp-SEMI-MIB-hpHttpMgHealthTrap
SNMPTRAP-hp-SEMI-MIB-hpHttpMgNonRecoverableHealthTrap
SNMPTRAP-hp-SEMI-MIB-hpHttpMgNonRecoverableHealthTrap
$ 

As usual,

A sed version:

 
sed -n '/SNMP/p' input_file | sed -e 's/{"\(.*[a-zA-Z]\).*/\1/g' -e 's/ [01]//g' > output_file