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
# 8  
Old 07-27-2016
Quote:
Originally Posted by RudiC
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...
sorry for my initial lack of information. i thought what i'd provided in my original post would be enough. i was wrong. I apologize.

thank you for your solution. seems like that'd be good enough. Smilie i'm hoping it'll be portable so that i am able to use it across platforms
# 9  
Old 07-27-2016
awk - only solution:
Code:
awk '
NR==1           {n = split ($0, T1, "_P_")
                 for (i=1; i<=n; i++)   {split (T1[i], 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
                                                }
                }
' <<< "$STRING1"$'\n'"$COUNTPRO"

May not work with awk versions that don't allow for a multi-char field separator.
This User Gave Thanks to RudiC For This Post:
# 10  
Old 07-27-2016
Quote:
Originally Posted by RudiC
awk - only solution:
Code:
awk '
NR==1           {n = split ($0, T1, "_P_")
                 for (i=1; i<=n; i++)   {split (T1[i], 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
                                                }
                }
' <<< "$STRING1"$'\n'"$COUNTPRO"

May not work with awk versions that don't allow for a multi-char field separator.
The standards require that all conforming versions of awk accept an extended regular expression (ERE) for the input field separator (AKA the awk FS variable) and for the 3rd parameter to split() (when it is supplied). Some versions of awk also allow the record separator (the RS variable) to be an ERE, but the standards only specify the behavior when RS is a single character. I don't know of any 1980's or later version of awk that doesn't support an ERE for FS.

On a Solaris/SunOS system, you'll have to use /usr/xpg4/bin/awk or nawk instead of awk.

But, <<< redirections are not specified by the standards and will be treated as a syntax error by many shells. If you want to try RudiC's suggestion on a shell that doesn't support <<<, you could change the last line of the script to:
Code:
' <<EOF
$STRING1
$COUNTPRO
EOF

Or, you could change the 1st and last lines of the awk script to:
Code:
printf '%s\n' "$STRING1" "$COUNTPRO" | awk '

and:
Code:
'

respectively.
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