Optimizing for loop with awk or anything similar and portable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Optimizing for loop with awk or anything similar and portable
# 1  
Old 07-27-2016
Optimizing for loop with awk or anything similar and portable

The variable COUNTPRO contains:

Code:
COUNTPRO='Error__posting__message__to__EMR__Queue=0
Error__parsing__ReceiptSummary=0
xinetd__=4327
HTTP__1_1__500___=0
START__=2164
Marshaller__exception__while__converting__to__Receipt__xml=0
MessagePublisher__is__not__configured__correctly=0
Error__populating__JAXB__Object=0
collectd__=42'

The variable STRING1 contains:

Code:
STRING1='Error.*posting.*message.*to.*EMR.*Queue#1,1#_P_Marshaller.*exception.*while.*converting.*to.*Receipt.*xml#1,1#_P_Error.*parsing.*ReceiptSummary#1,1#_P_Error.*populating.*JAXB.*Object#1,1#_P_MessagePublisher.*is.*not.*configured.*correctly#1,1#_P_HTTP.*1.1.*500.*-#1,1#_P_xinetd.*#1,1#_P_START:*#1,1#_P_collectd.*#1,1#'

my code:

Code:
 
for allcounts in $(echo $COUNTPRO | sed 's~,~ ~g')
do
      for estr in $(echo ${STRING1} | sed 's~|~ ~g')
      do
                WARNING=$(echo ${estr} | awk -F"[#,]" '{print $2}')
                CRITICAL=$(echo ${estr} | awk -F"[#,]" '{print $3}')
                samestring=$(echo ${estr} | sed 's_#[^#]*#__g' | sed 's/[^=0-9a-zA-Z]/_/g' | sed 's/__/_/g')

                newstring=$(echo ${allcounts} | awk -F"=" '{print $1}' | sed 's~__~_~g')

                if [ "${samestring}" = "${newstring}" ] ; then
                        VALEACH=$(echo ${allcounts} | awk -F"=" '{print $NF}' | sed 's~_n$~~g')
                         if [ $VALEACH -lt $WARNING ] ; then
                                       echo "OK-LGRBT:$allcounts"
                                       break
                        elif [ $VALEACH -ge $WARNING ] && [ $VALEACH -lt $CRITICAL ] ; then
                                      echo "WARNING-LGRBT:$allcounts"
                                      break
                        elif [ $VALEACH -ge $CRITICAL ] ; then
                                     echo "CRITICAL-LGRBT:$allcounts"
                                     break
                       fi
              fi
      done
done


Last edited by SkySmart; 07-27-2016 at 03:43 AM..
# 2  
Old 07-27-2016
What exactly is this script supposed to do?

EDIT: and what needs to be done to it?

Last edited by RudiC; 07-27-2016 at 04:42 AM..
# 3  
Old 07-27-2016
Quote:
Originally Posted by RudiC
What exactly is this script supposed to do?

EDIT: and what needs to be done to it?

it is suppose to show which patterns in the list stored in variable $COUNTPRO have breached the WARNING and CRITICAL thresholds. it looks through each pattern in COUNTPRO and loops through variable $STRING1 to find and make sure it's using the right threshold for the right pattern.

The WARNING and CRITICAL thresholds are taken from the "#1,1#" which is assigned to each pattern specified in variable $STRING1
# 4  
Old 07-27-2016
Can you also post the expected output please?
# 5  
Old 07-27-2016
Do I get it right that both WARNING and CRITICAL are always 1 in all cases in STRING1? And, what is that _P_ prefix for?
# 6  
Old 07-27-2016
Quote:
Originally Posted by RudiC
Do I get it right that both WARNING and CRITICAL are always 1 in all cases in STRING1? And, what is that _P_ prefix for?
oh sorry. the _P_ represents the pipe symbol "|". It is turned to "|" at a much earlier part of the script. the warning and critical thresholds can be different. its just, in this instance, they're the same. but warning can be 1, Critical can be 2. the numbers varies.

---------- Post updated at 07:18 AM ---------- Previous update was at 07:12 AM ----------

Quote:
Originally Posted by pilnet101
Can you also post the expected output please?
the expected output should be:

Code:
OK-LGRBT:Error_posting_message__to__EMR__Queue=0
OK-LGRBT:Error_parsing_ReceiptSummary=0
CRITICAL-LGRBT:xinetd_=4327
OK-LGRBT:HTTP_1_1_500_=0
CRITICAL-LGRBT:START_=2164
OK-LGRBT:Marshaller_exception_while__converting_to_Receipt_xml=0
OK-LGRBT:MessagePublisher_is_not_configured_correctly=0
OK-LGRBT:Error_populating_JAXB_Object=0
CRITICAL-LGRBT:collectd_=42

# 7  
Old 07-27-2016
How do you expect people to propose a reasonable solution to a broken specification like above, supplying absolutely vital info only reluctantly bit by bit when explicitly asked?

Anyway, try (assuming a recent bash shell)
Code:
sed 's/_P_/\n/g' <<< "$STRING1 "$'\n'"ENDREF"$'\n'"  $COUNTPRO " | awk '
/^ENDREF/       {ENDREF = 1
                 next
                }

!ENDREF         {split ($0, TMP, "#")
                 REF[TMP[1]] = TMP[2]
                 next
                }
                {split ($0, TMP, "=")
                 for (r in REF) if (TMP[1] ~ r) {split (REF[r], CW, ",")
                                                 if (TMP[2] <  CW[1]) LVL = "OK"
                                                   else if (TMP[2] >= CW[2]) LVL = "CRITICAL"
                                                   else LVL = "WARNING" 
                                                 print LVL "-LGRBT: ", $0
                                                 break
                                                }
                }
'
OK-LGRBT:    Error__posting__message__to__EMR__Queue=0
OK-LGRBT:  Error__parsing__ReceiptSummary=0
CRITICAL-LGRBT:  xinetd__=4327
CRITICAL-LGRBT:  START__=2164
OK-LGRBT:  Marshaller__exception__while__converting__to__Receipt__xml=0
OK-LGRBT:  MessagePublisher__is__not__configured__correctly=0
OK-LGRBT:  Error__populating__JAXB__Object=0
CRITICAL-LGRBT:  collectd__=42

EDIT: I'm pretty sure the sed part can be incorporated into the awk script but was just fed up with the problem...
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Optimizing bash loop

now, i have to search for a pattern within a particular time frame which the user will provide in the following format: 19/Jun/2018:07:04,21/Jun/2018:21:30 it is easy to get tempted to attempt this search with a variation of the following awk command: awk... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Vlookup using awk non similar files

I need to vlookup and check the server not found. Source file 1 server1 server2 server3 server4 server5_root server6_silver server7 server7-test server7-temp Source file 2 server1_bronze (6 Replies)
Discussion started by: ranjancom2000
6 Replies

3. Shell Programming and Scripting

Add values of similar patterns with awk

so my output is this: session_closed=157 session_opened=151 session_closed=18 session_opened=17 there are two patterns here, but with different values. the two patterns are "session_opened" and "session_closed". i expect there will be many more other patterns. what i want to do is... (8 Replies)
Discussion started by: SkySmart
8 Replies

4. Shell Programming and Scripting

Optimizing awk script

Can this awk statement be optimized? i ask because log.txt is a giant file with several hundred thousands of lines of records. myscript.sh: while read line do searchterm="${1}" datecurr=$(date +%s) file=$(awk 'BEGIN{split(ARGV,var,",");print var}' $line) ... (3 Replies)
Discussion started by: SkySmart
3 Replies

5. Shell Programming and Scripting

Merging two columns from two files with similar names into a loop

I have two files like this: fileA.net A B C fileA.dat 1 2 3 and I want the output output_expected A 1 B 2 C 3 I know that the easier way is to do a paste fileA.net fileA.dat, but the problem is that I have 10,000 couple of files (fileB.net with fileB.dat; fileC.net with... (3 Replies)
Discussion started by: valente
3 Replies

6. Shell Programming and Scripting

Help with awk or something similar

i have a file like this: wedd01A1 1 wedd01A2 2 wedd01A3 1 wedd02A2 3 wedd02A3 4 wadd02A1 1 wadd02A2 5 wqdd01A1 3 wsdd01A3 1 i want out like this: A1 A2 A3 wedd01 1 2 1 wedd02 0 3 4 wadd02 1 5 0 wqdd01 3 0 0 wsdd01 0 0 1 (8 Replies)
Discussion started by: aydj
8 Replies

7. Shell Programming and Scripting

Help in grep function or similar using awk

I have a list of id; for example: file 1 dfghd dfghe dfgey dfgeu I have another data file that contain this ids as headers; for ex. file2 >dfghd gfdgfddl;klfkld;ld;lgl;dld'l'dv >dfghe gkwhjhsgdjdjdjhjddj >dfgey jdkjfhdjhfdkjhfdkhkdk I wanted to compare file 1 and file 2... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

8. Shell Programming and Scripting

awk, sed or similar log repair help

I have a log file that for some reason, once or two time a month, line foods are missing. This log is generated from vmstat everyminute. I dont know why sometimes it does this. Each line in the log should have 18 columns separated by one or more spaces. Good Log: (not actual log) 1 1... (8 Replies)
Discussion started by: Ikon
8 Replies

9. Shell Programming and Scripting

awk - Counting number of similar lines

Hi All I have the input file OMAK_11. OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE ... (8 Replies)
Discussion started by: dhanamurthy
8 Replies

10. Shell Programming and Scripting

how to get the similar function in while loop or for loop

Dear all How to write the shell script for the following statement: (C programming) for (i=0;i<30;i++) { if i=1 continue *skip this number (To do function here....) ... } similar statement in while loop.... I wrote the script in sh... (3 Replies)
Discussion started by: trynew
3 Replies
Login or Register to Ask a Question