Regex in Shell Scripting to pick values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex in Shell Scripting to pick values
# 1  
Old 01-12-2015
Regex in Shell Scripting to pick values

Hi

After lot of trial and error I am really bowled out with the requirement in hand and honestly you are my last hope

Here is what I want to achieve

Values
Code:
 
 
*IF *VALUE MS_SQL_Statistics_Summary.Client_Count_Percent_Used *GT 70.00 *AND *VALUE MS_SQL_Statistics_Summary.Client_Count_Percent_Used *LE 90.00
*IF *VALUE System.Page_Scan_Rate *GE 500
*IF *VALUE Unix_Memory.Used_Swap_Space_Pct *GT 90.0
*IF *VALUE NT_System.%_Total_Processor_Time *GE 90 *AND *VALUE NT_System.%_Total_Processor_Time *LT 95
*IF *VALUE MS_SQL_Server_Enterprise_View.Total_Lock_Conflicts *GE 60 
*IF *VALUE MS_SQL_Server_Summary.Total_OS_CPU_Percent *GE 80.00 *AND *VALUE MS_SQL_Server_Summary.Time_Since_Startup *GE 10
*IF *VALUE NT_Memory.Memory_Usage_Percentage *GE 90 *AND *VALUE NT_Memory.Memory_Usage_Percentage *LT 95

I need to fill a variable with the following information

if GT or GE string is found then fill with the corresponding next number for example 70.00 for first line 500 for second line 90.0 for thirdline
Sixth line has two GE so fill with 80.00,10 that is the one which is first occurence [comma] second occurrence
There will be lines which doesn't have *GE or *GT fill nothing

Here is what I could achieve till now

formula is a variable which contains the line one by one above in a for loop

e.g
formula="*IF *VALUE MS_SQL_Server_Summary.Total_OS_CPU_Percent *GE 80.00 *AND *VALUE MS_SQL_Server_Summary.Time_Since_Startup *GE 10"

Code:
 
valueGreater=`echo $formula | sed -nr 's/.*GE|.*GT ([0-9]+).*/\1/p'`

Obviously the above line is not fetching me the right results as i would like to have
Note this is part of a huge script. I cannot change the scriptling language now

Please help with some pointersSmilieSmilie
# 2  
Old 01-12-2015
Hello radioactive9,

Not completly sure but if you need values of string GE and GT then could you please try following and let me know if this helps, I have taken values of GE and GT as follows.
Code:
awk -vs1="GE" '{for(i=1;i<=NF;i++){if($i ~ s1){A[NR]=A[NR]?A[NR] OFS $(i+1):NR OFS $(i+1)}}}  END{for(y in A){print A[y]}}' Input_file
OR
awk -vs1="GE" '{
                for(i=1;i<=NF;i++){
                                        if($i ~ s1){
                                                        A[NR]=A[NR]?A[NR] OFS $(i+1):NR OFS $(i+1)
                                                   }
                                  }
               }
                               END{
                for(y in A)       {
                                        print A[y]
                                  }
               }
             '  Input_file

Output is as follows:
Code:
4 90
5 60
6 80.00 10
7 90
2 500

For GT values:
Code:
awk -vs1="GT" '{for(i=1;i<=NF;i++){if($i ~ s1){A[NR]=A[NR]?A[NR] OFS $(i+1):NR OFS $(i+1)}}}  END{for(y in A){print A[y]}}' Input_file
OR
awk -vs1="GT" '{
                for(i=1;i<=NF;i++){
                                        if($i ~ s1){
                                                        A[NR]=A[NR]?A[NR] OFS $(i+1):NR OFS $(i+1)
                                                   }
                                  }
               }
                               END{
                for(y in A)       {
                                        print A[y]
                                  }
               }
             ' Input_file

Output will be as follows.
Code:
1 70.00
3 90.0

Here it is giving line number and then value of string next to it, also if there are more than a single occurance of words it is taking them into a single line. Kindly let me know additional details with complete input like how you are going to use it, will be helpful for us to advise you.

EDIT: Also you can remove the highlighted code and it wouldn't show you the line number which I added to just make sure
all same line's values are on same line.


Thanks,
R. Singh

Last edited by RavinderSingh13; 01-12-2015 at 06:43 AM.. Reason: Added a note to remove line numbers if happy with
# 3  
Old 01-12-2015
Hello,

You can try:
Code:
$ echo $formula | sed -nr 's/^[0-9.,]*//;:b;s/GE |GT ([0-9.]*)/\1\n/;tb;:c;s/([0-9.,]*)[^\n]*\n/\1,/;tc;s/^,| .*$//gp'

Regards
# 4  
Old 01-12-2015
Hi

Thank you R. Singh. That is hell of a code. Almost what I need.

I need GT and GE to be listed together and not separately that is if the code sees GT or GE just pick up the value next to it. If it sees both pick both

A line can be there where we will have *GE and *GT in same formula. Pick both in same variable $valueGreater with a space is OK



Code:
 
-bash-3.2$ valueGreater=`echo $formula | awk -vs1="GE" '{for(i=1;i<=NF;i++){if($i ~ s1){A[NR]=A[NR]?A[NR] OFS $(i+1):NR OFS $(i+1)}}}  END{for(y in A){print A[y]}}'`
-bash-3.2$echo $valueGreater
1 80.00 10
-bash-3.2$

---------- Post updated at 07:21 AM ---------- Previous update was at 07:10 AM ----------

Quote:
Originally Posted by disedorgue
Hello,

You can try:
Code:
$ echo $formula | sed -nr 's/^[0-9.,]*//;:b;s/GE |GT ([0-9.]*)/\1\n/;tb;:c;s/([0-9.,]*)[^\n]*\n/\1,/;tc;s/^,| .*$//gp'

Regards

Almost works perfectly for
Code:
 
 
*IF *VALUE MS_SQL_Server_Summary.Total_OS_CPU_Percent *GE 80.00 *AND *VALUE MS_SQL_Server_Summary.Time_Since_Startup *GE 10


But fails on

Code:
 
*IF *VALUE MS_SQL_Server_Summary.Total_OS_CPU_Percent *GT 80.00 *AND *VALUE MS_SQL_Server_Summary.Time_Since_Startup
 
*IF *VALUE MS_SQL_Statistics_Summary.Client_Count_Percent_Used *GT 90.00


Last edited by radioactive9; 01-12-2015 at 08:48 AM..
# 5  
Old 01-12-2015
Quote:
Originally Posted by radioactive9
Hi

Thank you R. Singh. That is hell of a code. Almost what I need.

I need GT and GE to be listed together and not separately that is if the code sees GT or GE just pick up the value next to it. If it sees both pick both

A line can be there where we will have *GE and *GT in same formula. Pick both in same variable $valueGreater with a space is OK


Also don't want that 1 2 3 at the start

Code:
 
-bash-3.2$ valueGreater=`echo $formula | awk -vs1="GE" '{for(i=1;i<=NF;i++){if($i ~ s1){A[NR]=A[NR]?A[NR] OFS $(i+1):NR OFS $(i+1)}}}  END{for(y in A){print A[y]}}'`
-bash-3.2$echo $valueGreater
1 80.00 10
-bash-3.2$

---------- Post updated at 07:21 AM ---------- Previous update was at 07:10 AM ----------




Almost works perfectly for
Code:
 
 
*IF *VALUE MS_SQL_Server_Summary.Total_OS_CPU_Percent *GE 80.00 *AND *VALUE MS_SQL_Server_Summary.Time_Since_Startup *GE 10


But fails on

Code:
 
*IF *VALUE MS_SQL_Server_Summary.Total_OS_CPU_Percent *GT 80.00 *AND *VALUE MS_SQL_Server_Summary.Time_Since_Startup
 
*IF *VALUE MS_SQL_Statistics_Summary.Client_Count_Percent_Used *GT 90.00

Hello radioactive9,

Could you please try following and let me know if this helps, hope this will(But not tested code though).
Code:
echo $formula | awk -vs1="GE" -vs2="GT" '{for(i=1;i<=NF;i++){if($i ~ s1){A[NR]=A[NR]?A[NR] OFS $(i+1):NR OFS $(i+1)}};if($i ~ s2){D[NR]=D[NR]?D[NR] OFS $(i+1):NR OFS $(i+1)}} END{for(i in A){print A[i]};for(j in D){print D[j]}}'

Thanks,
R. Singh
# 6  
Old 01-12-2015
Quote:
Originally Posted by radioactive9
Hi

Thank you R. Singh. That is hell of a code. Almost what I need.

I need GT and GE to be listed together and not separately that is if the code sees GT or GE just pick up the value next to it. If it sees both pick both

A line can be there where we will have *GE and *GT in same formula. Pick both in same variable $valueGreater with a space is OK


Also don't want that 1 2 3 at the start

Code:
 
-bash-3.2$ valueGreater=`echo $formula | awk -vs1="GE" '{for(i=1;i<=NF;i++){if($i ~ s1){A[NR]=A[NR]?A[NR] OFS $(i+1):NR OFS $(i+1)}}}  END{for(y in A){print A[y]}}'`
-bash-3.2$echo $valueGreater
1 80.00 10
-bash-3.2$

---------- Post updated at 07:21 AM ---------- Previous update was at 07:10 AM ----------




Almost works perfectly for
Code:
 
 
*IF *VALUE MS_SQL_Server_Summary.Total_OS_CPU_Percent *GE 80.00 *AND *VALUE MS_SQL_Server_Summary.Time_Since_Startup *GE 10


But fails on

Code:
 
*IF *VALUE MS_SQL_Server_Summary.Total_OS_CPU_Percent *GT 80.00 *AND *VALUE MS_SQL_Server_Summary.Time_Since_Startup
 
*IF *VALUE MS_SQL_Statistics_Summary.Client_Count_Percent_Used *GT 90.00

With this modification, it works better:
Code:
$ echo $formula | sed -nr 's/^[0-9.,]*//;:b;s/G[TE] *([0-9.]+)/\n\1/;tb;:c;s/([0-9.,]*)[^\n]*\n/\1,/;tc;s/^[^0-9]*|[^0-9]*$//gp'

This User Gave Thanks to disedorgue For This Post:
# 7  
Old 01-12-2015
Quote:
Originally Posted by RavinderSingh13
Hello radioactive9,

Could you please try following and let me know if this helps, hope this will(But not tested code though).
Code:
echo $formula | awk -vs1="GE" -vs2="GT" '{for(i=1;i<=NF;i++){if($i ~ s1){A[NR]=A[NR]?A[NR] OFS $(i+1):NR OFS $(i+1)}};if($i ~ s2){D[NR]=D[NR]?D[NR] OFS $(i+1):NR OFS $(i+1)}} END{for(i in A){print A[i]};for(j in D){print D[j]}}'

Thanks,
R. Singh
Now it doesn't return anything Smilie

---------- Post updated at 08:04 AM ---------- Previous update was at 08:03 AM ----------

Quote:
Originally Posted by disedorgue
With this modification, it works better:
Code:
$ echo $formula | sed -nr 's/^[0-9.,]*//;:b;s/G[TE] *([0-9.]+)/\n\1/;tb;:c;s/([0-9.,]*)[^\n]*\n/\1,/;tc;s/^[^0-9]*|[^0-9]*$//gp'


Awesome so far so good. This one is very very complex. I have no clue how you deduced such a complex code. Thank you man.

I don't understand how it is doing. But it is doing what I want Smilie so far so good
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to calculate avg values of csv file using shell scripting .?

hi all i have a reporting work and i want it to be automated using shell scripting kindly let me know how can i make that possibe . eg data are :... (2 Replies)
Discussion started by: Avinash shaw
2 Replies

2. UNIX for Dummies Questions & Answers

How to use square values in Shell Scripting?

:wall: Hi I am a newbie with Shell Scripting who stuck while creating a shell script for Pythagoras theorem.I need to know how to add the squares for the value in shell scripting(for eg: b2 =a2 +c2). Thanks VR (4 Replies)
Discussion started by: VoraciousReader
4 Replies

3. Shell Programming and Scripting

Need to pick max values of the columns

Hi, I have sar disk reports like below sample: 01:01:00 hdisk24 0 0.0 0 0 0.0 0.0 hdisk15 0 0.0 0 3 0.0 5.5 hdisk20 0 0.0 2 1 0.0 1.9 hdisk19 1 ... (3 Replies)
Discussion started by: reddyr
3 Replies

4. UNIX for Dummies Questions & Answers

How to compare to values returned from sql in shell scripting?

hey i am using this code to connect to sql , store the value in variable and then compare it with another variable after some time by executing the same query but the desired result is not coming #!/bin/bash val=$(sqlplus -s rte/rted2@rel76d2 <<ENDOFSQL set heading off set feedback off... (11 Replies)
Discussion started by: ramsavi
11 Replies

5. Shell Programming and Scripting

Assigning array values using awk in shell scripting

hi My script as below #!/bin/ksh for i in `seq 1 7` do a=$(awk '{print $i}' /home/rama/expenese.txt) done for i in `seq 1 7` do echo "${a}" done content of expense.txt is as below 5032 210179 3110 132813874 53488966 11459221 5300794 I want output as... (6 Replies)
Discussion started by: Ramakrishna V
6 Replies

6. Shell Programming and Scripting

mapping of values in shell scripting

sample content of file1: SSTY1 2145228348 652011011715140100000002419005432092074 008801726143662 VDZX01 MIO2 008801726143662 SSRTY 2145228349 ... (3 Replies)
Discussion started by: vsachan
3 Replies

7. Shell Programming and Scripting

Need help to change XML values with shell scripting for Network Simulation

Hello, I don't have experience in this scripting and I need some help to read a value from an XML file and change it with a random number to use in simulator for different network scenarios. </Description><sim_comm_rounds>35</sim_comm_rounds><num_clusters>1</num_clusters><Clocking> I want to... (5 Replies)
Discussion started by: erhanasd
5 Replies

8. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

9. UNIX for Dummies Questions & Answers

Concatenating arrays cell values in shell scripting

Hi All, I want to concatenate the array cell values and form a string.. Is it possible? for ex. I have an array word_array contains d u m b and after concatenating the string shld be 'dumb' thanks (2 Replies)
Discussion started by: mathur
2 Replies

10. Shell Programming and Scripting

Regex to pick up name from the following including carriage return at end of the line

has anyone got any suggestions how i would pick up the string as part of a substitution inclusive of the carriage return. ie i want to pick up <<NAME>> from the PS output but the <<; seems to be on the line before the NAME. Any ideas are appreciated! ... (3 Replies)
Discussion started by: Shakey21
3 Replies
Login or Register to Ask a Question