Hi all,
my problem is extract a value from a text, i mean, I have this text:
> ala
Nr of active alarms are: 16
================================================================================================
Sever Specific Problem Cause Mo-Reference
================================================================================================
Maj DcDevice_DeviceDisabled replaceable_unit_problem Pool=DcDevice,DcDevice=51
Maj NbapDedicated_RncRbsControlLinkDown transmission_error Iub=012,NbapDedicated=1
Maj NbapDedicated_RncRbsControlLinkDown transmission_error Iub=586,NbapDedicated=1
Maj Cell_ServiceUnavailable unavailable Cell=012U1
Maj Cell_ServiceUnavailable unavailable Cell=012U2
Maj Cell_ServiceUnavailable unavailable Cell=012U3
Maj Cell_ServiceUnavailable unavailable Cell=586U1
Maj Cell_ServiceUnavailable unavailable Cell=586U2
Maj Cell_ServiceUnavailable unavailable Cell=586U3
Warn Cell_NbapReconfigurationFailure performance_degraded Cell=032V2
Warn Cell_NbapReconfigurationFailure performance_degraded Cell=727U1
Warn Cell_NbapReconfigurationFailure performance_degraded Cell=727U2
Warn Cell_NbapReconfigurationFailure performance_degraded Cell=727U3
Warn VC ete Alarm Indication Signal alarm_indication_signal VccTp=011
Warn VC ete Alarm Indication Signal alarm_indication_signal VccTp=322
>>> Total: 16 Alarms (0 Critical, 9 Major)
I want to extract the line where "Total: 16 Alarms" is shown and extract the two value into (), e.g. (0 Critical, 9 Major) and use the two value, in this case 0 and 9 as a var.
Can anybody help me?
Thank you!
awk '{ if($0 ~ /^>>>/) {print substr($5, 2), $7)} }' inputfile | read var1 var2
echo "var1= $var1 var2= $var2"
# eval $(awk -F'[()]' '/Total:/{split($2,a,",| ");print a[2]"="a[1]";"a[5]"="a[4]}' file)
# echo $Critical
0
# echo $Major
9
1 Like
I agree the above solution are indeed "neat" , but as I am not an expert in awk, so just tried it with simple cut way -
inpLine=`grep -h ">>>" sampleFile`
#to extract "Total: 16 Alarms"
varTotal=`echo $inpLine | cut -d"(" -f1 | sed -e "s/>>> //g"`
#to extract value of critical in $valCrit
varCrit=`echo $inpLine |cut -d"(" -f2 | cut -d")" -f1 | cut -d"," -f1`
valCrit=`echo $varCrit | cut -d" " -f1`
#to extract value of major in $varMajor
varMajor=`echo $inpLine |cut -d"(" -f2 | cut -d")" -f1 | cut -d"," -f2`
valMajor=`echo $varMajor | cut -d" " -f1`
You can be , just take time and practice
Maybe you are not aware of Useless Use of Cat Award
Thank you guys, a very good lesson!
dr.house:
CUT != CAT
That's right did you follow the link ?
gini32:
inpLine=`grep -h ">>>" sampleFile`
#to extract "Total: 16 Alarms"
varTotal=`echo $inpLine | cut -d"(" -f1 | sed -e "s/>>> //g"`
#to extract value of critical in $valCrit
varCrit=`echo $inpLine |cut -d"(" -f2 | cut -d")" -f1 | cut -d"," -f1`
valCrit=`echo $varCrit | cut -d" " -f1`
#to extract value of major in $varMajor
varMajor=`echo $inpLine |cut -d"(" -f2 | cut -d")" -f1 | cut -d"," -f2`
valMajor=`echo $varMajor | cut -d" " -f1`
In this example we call 9 times cut, one extra sed and one grep.
All can be done by using awk call.
1 Like
Hi there,
i still have some problem: all solution you gave me are good, but i am not in deep knowledge to let them work.
The awk solution has some error, the other two don't give any answer.
Can anybody give me the full script (reading a file etc..)?
Thanks
e.g.
#!/bin/bash
rpt=/home/user/alarms
awk '{ if($0 ~ /^>>>/) {print substr($5, 2), $7)} }' alarms | read var1 var2
echo "var1= $var1 var2= $var2"
With bash you can do something like:
#!/bin/bash
rpt=/home/user/alarms
set $(awk '{ if($0 ~ /^>>>/) {print substr($5, 2), $7)} }' alarms)
echo "var1= $1 var2= $2"
marimovo:
Hi there,
i still have some problem: all solution you gave me are good, but i am not in deep knowledge to let them work.
The awk solution has some error, the other two don't give any answer.
Can anybody give me the full script (reading a file etc..)?
Thanks
e.g.
#!/bin/bash
rpt=/home/user/alarms
awk '{ if($0 ~ /^>>>/) {print substr($5, 2), $7)} }' alarms | read var1 var2
echo "var1= $var1 var2= $var2"
The problem here is likely to be that read is running in a subshell, so whatever it assigns to var1 and var2 is not visible when that subshell exists and you try to echo the values. You can try something like:
awk ... | { read var1 var2; echo "var1=$var1 var2=$var2"; }
Regards,
Alister
drewk
June 9, 2010, 12:10pm
12
danmero:
# eval $(awk -F'[()]' '/Total:/{split($2,a,",| ");print a[2]"="a[1]";"a[5]"="a[4]}' file)
# echo $Critical
0
# echo $Major
9
This works beautifully...
Here is a test and example:
$ cat test_text.txt
Nr of active alarms are: 16
================================================================================================
Sever Specific Problem Cause Mo-Reference
================================================================================================
Maj DcDevice_DeviceDisabled replaceable_unit_problem Pool=DcDevice,DcDevice=51
Maj NbapDedicated_RncRbsControlLinkDown transmission_error Iub=012,NbapDedicated=1
Maj NbapDedicated_RncRbsControlLinkDown transmission_error Iub=586,NbapDedicated=1
Maj Cell_ServiceUnavailable unavailable Cell=012U1
Maj Cell_ServiceUnavailable unavailable Cell=012U2
Maj Cell_ServiceUnavailable unavailable Cell=012U3
Maj Cell_ServiceUnavailable unavailable Cell=586U1
Maj Cell_ServiceUnavailable unavailable Cell=586U2
Maj Cell_ServiceUnavailable unavailable Cell=586U3
Warn Cell_NbapReconfigurationFailure performance_degraded Cell=032V2
Warn Cell_NbapReconfigurationFailure performance_degraded Cell=727U1
Warn Cell_NbapReconfigurationFailure performance_degraded Cell=727U2
Warn Cell_NbapReconfigurationFailure performance_degraded Cell=727U3
Warn VC ete Alarm Indication Signal alarm_indication_signal VccTp=011
Warn VC ete Alarm Indication Signal alarm_indication_signal VccTp=322
>>> Total: 16 Alarms (0 Critical, 9 Major)
$ eval $(awk -F'[()]' '/Total:/{split($2,a,",| ");print a[2]"="a[1]";"a[5]"="a[4]}' test_text.txt)
$ echo "Critical = $Critical Major = $Major"
Critical = 0 Major = 9
$
alister:
The problem here is likely to be that read is running in a subshell, so whatever it assigns to var1 and var2 is not visible when that subshell exists and you try to echo the values. You can try something like:
awk ... | { read var1 var2; echo "var1=$var1 var2=$var2"; }
Regards,
Alister
This solution seems to be perfect!
---------- Post updated at 12:00 PM ---------- Previous update was at 11:59 AM ----------
drewk:
This works beautifully...
Here is a test and example:
$ cat test_text.txt
Nr of active alarms are: 16
================================================================================================
Sever Specific Problem Cause Mo-Reference
================================================================================================
Maj DcDevice_DeviceDisabled replaceable_unit_problem Pool=DcDevice,DcDevice=51
Maj NbapDedicated_RncRbsControlLinkDown transmission_error Iub=012,NbapDedicated=1
Maj NbapDedicated_RncRbsControlLinkDown transmission_error Iub=586,NbapDedicated=1
Maj Cell_ServiceUnavailable unavailable Cell=012U1
Maj Cell_ServiceUnavailable unavailable Cell=012U2
Maj Cell_ServiceUnavailable unavailable Cell=012U3
Maj Cell_ServiceUnavailable unavailable Cell=586U1
Maj Cell_ServiceUnavailable unavailable Cell=586U2
Maj Cell_ServiceUnavailable unavailable Cell=586U3
Warn Cell_NbapReconfigurationFailure performance_degraded Cell=032V2
Warn Cell_NbapReconfigurationFailure performance_degraded Cell=727U1
Warn Cell_NbapReconfigurationFailure performance_degraded Cell=727U2
Warn Cell_NbapReconfigurationFailure performance_degraded Cell=727U3
Warn VC ete Alarm Indication Signal alarm_indication_signal VccTp=011
Warn VC ete Alarm Indication Signal alarm_indication_signal VccTp=322
>>> Total: 16 Alarms (0 Critical, 9 Major)
$ eval $(awk -F'[()]' '/Total:/{split($2,a,",| ");print a[2]"="a[1]";"a[5]"="a[4]}' test_text.txt)
$ echo "Critical = $Critical Major = $Major"
Critical = 0 Major = 9
$
It doesn't work! Don't know why, it says "BASH ERROR"!
drewk
June 10, 2010, 10:46am
14
Alister usually gets it right, and glad that works.
Curious why you get a Bash error tho for the other awk / eval solution -- what platform and Bash version?
drewk:
Alister usually gets it right, and glad that works.
Curious why you get a Bash error tho for the other awk / eval solution -- what platform and Bash version?
GNU bash, version 3.00.16(1)-release (sparc-sun-solaris2.10)
Copyright (C) 2004 Free Software Foundation, Inc.
drewk
June 11, 2010, 11:43am
16
Ahh solaris -- It is not your Bash at all; it is your version of awk.
On Sun, try `nawk` vs `awk`
So try:
eval $(nawk -F'[()]' '/Total:/{split($2,a,",| ");print a[2]"="a[1]";"a[5]"="a[4]}' test_text.txt)