Hi All,
I have Information in the file like,
============ Interface Information ====================
+++++++++++++++++ NMInterface ++++++++++++++
ObjID:251c55a2-2257-71dd-0f68-9887a1f10000
NNMObjID:82857
EntityName:aust00m1.mis.amat.com[ 0 [ 161 ] ]
Description:ATM9/0/0-atm layer
Discovered in Zone:0
EntityType:Interface
IPLevel:4
OverallStatus:NotMon
OADId:0
StatusChangeTime:0
CreateTime:1210839357
ChangeTime:1210839357
Containment Parent:aust00m1.mis.amat.com(1d5ebb70-2257-71dd-0f68-9887a1f10000)
==========Interface Property===============
VPI:0
VCI:0
BoardNo:-
PortNum:0
AuxPortNum:0
IfIndex:161
IfName:AT9/0/0
IfAlias:-
IfType:37
PhysicalAddress:-
L2DomainID:-1
IfOperStatus:2
IfAdminStatus:2
VlanPortType:2001689792
isDiscoContrivedIF:0
IfSpeed:0
IF Capability :
Requirement:
Whenever the string matches "+++++++++++++++++ NMInterface ++++++++++++++" then It should print followed of 3rd line, 8th line and 21st line.
Example result:
EntityName:aust00m1.mis.amat.com[ 0 [ 161 ] ] (3rd line)
OverallStatus:NotMon (8th line)
IfName:AT9/0/0 (21st line)
If anystring match again it should do the same operation.
Thanks in Advance,
Gobinathan.S
ranton
May 28, 2008, 10:23am
2
Try this, I am sure there are shorter ways to do this, but this one works fine and is easy to follow
#!/bin/sh
declare -i lineno
declare -i hadmatch=0
while read line; do
#echo $line
#match=`echo $line | grep 'NMInterface'`
match=`echo $line | grep '^+*\s*NMInterface\s*+*$'`
if [ ! -z "$match" ]; then
echo "$match"
lineno=0
hadmatch=1
fi
if [ $hadmatch != 0 ]; then
#echo "line $lineno: $line"
if [ $lineno == 3 ]; then
echo $line
elif [ $lineno == 8 ]; then
echo $line
elif [ $lineno == 21 ]; then
echo $line
fi
lineno=$lineno+1
fi
done
I'm getting the following error:
./Interface.sh[3]: declare: not found.
./Interface.sh[4]: declare: not found.
Also, Tell me How to insert the Input file?
Thanks,
Gobinathan.S
Anyother script or Solution that will help me?
ranton
May 28, 2008, 11:35am
5
Sorry, first line needs to be #!/bin/bash instead. of #!/bin/sh
/bin/sh is a link on my linux system to /bin/bash so I failed to notice I had put bash specific syntax in there.
Just pipe the input file into the script, like so
script < inputfile
Or if you want to pass it on the command line add something like this on the line immediately before the while like so:
cat $1 |
while read line; do
Which will cause it to read from the file passed as the first argument instead of from the standard input.
#Interface .sh < aust00m1
Memory fault(coredump)
ripat
May 28, 2008, 12:00pm
7
I imagine that your file contains more than one "record". Is there a separation between records. Like a blank line?
============ Interface Information ====================
+++++++++++++++++ NMInterface ++++++++++++++
ObjID:251c55a2-2257-71dd-0f68-9887a1f10000
...
==========Interface Property===============
...
VlanPortType:2001689792
isDiscoContrivedIF:0
IfSpeed:0
IF Capability :
============ Interface Information ====================
+++++++++++++++++ NMInterface ++++++++++++++
ObjID:251c55a2-2257-71dd-0f68-9887a1f10000
...
==========Interface Property===============
...
VlanPortType:2001689792
isDiscoContrivedIF:0
IfSpeed:0
IF Capability :
============ Interface Information ====================
+++++++++++++++++ NMInterface ++++++++++++++
ObjID:251c55a2-2257-71dd-0f68-9887a1f10000
...
==========Interface Property===============
...
VlanPortType:2001689792
isDiscoContrivedIF:0
IfSpeed:0
IF Capability :
The same way more than one record. There is no space line in between.
ripat
May 28, 2008, 12:47pm
9
Try this (g|m|n)awk snippet:
BEGIN {RS="=+ Interface";OFS=FS="\n"}
$2 ~ / NMInterface /{
print $5, $10, $23
}
Usage:
$ gawk -f test.awk file.to.parse
Getting this error:
# awk -f unman.awk aust00m1
awk: Cannot find or open file unman.awk.
The source line number is 1.
The error context is
>>> <<<
#
How to execute the gawk or awk. Pls. help me I will be very greaful to you.
Thanks,
Gobinathan.S
ripat
May 28, 2008, 1:30pm
11
Try this:
$ awk 'BEGIN {RS="=+ Interface";OFS=FS="\n"} $2 ~ / NMInterface /{print $5,$10,$23}' your.file
Ripat,
The script is giving this error:
awk: Input line
([SrcNodeObjID, Sr cannot be longer than 3,000 bytes.
The input line number is 49. The file is aust00m1.
The source line number is 1.
It's trying for some other text patterns.
Thanks,
Gobinathan.S
ripat
May 28, 2008, 2:03pm
13
Can you attach a bigger sample file that I could test?
I have attached the SAMPLE Input file here. Pls. give me the script for my req.
Thanks in Advance,
Gobinathan.S
ripat
May 28, 2008, 2:36pm
15
Script is working flawlessly on my box although only one record is returned. I notice that you have a number of occurrences of:
----------------- NMInterface --------------
+++++++++++++++++ NMInterface ++++++++++++++
and not only
============ Interface Information ====================
+++++++++++++++++ NMInterface ++++++++++++++
Not the same record separator.
Which version of awk do you have?
$ awk --version
Yes, But my requirement is,
Whenever the string matches "+++++++++++++++++ NMInterface ++++++++++++++" then It should print upcoming line of 3rd line, 8th line and 21st line. (No need to consider the above line in any of the text pattern)
I already have same kind of script that will print Whenever the string matches "+++++++++++++++++ Interface ++++++++++++++" it's printing next 5 lines.
#!/bin/sh
awk ' $0 ~ /\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+Interface\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+/ {
match_str="YES"; line_cnt=0; next; }
{
if((line_cnt < 5) && ( match_str=="YES"))
{
print $0;
line_cnt += 1;
}
else {
match_str="NO"; line_cnt=0; }
}' <Input_File>
Pls. guide me now.
Thanks,
Gobinathan.S
ripat
May 28, 2008, 3:11pm
17
ntgobinath:
Yes, But my requirement is,
Whenever the string matches "+++++++++++++++++ NMInterface ++++++++++++++" then It should print upcoming line of 3rd line, 8th line and 21st line. (No need to consider the above line in any of the text pattern)
Right. Try this:
$ awk 'BEGIN {RS="\++ NMInterface";OFS=FS="\n";ORS="\n\n"} {print $4,$9,$22}' your.file
But in that case the 21th line doesn't always return what you expect:
EntityName:aust00m1.mis.amat.com[ 0 [ 255 ] ]
OverallStatus:Normal
IfName:AT9/1/0.145
EntityName:aust00m1.mis.amat.com[ 0 [ 256 ] ]
OverallStatus:Normal
Capability:
EntityName:aust00m1.mis.amat.com[ 0 [ 257 ] ]
OverallStatus:Normal
IfName:AT9/1/0.315
EntityName:aust00m1.mis.amat.com[ 0 [ 258 ] ]
OverallStatus:Normal
Capability:
EntityName:aust00m1.mis.amat.com[ 0 [ 259 ] ]
OverallStatus:Normal
IfName:AT9/1/0.325
Still have the same pbl with awk?
Hi Ripat,
Thank you so much for your hard work on my requirement. I'm looking for an alternate way to achive my requirement. Because It's not giving proper info on 21st line.
I will get back on the forum after I find an alternate way to grep my info.
Thanks for your support, Thanks all
Gobinathan.S
ripat
May 29, 2008, 6:30am
19
Don't give up! If the 3rd info is not always on the 21st line, there is a work around. Try this:
#!/bin/bash
awk '
BEGIN {RS="\++ NMInterface";OFS=FS="\n";ORS="\n\n"}
{match($0, /IfName:[^\n]+/, out);print $4,$9,out[0]}' your.file
Ripat,
How to run this file?
I'm using HP-UX 11.23
First I copied all those your commands to file 'int'
then executed,
Error:
# ./int
syntax error The source line is 3.
The error context is
{match($0, >>> /IfName:[^\n]+/, <<<
awk: The statement cannot be correctly parsed.
The source line is 3.
Then I changed to bash.
then executed,
ERROR:
# bash
# ./int
Segmentation fault (core dumped)
How to resolve this issue?
Thanks,
Gobinathan.S