![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Shell Script Required | ntgobinath | Shell Programming and Scripting | 3 | 05-06-2008 10:04 AM |
| Shell Script Required! | vats | Shell Programming and Scripting | 3 | 08-27-2007 11:29 PM |
| shell script required | sethunath | Linux | 1 | 07-07-2007 02:04 AM |
| shell script required | sethunath | Shell Programming and Scripting | 4 | 07-06-2007 12:39 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Shell Script Required? Pls. help me
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 |
| Forum Sponsor | ||
|
|
|
|||
|
Re: Shell Script Required? Pls. help me
Try this, I am sure there are shorter ways to do this, but this one works fine and is easy to follow
Code:
#!/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
|
|
|||
|
declare not found
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: Code:
cat $1 | while read line; do |
|
|||
|
I imagine that your file contains more than one "record". Is there a separation between records. Like a blank line?
Code:
============ 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 : |
|||
| Google UNIX.COM |