Please suggest some changes in my code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please suggest some changes in my code
# 1  
Old 08-07-2008
Please suggest some changes in my code

Hi All,

I have made the following code which is taking 10-15 mins to execute completely. Here the file am using is very big( around 1050993843). Can any one make some changes in my code which reduces the time it takes to execute as am very much new to Unix(learner).

One more thing, i have to run this script in 4 of my App Servers. Is there any way that this script should automatically login into the all the servers and get me the final output..

I have tried with public key generation and it is not working properly... it is connecting to the second server and not coming back to the script.....
Please do help me, its very urgent..

#!/bin/ksh

fname=sample.log
server=$(hostname)
cp /dev/null /axphome/mvelmu/scriptcount.txt
check()
{
count=`cat $1 | grep -c "$2"`
if [ $count -eq 0 ]
then
echo "There is no $3 in the log file" >> /axphome/mvelmu/scriptcount.txt
else
echo "$3 = $count" >> /axphome/mvelmu/scriptcount.txt
fi
}
check2()
{
count=`cat $1 | grep -i "$2" | grep -i -c "$3"`
if [ $count -eq 0 ]
then
echo "There is no $4 in the log file" >> /axphome/mvelmu/scriptcount.txt
else
echo "$4 = $count" >> /axphome/mvelmu/scriptcount.txt
fi
}
echo "$server" >> /axphome/mvelmu/scriptcount.txt
echo "*******" >> /axphome/mvelmu/scriptcount.txt
check "$fname" "SuspendCardWebA2AServiceCall.suspendCard Entry" "Suspend card"
check "$fname" "MaintainAccountStatusFacade.suspend card com.americanexpress.gcbs.exception.DataException" "Suspend card Data Exception"
check "$fname" "MaintainAccountStatusFacade.suspend card com.americanexpress.gcbs.exception.BusinessSystemException" "Suspend card BusinessSystemExceptions"
check "$fname" "ApplyForACardHelper.insertStatus Entry" "Apply card"
check "$fname" "ApplyForACardFacade.getBcaProfile com.americanexpress.gcbs.exception.BusinessSystemException" "Apply card BusinessSystemException"
check "$fname" "ApplyForACardFacade.getBcaProfile com.americanexpress.gcbs.exception.UnexpectedException" "Apply card UnExpected Exceptions"
check "$fname" "ApplyForACardBusinessLogic.getBcaProfileProvider exception occured while executing the command" "Apply card BCA profile error"
check "$fname" "MaintainAccountStatusFacade.cancel card Entry" "Cancel card"
check "$fname" "MaintainAccountStatusFacade.cancel card com.americanexpress.gcbs.exception.BusinessSystemException" "Cancel card BusinessSystemException"
check "$fname" "MaintainAccountStatusFacade.cancel card com.americanexpress.gcbs.exception.UnexpectedException" "Cancel card UnExpectedException"
check2 "$fname" "ChangeLimitsServiceBean.process" "entry" "Change limits"
check2 "$fname" "ChangeLimitsBusinessLogic" "DBUnexpectedException" "Change limits DBUnexpected Exception"
check2 "$fname" "ChangeLimitsBusinessLogic" "UnexpectedException" "Change limits UnExpected Exception"
check "$fname" "ChangeCMDetailsServiceBean.validateRequest Entry" "Change CMDetails"
check2 "$fname" "ChangeCMDetailsBusinessLogic" "DBCommunicationException" "Change CMDetails DB exception"
check2 "$fname" "ChangeCMDetailsBusinessLogic" "MYCACommunicationException" "Change CMDetails MYCA exception"
check2 "$fname" "ChangeCMDetailsBusinessLogic" "MultipleExceptions" "Change CMDetails Multiple exception"
#cat /axphome/mvelmu/scriptcount.txt
cat /axphome/mvelmu/scriptcount.txt | mail -s "Script count for $server" Madhav.K.Sunduru@aexp.com,Arun.V.Shankar@aexp.com,Ramasubramanian.Z.Murugesan@aexp.com,Bini.R.Mathew @aexp.com,Rafikul.H.Sekh@aexp.com,Uma.M.Subburayan@aexp.com,Kavitha.V.Madanagopal@aexp.com


Thanks in Advance...
# 2  
Old 08-07-2008
I guess you should not use the below line
count=`cat $1 | grep -c "$2"`
Instead use the above command with slight modification
count=`grep -c "$2" $1`

I want to mention you are using cat command unneccessary(useless use of cat).The line
cat $1 | grep -c "$2"
will read file twice first by "cat" and then by command "grep" and if your file is large it will cost a time to be read twice.

Last edited by Dhruva; 08-07-2008 at 04:18 AM..
# 3  
Old 08-07-2008
Yes agree with dhruva. Dont use pipes unless u really require it.

- nilesh
# 4  
Old 08-07-2008
The Useless Use of Cat is not particularly slowing down the script by much; the main problem is that you are grepping the file over and over for each search string. Perhaps you could change it to grep for all the strings at the same time, then change the output into the format you want. A simple sed script should work for this.

Code:
sed -n -f - sample.log <<HERE | sort | uniq -c
s/.*SuspendCardWebA2AServiceCall.suspendCard Entry.*/Suspend card/p
s/.*MaintainAccountStatusFacade.suspend card com.americanexpress.gcbs.exception.DataException.*/Suspend card Data Exception/p
s/.*MaintainAccountStatusFacade.suspend card com.americanexpress.gcbs.exception.BusinessSystemException.*/Suspend card BusinessSystemExceptions/p
# etc
# and then the check2 cases
/ChangeLimitsServiceBean.process/s/.*entry.*/Change limits/p
/ChangeLimitsBusinessLogic/s.*DBUnexpectedException.*/Change limits DBUnexpected Exception/p
# etc
HERE

This is just a proof of concept, and doesn't handle the case-insensitive searching you have in the original script. Other than that, it will find matching lines, and change them into the corresponding label; then count the number of occurrences of these labels in the resulting output.

Anyway, the point is, in order to speed it up, see if you can run only one pass over the log file, and end up with a much smaller amount of data which you could process further into the report you want.

This doesn't print anything at all for fields which are not found; if you want explicit zero counts, adding that as a post-processing stage should be a fairly trivial exercise.

Perhaps you would do well to separate the string and label data from the script itself. That's also left as an exercise.

Last edited by era; 08-07-2008 at 04:51 AM.. Reason: Oops, updated to also handle the check2 cases (I hope)
# 5  
Old 08-07-2008
ThanQ very much for all your Quick response...
# 6  
Old 08-07-2008
Hi Dhruva,

the command what you have suggest using only one grep is taking me more time than what i was using previously... Can you suggest me some other command which reduces exec time...
# 7  
Old 08-08-2008
Here's a slightly more elaborate awk script which works on the same principle as the sed I posted previously, but includes the zero counts and avoids the need to sort the output. It also somewhat separates the data from the logic. Still missing is a facility to make some of the matching case-insensitive.

Code:
#!/bin/sh

awk 'NR==FNR {
    f = split ($0, a, ":");
    if (f>2) {
        c[a[3]] = a[2];
        r[a[3]] = a[1];
        i[NR] = a[3];
    }
    else {
        c[a[2]] = ".";
        r[a[2]] = a[1];
        i[NR] = a[2];
    }
    next
}
{
    for (l in r) {
        if ($0 ~ r[l] && $0 ~ c[l])
            ++n[l]
    }
}
END {
    for (j = 1; j in i; ++j) {
        if (n[i[j]])
            printf "%s = %i\n", i[j], n[i[j]];
        else
            printf "There is no %s in the log file\n", i[j];
    }
}' - $1 <<"HERE"
SuspendCardWebA2AServiceCall.suspendCard Entry:Suspend card
MaintainAccountStatusFacade.suspend card com.americanexpress.gcbs.exception.DataException:Suspend card Data Exception
MaintainAccountStatusFacade.suspend card com.americanexpress.gcbs.exception.BusinessSystemException:Suspend card BusinessSystemExceptions
... etc ...:... etc ...
ChangeLimitsServiceBean.process:entry:Change limits
ChangeLimitsBusinessLogic:DBUnexpectedException:Change limits DBUnexpected Exception
... etc ...:... etc ...:... etc ...
HERE

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Suggest books

Hi, I'm a beginner and am learning c programming. I want to learn UNIX/LINUX in parallel. But I don't know difference between UNIX and LINUX and where they are applied in real life. As a beginner, some people asked me to start with UNIX. Please let me know some very good books for UNIX. Also a... (6 Replies)
Discussion started by: nerdbee
6 Replies

2. Shell Programming and Scripting

Any body suggest me.........!!!!

i am jaswanth, i am very new to unix/linux, upto now i worked in windows only., but i took coatching for unix.., and my sir teached all my classes in red hat linux and told me that all are same...!!! I know shall programming in red hat linux.., but now i installed opensloaris but the... (5 Replies)
Discussion started by: strgraphics
5 Replies

3. UNIX for Advanced & Expert Users

Pls review this code and suggest if it can be written in a better way

Pls review this code and provide your feedbacks to make it more efficient.I have tried to add to each section. Code ############################################################### #!/bin/ksh RRSRC=/test RREP=/test #Directories test_dir=/test #Imp Files FILENAME=/test/files.txt #... (5 Replies)
Discussion started by: w020637
5 Replies

4. Shell Programming and Scripting

please suggest me a site

hi i need to get the values from an xml file like the <TAG> values and write to a file please suggest me the commands and some good reading material sites so that i can implement (1 Reply)
Discussion started by: perlamohan
1 Replies

5. Shell Programming and Scripting

Can yum be used. If not please suggest.

Hi! I need to install a application from one server to several other servers. My script would copy the install-script to other machines and run it.Since it has to be non-interactive , just wondering if yum can be used for the same. Please let me know , if you guys are aware of other... (1 Reply)
Discussion started by: nua7
1 Replies

6. UNIX for Advanced & Expert Users

suggest book

Hi I am new to Unix/Linux I know commands and shell scripts which are useful for my project. But i need to know the basics and commands and shell scripts in detail and easy guide. Please refer a book. Thanks Haripatn (6 Replies)
Discussion started by: haripatn
6 Replies

7. Shell Programming and Scripting

Can you suggest a more efficient way for this?

Hi I have the following at the end of a service shutdown script used in part of an active-passive failover setup: ### # Shutdown all primary Network Interfaces # associated with failover ### # get interface names based on IP's # and shut them down to simulate loss of # heartbeatd ... (1 Reply)
Discussion started by: mikie
1 Replies

8. UNIX for Advanced & Expert Users

Suggest me the easiest method

Hi, I want to check whether a file of the format myfile_YYYYMMDD_HHMMSS.txt exists in a particular directory. Here YYYYMMDD_HHMMSS is the time stamp, so it will be numbers always . What is the best method to do this I did it like this : ls myfile_*_*.txt but it will list files... (1 Reply)
Discussion started by: shihabvk
1 Replies

9. AIX

Look into this and suggest if any changes needed

Hi, I am new script programming, I have written a script shown velow to read username and passwd from /etc/security/passwd, i am able to read username, but unable to grep lastupdate. please look into the code and suggest if any changes need. #!/bin/ksh USERNAME="" fname=/usr/bin/lastupdate... (1 Reply)
Discussion started by: me_haroon
1 Replies
Login or Register to Ask a Question