Problem cutting & comparing values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem cutting & comparing values
# 1  
Old 02-09-2006
Problem cutting & comparing values

Hi guys

I am having some problem cutting and comparing values.I got an INI file which is has some values and ranges mapping to error and warning codes

eg
criticalerror:69,20,1to9
warningmsg:101,10to19

So taking the scenrio where i have a control script that execute a.ksh, when a.ksh returns a value , in this case 8, i need to map the return code to the critical error that is within the range 1 to 9.

And i have having a tough time trying to cut using expr or awk toactually retrive the 1 to 9 and using it to compare. My approach is to do something and retrive 1 and 9 and use it to compare the return code.however i am having rather diffculty cutting and comparing.

Can anyone out there help me out ?

regards
wilson
# 2  
Old 02-09-2006
Script requires an input: the error number.
range.txt contains all the error types.

Code:
[/tmp]$ cat extract.ksh 
#! /bin/ksh

(($#)) || { echo "Input an error number" ; exit 1 ; }

err=$1

while read line
do
        range=${line##*,}
#       echo "$range"
        min=${range%%to*}
#       echo $min
        max=${range##*to}
#       echo "$max"

        if [ $err -ge $min -a $err -le $max ] ; then
        echo "Error type: ${line%%:*}"
        break ;
        fi ;

done < range.txt

exit 0;

If you enter an error number outside the range, it will return nothing.
# 3  
Old 02-09-2006
Quote:
Originally Posted by vino
Script requires an input: the error number.
range.txt contains all the error types.

Code:
[/tmp]$ cat extract.ksh 
#! /bin/ksh

(($#)) || { echo "Input an error number" ; exit 1 ; }

err=$1

while read line
do
        range=${line##*,}
#       echo "$range"
        min=${range%%to*}
#       echo $min
        max=${range##*to}
#       echo "$max"

        if [ $err -ge $min -a $err -le $max ] ; then
        echo "Error type: ${line%%:*}"
        break ;
        fi ;

done < range.txt

exit 0;

If you enter an error number outside the range, it will return nothing.
Hi Vino,

thanks for the quick reply, 1 question, is my range.txt supposed to have values like this ? criticalerror:20to29

as i am getting a badnumber error despite entering values within the range
# 4  
Old 02-09-2006
Quote:
Originally Posted by wilsontan
is my range.txt supposed to have values like this ? criticalerror:20to29

as i am getting a badnumber error despite entering values within the range
The range.txt should have entries in the manner you specified i.e.

criticalerror:69,20,1to9
warningmsg:101,10to19


The delimiter is a ,

If it is to be in the manner like

criticalerror:20to29

then change the delimiter to : i.e. range=${line##*,} to range=${line##*:}
# 5  
Old 02-09-2006
Quote:
Originally Posted by vino
The range.txt should have entries in the manner you specified i.e.

criticalerror:69,20,1to9
warningmsg:101,10to19


The delimiter is a ,

If it is to be in the manner like

criticalerror:20to29

then change the delimiter to : i.e. range=${line##*,} to range=${line##*:}

Hi Vino,

thanks for the help it works, just curious what happens in if my INI file has the following format

criticalerror:69,20,1to9
warningmsg:10to19


what will i use to delimit ??
# 6  
Old 02-09-2006
Quote:
Originally Posted by wilsontan
Hi Vino,

thanks for the help it works, just curious what happens in if my INI file has the following format

criticalerror:69,20,1to9
warningmsg:10to19


what will i use to delimit ??
Simple.

Your delimiter would be :

After that, you check for the presence of a , and take the route of range=${line##*,}. Else go down on this lane range=${line##*:}
# 7  
Old 02-09-2006
one more,

Code:
# !/usr/bin/ksh

mesg=""
first=0
last=0
notfound=1

while read line
do
mesg=`echo $line | sed 's/:.*//'`
echo $line | sed -e 's/^.*://;s/\(.*\),\(.*\),\(.*\)to\(.*\)/\3 \4/' | read first last

if [ \( $1 -ge $first \) -a \( $1 -le $last \) ]
then
echo "message is $mesg"
notfound=0
fi

done < ini

if [ $notfound -eq 1 ]
then
echo "unable to find message"
fi

exit 0

Code:
>cat ini
criticalerror:69,20,1to9
warningmsg:101,1,10to19

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting and comparing values

I was trying to extract value of g1 and p1 only inside the tags where t1 is "Reading C (bytes)" and comparing them to make sure p1 is always less than g1. Here is the Json file I'm using - File:- { "g1" : 1482568, "n1" : "v_4", "p1" : 0, "s1" : "RC", "t1" : "LM", } { "g1" :... (3 Replies)
Discussion started by: Mannu2525
3 Replies

2. Shell Programming and Scripting

Cutting string values from a file

My file looks like this.... User:SYSTEM,O/S User:oracle,Process:3408086,Machine:hostname ,Program:sqlplus@hostname (TNS V1-V,Logon Time:25-JUL-20 14 13:36 I want to get the date and time which is displayed after the 'Logon time'. (5 Replies)
Discussion started by: Nagesh_1985
5 Replies

3. Shell Programming and Scripting

Cutting values with delimiter

Hi All, I have a string with , delimiter america,finland,netherlands Now i want these values to be stored in file as below with newline character at end of each value america finland netherlands Regards Prasad (3 Replies)
Discussion started by: krishna_gnv
3 Replies

4. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

5. Shell Programming and Scripting

Comparing the values of two files

Hi Am trying to compare the values of two files.. One is a big file that has many values and the other is a small file.. The big file has all values present in small file.. # cat SmallFile 4456602 22347881 7471282 15859891 8257690 21954701 7078068 18219229 2883826 6094959 100000 ... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

6. Shell Programming and Scripting

Comparing values in column 1

Hi to all, I have the following text within inputfile data1,value1,value2 data1,value3,value2 data1,value5,value6 data2,value1,value2 data2,value3,value4 data3,value1,value2 data3,value3,value4 data4,value1,value2 data4,value3,value4 data4,value5,value6 I would like to... (4 Replies)
Discussion started by: cgkmal
4 Replies

7. Shell Programming and Scripting

comparing values

i have two file and i am comparing both.. in cmp1 ,the content is : the nu of file is : <some integer value> in cmp2 ,the content is : the nu of file is : so want a script which will take value (2) when cmp1 is compared with cmp2.. i mean cmp cmp1 cmp2 the the output will be he nu of... (1 Reply)
Discussion started by: Aditya.Gurgaon
1 Replies

8. Shell Programming and Scripting

comparing values of same variable

Hello all while writing a small script , i got stuck with this simple thing.Hope you guyz can help. Iam trying to read password echo"enter password" read $pwd now i have to check this echo"enter password to proceed" read $pwd now i have to check both the values of the... (2 Replies)
Discussion started by: coolkid
2 Replies

9. Shell Programming and Scripting

problem cutting

echo $line|cut -d " " -f`$plannedCount`- and this is the output $ ./read.sh ./read.sh: 12: not found JADE TRADER 143W MYPEN 40 HC M X10 28 7 1 0 ./read.sh: 9: not found MYPEN 20 GP X X10 15 2 1 0 ./read.sh: 9: not found MYPEN 40 GP X X10 28 7 1 0 ./read.sh: 9: not found MYPEN 20... (6 Replies)
Discussion started by: finalight
6 Replies

10. Shell Programming and Scripting

comparing two float values

I am trying to compare 2 float values using if the foll code does not work a=1.4 b=1.6 if test $a -gt $b then echo "$a is max" else echo "$b is max" fi does -gt work for floating point numbers, if not how do go about for my requirement? can i use bc ? pls help thanks in advance... (2 Replies)
Discussion started by: kavitha
2 Replies
Login or Register to Ask a Question