Newbie looking for how to Grep times more than 10 seconds apart


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Newbie looking for how to Grep times more than 10 seconds apart
# 8  
Old 09-18-2016
Don,

Sorry. The times are always on the same day.

I think I asked for more details than I can understand. I got lost on that video after the first minute or two. I am so new to unix that any of these commands have NO meaning to me.

Mark
# 9  
Old 09-18-2016
Hi,

If fields are fixed and if it is always same day , you can try this :

Code:
 awk '{split($2,a,":");sub(/\,/,"",$NF);split($NF,b,":");if ( (a[3]-b[3])>10) { printf "%s ",$0","; }}' inputfile

# 10  
Old 09-18-2016
Greet_sed,

That worked perfectly except that it put them all on one line.
Is there a way to separate them out one per line?

Mark
# 11  
Old 09-18-2016
Quote:
Originally Posted by Markham
Greet_sed,

That worked perfectly except that it put them all on one line.
Is there a way to separate them out one per line?

Mark
Hi Mark,
I'm glad that you think greet_sed's script did what you want, but it seems a little bit too simple to me. If we take a slightly different sample file (expanded from your example in post #1 in this thread:
Code:
2016-09-17 19:30:57  INFO: [D3B4AEB3] id: 4562079216, time: 2016-09-17 19:30:41,
2016-09-17 12:02:26  INFO: [D3B4AEB3] id: 4562079193, time: 2016-09-17 12:02:25,
2016-09-17 19:31:57  INFO: [D3B4AEB3] id: 4562079300, time: 2016-09-17 19:30:57,
2016-09-17 20:30:57  INFO: [D3B4AEB3] id: 4562079301, time: 2016-09-17 19:30:57,
2016-09-17 19:30:07  INFO: [D3B4AEB3] id: 4562079302, time: 2016-09-17 19:20:58,
2016-09-17 19:40:01  INFO: [D3B4AEB3] id: 4562079302, time: 2016-09-17 19:39:50,

We see that the code suggested by Scrutinizer in post #3 in this thread produces the output:
Code:
2016-09-17 19:30:57  INFO: [D3B4AEB3] id: 4562079216, time: 2016-09-17 19:30:41,
2016-09-17 19:31:57  INFO: [D3B4AEB3] id: 4562079300, time: 2016-09-17 19:30:57,
2016-09-17 20:30:57  INFO: [D3B4AEB3] id: 4562079301, time: 2016-09-17 19:30:57,
2016-09-17 19:30:07  INFO: [D3B4AEB3] id: 4562079302, time: 2016-09-17 19:20:58,
2016-09-17 19:40:01  INFO: [D3B4AEB3] id: 4562079302, time: 2016-09-17 19:39:50,

(with each input line output on a separate line) which seems to me to be correct.

The code greet_sed suggested, however, only produces the output:
Code:
2016-09-17 19:30:57 INFO: [D3B4AEB3] id: 4562079216, time: 2016-09-17 19:30:41,

with no line terminator. Note that it also converts all sequences of multiple blanks on an input line to a single space on output lines (in this case changing the two spaces before INFO to a single space). I don't know if this will matter to whatever will be looking at your output.

You can't just look at the seconds field to determine if two timestamps are within ten seconds of each other. As shown above, greet_sed's code does not detect when the start and end times are one minute apart, one hour apart, or even eleven seconds apart if the two times are not in the same minute.

Now that we know that all timestamps in your input data will occur on a single date, we can slightly simplify Scrutinizer's code and get the same results:
Code:
awk '
{	# Split end time field into hours, minutes, and seconds.
	split($2, time, ":")
	# Convert hour, minutes, and seconds to seconds since midnight.
	end_time = time[1] * 3600 + time[2] * 60 + time[3]
	# Split start time field into hours, minutes, and seconds.
	split($NF, time, ":")
	# Convert hour, minutes, and seconds to seconds since midnight.
	start_time = time[1] * 3600 + time[2] * 60 + time[3]
}
# If the end time is more than ten seconds after the start time, print the line.
end_time - start_time > 10
' Example

# 12  
Old 09-18-2016
Don,

I appreciate you trying to assist, but you might as well provide the theories of Relativity as well because everything you said makes about as much sense to me.

I respectfully appreciate that you are trying to explain it, but i lost you after "Hi Mark".
# 13  
Old 09-18-2016
Quote:
Originally Posted by Markham
Don,

I appreciate you trying to assist, but you might as well provide the theories of Relativity as well because everything you said makes about as much sense to me.

I respectfully appreciate that you are trying to explain it, but i lost you after "Hi Mark".
Mark,
OK. Let me simplify it... The code greet_sed suggested does not work. The code Scrutinizer suggested in post #3 in this thread does work.

Now that we know that all all of your timestamps are on a single date, we can simplify Scrutinizer's code (and strip out the comments since you apparently don't want an explanation of how anything works and don't want to be distracted by our attempts to help you) to just:
Code:
awk '
{	split($2, t, ":")
	e = t[1] * 3600 + t[2] * 60 + t[3]
	split($NF, t, ":")
	s = t[1] * 3600 + t[2] * 60 + t[3]
}
e - s > 10
' Example

I sincerely apologize for trying to help you understand that greet_sed's code does not work and for showing you examples that show failures in the output greet_sed's code produces.
# 14  
Old 09-18-2016
Don,

I appreciate all the effort that you went to and I now see that his code did not work.

I tried your last suggestion and it is getting results for times less than 10 seconds.

I must be doing something wrong.

Mark
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Problem with implementing the times() function in C (struct tms times return zero/negative values)

Hello, i'm trying to implement the times() function and i'm programming in C. I'm using the "struct tms" structure which consists of the fields: The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime structure... (1 Reply)
Discussion started by: g_p
1 Replies

2. HP-UX

grep for x for m times ',\{11\}'

All: OS version HP-UX ga016a501 B.11.31 U ia64 from the command prompt -grep for 1 to 11 occurences of "," returns both rows from the command prompt -grep for 11 occurences of "," returns 0 rows - should be 1 row. Any ideas - why? ga016a501 -> grep ',\{1,11\}' test3 | more ... (7 Replies)
Discussion started by: Bill L.
7 Replies

3. Shell Programming and Scripting

perl newbie . &&..programming newbie

Hi, I am new to programming and also to perl..But i know 'perl' can come to my rescue, But I am stuck at many places and need help..any small help is much appreciated... below is the description of what i intend to acheive with my script. I have a files named in this format... (13 Replies)
Discussion started by: xytiz
13 Replies

4. Shell Programming and Scripting

perl newbie . &&..programming newbie (question 2)

Hello everyone, I am having to do a lot of perl scripting these days and I am learning a lot. I have this problem I want to move files from a folder and all its sub folders to one parent folder, they are all .gz files.. there is folder1\folder2\*.gz and there are about 50 folders... (1 Reply)
Discussion started by: xytiz
1 Replies

5. UNIX for Dummies Questions & Answers

UNIX newbie NEWBIE question!

Hello everyone, Just started UNIX today! In our school we use solaris. I just want to know how do I setup Solaris 10 not the GUI one, the one where you have to type the commands like ECHO, ls, pwd, etc... I have windows xp and I also have vmware. I hope I am not missing anything! :p (4 Replies)
Discussion started by: Hanamachi
4 Replies

6. UNIX for Dummies Questions & Answers

Newbie Help with Grep or Awk .. Easy one ...

I have this output: uniquemember=uid=315kthatch,ou=people,ou=client315,dc=paisleyhosting,dc=com and i want the output to be just this: 315kthatch I need it to be generic tho, because I have hundreds of lines of output, and the preceding numbers are not always 315. So I would need... (3 Replies)
Discussion started by: kthatch
3 Replies

7. AIX

how would you know your server was rebooted 3 times or 5 times

Is there such location or command to know how many times did you reboot your server in that particular day?in AIX. (3 Replies)
Discussion started by: kenshinhimura
3 Replies

8. UNIX for Dummies Questions & Answers

Simple newbie grep question

How come grep testfile1 won't find anything in testfile1 (even though the characters sd are there in great quantity), but grep '' testfile1 will find plenty? Do the single quotes prevent the shell from interpreting the testfile1 is interpreted as: grep *test whether or not characters sd exist*... (5 Replies)
Discussion started by: doubleminus
5 Replies

9. Shell Programming and Scripting

GREP Searching for a newbie...

Hi, I really need some help with GREP searching... I need to find all occurances of a file reference and remove two characters from the end of the reference. For example, here are a few lines showing the text: <image file="STRAIGHT_004CR.jpg" ALT="STRAIGHT_004CR.jpg" /> <image... (8 Replies)
Discussion started by: steveglevin
8 Replies

10. AIX

grep to give how many times each lines were found

Lets say I have a file containing string patterns to be looked for inside a file. I would normaly do : grep -if MyFilePattern FiletoSearchInto if I use the -c it gives how many total lines it found out of my whole pattern file, but what if i want grep to report how many times it found each... (4 Replies)
Discussion started by: Browser_ice
4 Replies
Login or Register to Ask a Question