Extract value from a text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract value from a text
# 8  
Old 06-08-2010
Quote:
Originally Posted by dr.house
CUT != CAT
That's right Smilie did you follow the link ?
Quote:
Originally Posted by gini32
Code:
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.
This User Gave Thanks to danmero For This Post:
# 9  
Old 06-09-2010
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.

Code:
#!/bin/bash
rpt=/home/user/alarms
awk '{ if($0 ~ /^>>>/) {print substr($5, 2), $7)} }' alarms | read var1 var2
echo "var1= $var1  var2= $var2"


Last edited by Franklin52; 06-09-2010 at 10:31 AM.. Reason: Please use code tags!
# 10  
Old 06-09-2010
With bash you can do something like:

Code:
#!/bin/bash

rpt=/home/user/alarms
set $(awk '{ if($0 ~ /^>>>/) {print substr($5, 2), $7)} }' alarms)

echo "var1= $1  var2= $2"

# 11  
Old 06-09-2010
Quote:
Originally Posted by 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.

Code:
#!/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:
Code:
awk ... | { read var1 var2; echo "var1=$var1  var2=$var2"; }


Regards,
Alister
# 12  
Old 06-09-2010
Quote:
Originally Posted by danmero
Code:
# 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... Smilie

Here is a test and example:

Code:
$ 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
$


Last edited by drewk; 06-09-2010 at 01:18 PM.. Reason: added example...
# 13  
Old 06-10-2010
Quote:
Originally Posted by 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:
Code:
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 ----------

Quote:
Originally Posted by drewk
This works beautifully... Smilie

Here is a test and example:

Code:
$ 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"!
# 14  
Old 06-10-2010
Quote:
Originally Posted by marimovo
This solution seems to be perfect!


It doesn't work! Don't know why, it says "BASH ERROR"!
Alister usually gets it right, and glad that works. Smilie

Curious why you get a Bash error tho for the other awk / eval solution -- what platform and Bash version?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract pattern from text

Hi all, I got a txt here and I need to extract all D 8888 44 and D 8888 43 + next field =",g("en")];f._sn&&(f._sn= "og."+f._sn);for(var n in f)l.push("&"),l.push(g(n)),l.push("="),l.push(g(f));l.push("&emsg=");l.push(g(d.name+":"+d.message));var m=l.join("");Ea(m)&&(m=m.substr(0,2E3));c=m;var... (5 Replies)
Discussion started by: stinkefisch
5 Replies

2. Shell Programming and Scripting

Extract text between two strings

Hi, I have a text like these: ECHO "BEGGINING THE SHELL....." MV FILE1 > FILE2 UNIQ_ID=${1} PARTITION_1=`${PL}/Q${CON}.KSH "SELECT ....." PARTITION_2=`${PL}/Q${CON}.KSH "SELECT ........" ${PL}/Q${CON}.KSH "CREATE ...." IF .... ....... I would like to extract only text that only... (4 Replies)
Discussion started by: mierdatuti
4 Replies

3. Shell Programming and Scripting

Extract a block of text

Hello all, I am working on a script which should parse a large file called input.txt which contains table definitions, index definitions and comments like these ones: ------------------------------------------------ -- DDL Statements for table "CMWSYS"."CMWD_TEC_SUIVI_TRT"... (12 Replies)
Discussion started by: kiki_riki_miki
12 Replies

4. Shell Programming and Scripting

Extract text from string

Dear community, I know, this is very stupid question, but I'm scratching my head to find a solution. I have a variable like this: var=" INFO : ABCDEFG"Now I need to remove the leading spaces and output the result like: echo "FIELD1 ; FIELD2 ; $RESULT ; FIELD4" ... (17 Replies)
Discussion started by: Lord Spectre
17 Replies

5. Shell Programming and Scripting

Extract text between two strings

Hi I have something like this: EXAMPLE 1 CREATE UNIQUE INDEX "STRING_1"."STRING_2" ON "BOSNI_CAB_EVENTO" ("CD_EVENTO" , "CD_EJECUCION" ) PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 5242880 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "DB1000_INDICES_512K"... (4 Replies)
Discussion started by: chrispaz81
4 Replies

6. Shell Programming and Scripting

How to Extract text between two strings?

Hi, I want to extract some text between two strings in a line i am using following command i.e; awk '/-string1/,/-string2/' filename contents of file is--- line1 line2 aaa -bbb -ccc -string1 c,d,e -string2 line4 but it is showing complete line which is having searched strings. aaa... (19 Replies)
Discussion started by: emresearch
19 Replies

7. Shell Programming and Scripting

extract text from a file

I have been reading several posts regarding how to extract text from a file, but none of those have helped me for what I need. This is my problem: I need to extract the text after my pattern So my line is: 485.74 6589.5 Log likelihood: 1485.79 My pattern is 'Log likelihood:' and I need... (2 Replies)
Discussion started by: loperam
2 Replies

8. Shell Programming and Scripting

Extract particular text

I executed a following sed command => echo "a/b/c/d/e/f/g/h" | sed 's/\/*$//g' a/b/c/d/e/f/g Now what if I want to extract "g" from "a/b/c/d/e/f/g/h" . That is second last string using SED. (4 Replies)
Discussion started by: Shell_Learner
4 Replies

9. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies

10. UNIX for Advanced & Expert Users

extract text b/w two delimiters

I have an input file which looks like " @$SCRIPT/atp_asrmt_adj.sql $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1005w.pls $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1006w.pls start $SCRIPT/cim1020d.sql;^M spool $DATA/cim1021m.sql @$DATA/cim1021m.sql ! rm $DATA/cim1021m.sql spool $DATA/cim1021m.sql... (6 Replies)
Discussion started by: dowsed4u8
6 Replies
Login or Register to Ask a Question