Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
07-23-2012, 06:29 PM
|
#1
|
LQ Newbie
Registered: Jun 2012
Posts: 15
Rep: 
|
Script - Find time between entries in log file
Hello all,
I have a log file that looks like this:
Quote:
280898275 [org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] INFO c.v.i.c.c.s.CardGenServiceImpl - generating VirtualCard
280952720 [org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] INFO c.v.i.c.c.s.CardGenServiceImpl - Found Fundingcard com.xxxxxxx.xxxxxxx.transactional.FundingCard@15ffb1bd
280952737 [org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] INFO c.v.i.c.c.s.CardGenServiceImpl - Completed generating VirtualCard
280952787 [org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] INFO c.v.i.c.c.s.CardGenServiceImpl - Card controls stored for Virtual Card Hanlde :19096
280953126 [org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] INFO c.v.i.c.c.s.CardGenServiceImpl - generating VirtualCard
281007859 [org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] INFO c.v.i.c.c.s.CardGenServiceImpl - Found Fundingcard com.xxxxxxxx.xxxxxxx.transactional.FundingCard@3ad721b1
281007874 [org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] INFO c.v.i.c.c.s.CardGenServiceImpl - Completed generating VirtualCard
|
The first part is the time in milliseconds. (I cant change this, I dont have access to the code for that.) What I need is to grep the file for "generating VirtualCard" take the number in the beginning and make it "variable a". Then grep for "Completed generating VirtualCard", take the number in the beginning, and make it "variable b". Once thats done, I need to take variable a and subtract it from variable b, and output the result. I just need the output.
What I am looking for is the amount of time it take for the card generation. Any help would be great, as I am not familiar with this..
|
|
|
07-23-2012, 06:53 PM
|
#2
|
Member
Registered: Jun 2008
Location: Belgium
Distribution: Debian
Posts: 109
Rep:
|
Hello
That should be as easy as
Code:
timeA=$(grep "generating VirtualCard" logfile | egrep -o "^[0-9]*")
timeB=$(grep "Completed generating VirtualCard" logfile | egrep -o "^[0-9]*")
echo Time elapsed : $(expr $timeB - $timeA)
|
|
|
07-23-2012, 07:20 PM
|
#3
|
Member
Registered: Jun 2008
Location: Belgium
Distribution: Debian
Posts: 109
Rep:
|
Well, I saw your log file actually has multiple generations of VirtualCard.
Do you need the generation time for each log entry ? If so, you could pipe your file to the following awk script :
Code:
#!/usr/bin/awk -f
BEGIN {
start = -1;
}
/generating VirtualCard/ {
if (start == -1) {
start = $1;
} else {
print "Generations are not sequential !";
exit 1;
}
}
/Completed generating VirtualCard/ {
if (start != -1) {
print "Generated a VirtualCard in", $1 - start, "ms";
start = "-1";
} else {
print "Bad log file";
exit 1;
}
}
|
|
1 members found this post helpful.
|
07-23-2012, 07:29 PM
|
#4
|
LQ Newbie
Registered: Jun 2012
Posts: 15
Original Poster
Rep: 
|
That is correct, there are multiple entries in the same log file that I need the times for. I will try both of these options, and get back to you. Thank you so much!
|
|
|
07-23-2012, 07:35 PM
|
#5
|
Member
Registered: Jun 2008
Location: Belgium
Distribution: Debian
Posts: 109
Rep:
|
Okay, but note that first one won't work with multiple entries.
|
|
|
07-23-2012, 07:37 PM
|
#6
|
LQ Newbie
Registered: Jun 2012
Posts: 15
Original Poster
Rep: 
|
Oh, Okay. Will the second one loop through the whole file and output them all?
|
|
|
07-23-2012, 07:38 PM
|
#7
|
Member
Registered: Jun 2008
Location: Belgium
Distribution: Debian
Posts: 109
Rep:
|
Yes. That is what you want, right ?
|
|
|
07-23-2012, 07:49 PM
|
#8
|
LQ Newbie
Registered: Jun 2012
Posts: 15
Original Poster
Rep: 
|
Thats correct. Thank you so much!
|
|
|
07-23-2012, 09:03 PM
|
#9
|
LQ Newbie
Registered: Jun 2012
Posts: 15
Original Poster
Rep: 
|
Ok, I guess I dont know enough about this.. Can you point me in the direction on how I would accomplish what you said, pipe the logfile into the script..
|
|
|
07-23-2012, 09:16 PM
|
#10
|
Member
Registered: Jun 2008
Location: Belgium
Distribution: Debian
Posts: 109
Rep:
|
Once you've created the script and made it executable with chmod +x, you can pipe your log file as with any command
Code:
cat logfile | ./script
|
|
1 members found this post helpful.
|
07-23-2012, 09:24 PM
|
#11
|
LQ Newbie
Registered: Jun 2012
Posts: 15
Original Poster
Rep: 
|
Thanks. I'm getting "Generations are not sequential !"
|
|
|
07-24-2012, 12:13 PM
|
#12
|
LQ Newbie
Registered: Jun 2012
Posts: 15
Original Poster
Rep: 
|
Thought I would check in and let you know where I got to. Using the code you gave me, I changed it a little, and came up with this:
Code:
#!/usr/bin/awk -f
BEGIN {
start = 1;
}
/- generating VirtualCard/ {
if (start < $1) {
start = $1;
} else {
print "Generations are not sequential !";
exit 1;
}
}
/Completed generating VirtualCard/ {
if (start != -1) {
print "Generated a VirtualCard in", $1 - start, "ms";
start = "-1";
} else {
print "Bad log file";
exit 1;
}
}
Gives me output of:
Quote:
Generated a VirtualCard in 1041 ms
Generated a VirtualCard in 1060 ms
Generated a VirtualCard in 1060 ms
Generated a VirtualCard in 1082 ms
|
Now to change them to seconds. 
|
|
|
07-24-2012, 01:14 PM
|
#13
|
LQ Newbie
Registered: Jun 2012
Posts: 15
Original Poster
Rep: 
|
Ok, pass on the seconds.. Maybe someone can help me with this next part. I need to be able to write a script to loop through the log directory in sequence based on date, and write the output to a file. Example:
Directory:
~/test/
logFile.2012-06-04.log
logFile.2012-06-05.log
logFile.2012-06-06.log
logFile.2012-06-07.log
script.sh
generation.log
Command I am using:
cat logFile.2012-06-04.log | ./script.sh > generation.log
I could do them all manually, by just changing the date of the log file in the command, but I would like to be able to put other files in there, and have it pick them up as well. The log file names will always be logFile.YYYY.MM.DD.log
|
|
|
07-24-2012, 01:35 PM
|
#14
|
LQ Newbie
Registered: Jun 2012
Posts: 15
Original Poster
Rep: 
|
Using this seems to get me what I want:
Code:
#!/bin/bash
i=1
for file in /test/*.log
do
echo "$file" >> generation.log
cat $file | ./script.sh >> generation.log
# echo "File $((i++)) : $file"
done
Now to add some clean up, checking if the generation.log file exists, and if it does, move it to generation.bak.
Will continue to work through it 
|
|
|
07-24-2012, 01:49 PM
|
#15
|
LQ Newbie
Registered: Jun 2012
Posts: 15
Original Poster
Rep: 
|
That was pretty easy. Added a simple if\then:
Code:
DATE=`date '+%m-%d-%y:%H:%M:%S'`
LOG=generation.log
LOGBACK=generation.$DATE.bak
if [ -f $LOG ];
then
mv $LOG $LOGBACK
fi
|
|
|
All times are GMT -5. The time now is 06:48 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|