I'm trying to extract all the description fields from a MIB file which contain multiple instances of the following text:
ENTERPRISE compaq
VARIABLES { sysName, cpqHoTrapFlags, cpqSsBoxCntlrHwLocation,
cpqSsBoxCntlrIndex, cpqSsBoxBusIndex, cpqSsBoxVendor,
cpqSsBoxModel, cpqSsBoxSerialNumber, cpqSsBoxFanStatus }
DESCRIPTION
"Storage System fan status change.
The agent has detected a change in the Fan Status of a storage
system. The variable cpqSsBoxFanStatus indicates the current
fan status.
User Action: If the fan status is degraded or failed, replace
any failed fans."
--#TYPE "Fan Status Change (8026)"
Ideally I need to search through the document, and for each instance of 'DESCRIPTION' found, I need to extract the text found immediately after it in quotes, and join them onto a single line. So for example the above would produce something like:
Storage System fan status change. The agent has detected a change in the Fan Status of a storage system. The variable cpqSsBoxFanStatus indicates the current fan status. User Action: If the fan status is degraded or failed, replace any failed fans.
Each instance should be on a seperate line. I'm getting nowhere with this, as my grasp of sed/awk is basic to say the least.
If someone could help me out here, it would literally save me hours or even days in monotonous work!
awk '
{ $1 = $1 }
/DESCRIPTION/ { ORS = " "; P = 1; next }
/"$/ && P { ORS = RS; sub( /"$/, "" ); print; P = 0 }
{ sub( /^"/, "" ) }
P
'
Storage System fan status change. The agent has detected a change in the Fan Status of a storage system. The variable cpqSsBoxFanStatus indicates the current fan status. User Action: If the fan status is degraded or failed, replace any failed fans.
$ tr '\n' ' ' < t1 | sed -rn 's/.*(DESCRIPTION[^"]*")([^"]*)".*/\2/p;' | sed -rn 's/[ \t]+/ /gp'
Storage System fan status change. The agent has detected a change in the Fan Status of a storage system. The variable cpqSsBoxFanStatus indicates the current fan status. User Action: If the fan status is degraded or failed, replace any failed fans.
What do you mean a start :D. It seems to work really well, also for multiple MIB descriptions plus it leaves out the double quotes. I think I managed to shorten it a little:
awk '
{ $1 = $1 }
/DESCRIPTION/ { p=" "; next }
p { p=p" "$0; if (/"$/) {gsub(/ *"/,"",p); print p;p=""}}
' infile
This code is printing the first DESCRIPTION paragraph but if there 2 or more this code will print only the first paragraph.
ex:-
paragraph..
ENTERPRISE compaq
VARIABLES { sysName, cpqHoTrapFlags, cpqSsBoxCntlrHwLocation,
cpqSsBoxCntlrIndex, cpqSsBoxBusIndex, cpqSsBoxVendor,
cpqSsBoxModel, cpqSsBoxSerialNumber, cpqSsBoxFanStatus }
DESCRIPTION
"Storage System fan status change.
The agent has detected a change in the Fan Status of a storage
system. The variable cpqSsBoxFanStatus indicates the current
fan status.
User Action: If the fan status is degraded or failed, replace
any failed fans."
--#TYPE "Fan Status Change (8026)"
DESCRIPTION
000000000000000000000000
3333333333333333333333
211111111111111111
--#TYPE i7i7i7i
output is
Storage System fan status change. The agent has detected a change in the Fan Status of a storage system. The variable cpqSsBoxFanStatus indicates the current fan status. User Action: If the fan status is degraded or failed, replace any failed fans.
so the code need modification...it is a start as scottn said.
BR