Need to reduce the execution time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to reduce the execution time
# 1  
Old 01-29-2018
Need to reduce the execution time

We are trying to execute below script for finding out the occurrence of a particular word in a log file Need suggestions to optimize the script.

Test.log size - Approx to 500 to 600 MB

Code:
$wc -l Test.log

16609852 Test.log

po_numbers - 11 to 12k po's to search

Code:
$more po_numbers

xxx1335
AB1085
SSS6205
UY3347
OP9111
....and so on

Current Execution Time - 2.45 hrs

Code:
while IFS= read -r po
do
check=$(grep -c "PO_NUMBER=$po" Test.log)
echo $po "-->" $check >>list3

if [ "$check" = "0" ]
then
echo $po >>po_to_server
#else break
fi
done < po_numbers

# 2  
Old 01-29-2018
Welcome KumarPiyush7225,

I have a few to questions pose in response first:-
  • What OS, shell and version are you using?
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
  • What are the rules for causing an alert? Is it simply the record being mentioned 3 or more times?
  • What is the format of the Test.log file?

Looking at the code, for every record in po_numbers you are reading the full Test.log file. That is the target - reduce the number of times you read a huge file.


There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


Kind regards,
Robin
# 3  
Old 01-29-2018
Thanks for your response.
  • Linux simtosp81 2.6.18-274.el5 #1 SMP Fri Jul 8 17:36:59 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux
  • I want to know if the po in po_numbers file does not exist in test.log than put that in po_to_server file.
  • If the pattern matches i.e. PO_NUMBER=$po then its OK if not then print that po to po_to_server file.
  • The test.log file is a Human readable text file which can have the pattern PO_NUMBER=$po in any position/row of the log file.
# 4  
Old 01-29-2018
Something like this?

Code:
awk -v s='^PO_Number=' '
  NR==FNR {
    A[$1]
    next
  }
  {
    for(i=1; i<=NF; i++)
      if (sub(s,x,$i))
        if ($i in A)
          C[$i]++
  } 

  END {
    for(i in C) print i " --> " C[i]
  }
' po_numbers Test.log  > list3

or with some shell-fu:
Code:
grep -of <(sed 's/^/PO_Number=/' po_numbers) Test.log |
sort -u |
uniq -c |
sed 's/^ *\([0-9]*\) PO_Number=\(.*\)/\2 --> \1/' > list3


Last edited by Scrutinizer; 01-29-2018 at 12:14 PM..
# 5  
Old 01-29-2018
Another approach in python:-
Code:
match = set(line.strip() for line in open('po_numbers'))

count = {}

for po_num in open('Test.log').read().split():
        if po_num in match:
                if po_num in count:
                        count[po_num] += 1
                else:
                        count[po_num] = 1

f = open('po_to_server', 'w')

for po_number in match:
        if po_number in count:
                print(po_number, count[po_number])
        else:
                print(po_number, 0)
                f.write(po_number + '\n')

f.close()

# 6  
Old 01-29-2018
Try also
Code:
awk '
FNR == NR       {PAT=PAT "|" $1
                 next
                }
FNR == 1        {sub ("=\|", "=(", PAT)
                 sub ("$", ")", PAT)
                }
match ($0, PAT) {TMP = substr ($0, RSTART+10, RLENGTH-10)
                 print TMP > "list3"
                 sub (TMP, "", PAT)
                 sub ("\|\|", "|", PAT)
                 sub ("\(\|", "(", PAT)
                 sub ("\|\)", ")", PAT)
                }

END             {gsub ("[()]", "", PAT)
                 for (n = split (PAT, T, "[=|]"); n>1; n--) print T[n] > "po_2_server"
                }

' PAT="PO_NUMBER=" po_numbers Test.log

# 7  
Old 01-30-2018
In reply to
Scrutinizer
Post# 4

How do i get the other file i.e. po_to_server.(it will have all the po which where not found in the test.log) ??

---------- Post updated at 04:59 PM ---------- Previous update was at 04:51 PM ----------

How do i get the other file i.e. po_to_server.(it will have all the po which where not found in the test.log) ??




Quote:
Originally Posted by Scrutinizer
Something like this?

Code:
awk -v s='^PO_Number=' '
  NR==FNR {
    A[$1]
    next
  }
  {
    for(i=1; i<=NF; i++)
      if (sub(s,x,$i))
        if ($i in A)
          C[$i]++
  } 

  END {
    for(i in C) print i " --> " C[i]
  }
' po_numbers Test.log  > list3

or with some shell-fu:
Code:
grep -of <(sed 's/^/PO_Number=/' po_numbers) Test.log |
sort -u |
uniq -c |
sed 's/^ *\([0-9]*\) PO_Number=\(.*\)/\2 --> \1/' > list3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Optimizing script to reduce execution time

AFILENAME=glow.sh FILENAME="/${AFILENAME}" WIDTHA=$(echo ${FILENAME} | wc -c) NTIME=0 RESULTS=$(for eachletter in $(echo ${FILENAME} | fold -w 1) do WIDTHTIMES=$(awk "BEGIN{printf... (5 Replies)
Discussion started by: SkySmart
5 Replies

2. Shell Programming and Scripting

Help to reduce time of archiving

hi all, i have written the following script that does this work: 1. copy large logs files from one server to another. 2. then unzip this files and extraxt from these large https logs only those fields that are neccesary. 3. then archive the extracted logs to new files. BUT the problem is... (7 Replies)
Discussion started by: arrals_vl
7 Replies

3. Shell Programming and Scripting

Automation script to reduce the installation time

DELETED. (0 Replies)
Discussion started by: vasuvv
0 Replies

4. UNIX for Dummies Questions & Answers

time taken for execution

how much time a particular command or shell script executed there is any command to know this thanks (5 Replies)
Discussion started by: tsurendra
5 Replies

5. Shell Programming and Scripting

need inputs on how i can change my script to reduce amount of time the script takes

HI , I have a list1 which consists of data that i have to search and a list2 which has the files that need to be searched .So basically i am using list1 on list2 to see if list1 data is present if found replace it .I have written the code using foreach loop for each list .This is taking the... (1 Reply)
Discussion started by: madhul2002
1 Replies

6. Shell Programming and Scripting

To reduce execution time

Hi All, The below script I run daily and it consumes 2 hours approx. In this I am calling another script and executing the same twice. Is the loop below the cause for the slow process?Is it possible to finetune the program so that it runs in a much faster way? The first script: #!/bin/ksh... (4 Replies)
Discussion started by: Sreejith_VK
4 Replies

7. UNIX for Advanced & Expert Users

specifying an execution time

Hi all, do ny o u'll know how to set a particular execution time for a program??? for eg.: --> during the execution of a file, i call a certain other function. --> while calling this function, my comp hangs. now is there ny way in which i can go to the nxt line of code by aborting the call... (7 Replies)
Discussion started by: VGR
7 Replies

8. UNIX for Dummies Questions & Answers

last execution time

is there a command in Solaris 8 that will show a particular scripts last execution time? (1 Reply)
Discussion started by: cubs0729
1 Replies

9. BSD

Reduce boot-time delay on FreeBSD?

Say for instance, I would like to reduce the delay/waiting time for the boot-time menu from 10 seconds to 5 seconds, how would I go about doing it? From what I've been able to find, entering "autoboot 5" into the right file would take care of that for me, but the man pages are unclear as to... (1 Reply)
Discussion started by: DownSouthMoe
1 Replies

10. Programming

execution time

hi , i ve coded a C program in that im using malloc dynamically , it is being called many times in the program The program is to simulate jobs in manufacturing system. the execution time is increasing drastically as the number of jobs are increased. could any body tel what may be the problem... (2 Replies)
Discussion started by: ramki_rk
2 Replies
Login or Register to Ask a Question