compare lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare lines in a file
# 1  
Old 10-03-2011
Question compare lines in a file

Hi Folks,

I need to compare the cron's timings from a text file. Need to display how much time does it took for that job.

For example i have the below txt file, I have cron1 started at 05:23:15 and completed at 05:25:57, now i need to find how much time did it took to complete corn1 job for a particular time also need to compare cron, cron2, cron3 with cron, cron2, cron3 respectively. Cron1 has been restarted at 07:38:25, but this should be compared to only cron1 which ran at 07:45:48.
Code:
$ cat sample.TXT
  cron1  Begin      05:23:15          
  cron1  Complete 05:25:57         
  cron2  Begin      05:36:45          
  cron3  Begin      05:36:45          
  cron   Begin      05:36:45           
  cron   Complete   05:40:02          
  cron2  Complete   05:42:03
  cron1  Begin      07:38:25          
  cron1  Complete   07:45:48

I really appreciate your help. Thank you in advance Smilie

Regards
Sendhil Kumaran

Last edited by Franklin52; 10-05-2011 at 03:09 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-03-2011
The entries are straightforward and an awk or even shell script will do the job...tricky entries are those that rollover to the next day...like a cron job that starts 3 mins before midnight and completes 2 mins past midnight. If you have those entries post them to get an idea of their format and use perl to process that data...but first of all what have you tried to solve this.
# 3  
Old 10-03-2011
Not sure your expect output, below code can support overnight time calculate.


Code:
 awk 'function t(x,y,z) {return x+y/60+z/3600}
      /Begin/ {a[$1]=$3}
      /Complete/&&a[$1] { split(a[$1],b,":") 
                          split($3,c,":") 
                          c[1]=c[1]>=b[1]?c[1]:c[1]+24
                          printf "cronjob %s used %.2f hours\n", $1, t(c[1],c[2],c[3])-t(b[1],b[2],b[3])
                          delete a[$1]
                        }' sample.TXT

cronjob cron1 used 0.04 hours
cronjob cron used 0.05 hours
cronjob cron2 used 0.09 hours
cronjob cron1 used 0.12 hours

This User Gave Thanks to rdcwayx For This Post:
# 4  
Old 10-04-2011
Thanks a lot I will try it out Smilie

Quote:
Originally Posted by rdcwayx
Not sure your expect output, below code can support overnight time calculate.


Code:
 awk 'function t(x,y,z) {return x+y/60+z/3600}
      /Begin/ {a[$1]=$3}
      /Complete/&&a[$1] { split(a[$1],b,":") 
                          split($3,c,":") 
                          c[1]=c[1]>=b[1]?c[1]:c[1]+24
                          printf "cronjob %s used %.2f hours\n", $1, t(c[1],c[2],c[3])-t(b[1],b[2],b[3])
                          delete a[$1]
                        }' sample.TXT

cronjob cron1 used 0.04 hours
cronjob cron used 0.05 hours
cronjob cron2 used 0.09 hours
cronjob cron1 used 0.12 hours

---------- Post updated 10-04-11 at 11:06 AM ---------- Previous update was 10-03-11 at 08:45 PM ----------

Thanks mate its working for meSmilie can you please let me know how can I modify this to consider seconds also??? As because Currently it's considering only minutes

Regards
Sendhil

QUOTE=rdcwayx;302561310]Not sure your expect output, below code can support overnight time calculate.


Code:
 awk 'function t(x,y,z) {return x+y/60+z/3600}
      /Begin/ {a[$1]=$3}
      /Complete/&&a[$1] { split(a[$1],b,":") 
                          split($3,c,":") 
                          c[1]=c[1]>=b[1]?c[1]:c[1]+24
                          printf "cronjob %s used %.2f hours\n", $1, t(c[1],c[2],c[3])-t(b[1],b[2],b[3])
                          delete a[$1]
                        }' sample.TXT

cronjob cron1 used 0.04 hours
cronjob cron used 0.05 hours
cronjob cron2 used 0.09 hours
cronjob cron1 used 0.12 hours

[/QUOTE]
# 5  
Old 10-04-2011
Code:
gawk 'function time(r,s,t,x,y,z) 
      {str1=mktime("2011 1 1" FS r FS s FS t)
       str2=mktime("2011 1 1" FS x FS y FS z)
       diff=str1-str2 
       H=int(diff/3600)
       M=int((diff-H*3600)/60)
       S=diff-H*3600-M*60
       return sprintf ("%02d:%02d:%02d",H,M,S) 
      }
      /Begin/ {a[$1]=$3}
      /Complete/&&a[$1] { split(a[$1],b,":") 
                          split($3,c,":") 
                          c[1]=c[1]>=b[1]?c[1]:c[1]+24
                          printf "cronjob %-6s used %s\n", $1, time(c[1],c[2],c[3],b[1],b[2],b[3])
                          delete a[$1]
                        }' sample.TXT

cronjob cron1  used 00:02:42
cronjob cron   used 00:03:17
cronjob cron2  used 00:05:18
cronjob cron1  used 00:07:23

This User Gave Thanks to rdcwayx For This Post:
# 6  
Old 10-05-2011
Fabulous thanks a ton Smilie It is working perfectly for me. If possible can you please elaborate for my knowledge. Appreciate your kind support and Once again Thank you so much.

Regards
Sendhil


Quote:
Originally Posted by rdcwayx
Code:
gawk 'function time(r,s,t,x,y,z) 
      {str1=mktime("2011 1 1" FS r FS s FS t)
       str2=mktime("2011 1 1" FS x FS y FS z)
       diff=str1-str2 
       H=int(diff/3600)
       M=int((diff-H*3600)/60)
       S=diff-H*3600-M*60
       return sprintf ("%02d:%02d:%02d",H,M,S) 
      }
      /Begin/ {a[$1]=$3}
      /Complete/&&a[$1] { split(a[$1],b,":") 
                          split($3,c,":") 
                          c[1]=c[1]>=b[1]?c[1]:c[1]+24
                          printf "cronjob %-6s used %s\n", $1, time(c[1],c[2],c[3],b[1],b[2],b[3])
                          delete a[$1]
                        }' sample.TXT

cronjob cron1  used 00:02:42
cronjob cron   used 00:03:17
cronjob cron2  used 00:05:18
cronjob cron1  used 00:07:23


Last edited by Sendhil.Kumaran; 10-05-2011 at 04:24 PM..
# 7  
Old 10-11-2011
Great work very helpful.

Yeah, I got a similar kind of the things to do, but in my log file I do have file size for both start and end. Now I need to print difference and the file size.

My log has the following fields in same format. So now by using the above script I did get diff between start and end. Next is that I need to display file size from start only as in end statement the file size is always zero.

File1 start time filesize
File1 end time filesize
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare 2 files and create a result file with unmatched lines from first file.?

HI, I have 2 text files. file1 and file2. file1.txt (There are no duplicates in this file) 1234 3232 4343 3435 6564 6767 1213 file2.txt 1234,wq,wewe,qwqw 1234,as,dfdf,dfdf 4343,asas,sdds,dsds 6767,asas,fdfd,fdffd I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Discussion started by: Little
6 Replies

2. Shell Programming and Scripting

Script to compare lines in a file

Need help to create the script that does the following : - 1. Compare the current line "column B and C" with next line "column B and C" 2. If they are the same, print output to a file Input file 2014-08-25 04:45:56.673|T1|JO|Begin|10 2014-08-25 04:55:56.673|T1|JO|Begin|11 2014-08-25... (8 Replies)
Discussion started by: chailee
8 Replies

3. UNIX for Dummies Questions & Answers

Compare 2 files print the lines of file 2 that contain a string from file 1

Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help File 1 123 232 W343... (6 Replies)
Discussion started by: KevinRidley
6 Replies

4. Shell Programming and Scripting

need to read lines in file and compare value in if not working

Hello, I have this file that sometime contains 0 lines and sometimes 1 or more. It's supposed to then put the result (could be 0 or 1 or 2 or more) into a variable. Then it's supposed to echo using an if else statement depending on the value of the variable. flagvar='wc -l $tempfile |... (1 Reply)
Discussion started by: script_op2a
1 Replies

5. Shell Programming and Scripting

Compare lines within a file

could someone write a short script which is able to take a text file input and compare lines 1 and 2, 3 and 4, 5 and 6 ... ... until the end of the file. And to prompt if the lines are not equal. Thank you! (10 Replies)
Discussion started by: c_lady
10 Replies

6. Shell Programming and Scripting

Read Two lines in a CSV File and Compare

Hi , I have a CSV file ( file.csv) with some data as below: A,1,abc,x,y,z,,xyz,20100101,99991231 A,1,abc,x,y,z,234,xyz,20100101,99991231 I have to delete the duplicate line based on unique identifiers which are values in the fields- 2,3,4,8.These coulmns in both the rows have same... (6 Replies)
Discussion started by: Sheel
6 Replies

7. Shell Programming and Scripting

Trying to do a compare with multiple lines in a file

Hey guys I am having a problem with being able to find unused profiles in a configuration check script I am trying to create for accountability purposes for managing a large number of systems. What I am trying to do is run a script that will look at the raw config data in a file and pull all the... (3 Replies)
Discussion started by: scottzx7rr
3 Replies

8. Shell Programming and Scripting

compare values in different lines of file

Hi everybody, I have a file that looks like: A B C D -1 0 E F G H -2 0 I J K L +1 M N O P -6 I would like to compare $5 of every line. If both values are negative, I calculate a mean value and write the first line and delete the second one. If the two $5 values are different only... (6 Replies)
Discussion started by: s-layer
6 Replies

9. Shell Programming and Scripting

How do I compare the last two lines of a file?

Hi, I want to compare the last two lines of a files, specifically characters 32 - 50 in both lines and generate an exit code if the range of characters do not match. Please advise. Thanks in advance! (2 Replies)
Discussion started by: limshady411
2 Replies

10. Shell Programming and Scripting

Help! How to compare two lines in a file

Hello, I am newcomer and sorry for this simple question. I want to how to compare two lines in a file? For example, to compare the first line and the second line of a file to know if they are same? Thanks in advance! Leon (3 Replies)
Discussion started by: sabertooth2000
3 Replies
Login or Register to Ask a Question