Grepping a timestamp range and assigning it to a constant


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping a timestamp range and assigning it to a constant
# 1  
Old 03-24-2017
Grepping a timestamp range and assigning it to a constant

Hi,

I couldn't find any thing on google about it and have been trying to figure this out but am not getting anywhere. I want to know if its possible through a script. I have a file with columns start time and end time separated by a comma, basically there are some other columns which I need to work on, but first I wanna get this time thing sorted. Coming back to the question:

Can the start time and end time be grouped and then assigned to a constant, say CUT1, CUT2, etc or it better to do this manually?

For eg, a time between 15 - 17 is CUT 1, 18-20 is CUT 2 and 00-03 is CUT 3

Code:
START_TIME,END_TIME
16:17:10,16:24:48
16:17:10,16:17:19
16:17:11,16:17:13
16:17:14,16:23:13
16:17:18,16:26:40
16:17:22,16:22:58
16:18:30,16:25:40
16:18:38,16:23:51
16:18:53,16:25:19
16:19:07,16:19:10
16:19:11,16:26:43
16:19:47,16:19:51
16:19:54,16:29:43
16:20:02,16:20:11
16:20:04,16:20:19
16:20:06,16:20:16
00:47:31,00:47:34
00:47:37,00:48:05
00:47:43,00:57:26
00:47:43,00:48:03
00:47:48,00:55:38
00:47:49,00:47:57
16:20:08,16:20:20
16:20:10,16:21:08
16:20:12,16:20:31
18:30:54,18:32:57
18:31:56,18:32:09
18:32:08,18:32:14
18:32:17,18:32:22
01:55:32,02:04:02

Thank you
# 2  
Old 03-24-2017
huh??
# 3  
Old 03-24-2017
Smilie I knew I was not stating the correct requirements. anyway! thanks for at least taking the time to read it.

the original file delimited by a ','. It has columns ORIGINAL_FILE_NAME,START_TIME,END_TIME
Code:
pvis_POB_80byte_mccy_1303170025574.inc,16:17:10,16:24:48
pvis_POU_80byte_mccy_1303172240000.inc,00:47:31,00:47:34
pvis_MAC_80byte_mccy_1303171715074.inc,18:31:56,18:32:09


this is the format that I am expecting. If you notice, I have replaced the 2nd and 3rd column with CUT 1/CUT 2/CUT 3
Code:
pvis_POB_80byte_mccy_1303170025574.inc,CUT 1
pvis_POU_80byte_mccy_1303172240000.inc,CUT 3
pvis_MAC_80byte_mccy_1303171715074.inc,CUT 2

I was actually going through a sed tutorial about the search and replace function and was wondering if I do a search stating that if the 2nd column(delimited by comma) falls between 15:00:00-17:00:00 replace it with a CUT 1, similarly 18:00:00-21:00:00 and the same for 00:00:00-03:00:00 , replace it with CUT 3. Once that is done, I will pipe it to a awk substr on the 1st column with '_' as the delimiter and get the 1st and 2nd field of the 1st column of the filename.

What I ultimately want is:
Code:
pvis_POB CUT 1
pvis_POU CUT 3
pvis_MAC CUT 2

Hope I am making sense now
# 4  
Old 03-24-2017
Unfortunately, your relation between start hour and the CUT No. is not linear, else this might work
Code:
awk -F, '{sub (/_[^_]*_[^_]*_[^_]*$/, _, $1); split ($2, T, ":"); print $1, "CUT", int ((T[1]+21)%24 /3)-3} ' file
pvis_POB CUT 1
pvis_POU CUT 4
pvis_MAC CUT 2

# 5  
Old 03-24-2017
Why not just use awk to do all of that in the first place? Unlike sed, awk actually has a proper 'greater than' operator. Even better, it has variables and math, so it can actually calculate which range its in.

Code:
$ cat inputfile

junk,00:00:00,00:00:00
junk,00:30:00,00:00:00
junk,01:00:00,00:00:00
junk,01:30:00,00:00:00
junk,02:00:00,00:00:00
junk,02:30:00,00:00:00
junk,03:00:00,00:00:00
junk,03:30:00,00:00:00
junk,04:00:00,00:00:00
junk,04:30:00,00:00:00
junk,05:00:00,00:00:00
junk,05:30:00,00:00:00
junk,06:00:00,00:00:00
junk,06:30:00,00:00:00
junk,07:00:00,00:00:00
junk,07:30:00,00:00:00
junk,08:00:00,00:00:00
junk,08:30:00,00:00:00
junk,09:00:00,00:00:00
junk,09:30:00,00:00:00
junk,10:00:00,00:00:00
junk,10:30:00,00:00:00
junk,11:00:00,00:00:00
junk,11:30:00,00:00:00
junk,12:00:00,00:00:00
junk,12:30:00,00:00:00
junk,13:00:00,00:00:00
junk,13:30:00,00:00:00
junk,14:00:00,00:00:00
junk,14:30:00,00:00:00
junk,15:00:00,00:00:00
junk,15:30:00,00:00:00
junk,16:00:00,00:00:00
junk,16:30:00,00:00:00
junk,17:00:00,00:00:00
junk,17:30:00,00:00:00
junk,18:00:00,00:00:00
junk,18:30:00,00:00:00
junk,19:00:00,00:00:00
junk,19:30:00,00:00:00
junk,20:00:00,00:00:00
junk,20:30:00,00:00:00
junk,21:00:00,00:00:00
junk,21:30:00,00:00:00
junk,22:00:00,00:00:00
junk,22:30:00,00:00:00
junk,23:00:00,00:00:00
junk,23:30:00,00:00:00

$ awk -F"," -v OFS="," '{
        split($2, A, ":");
        sub(/^0/, "", A[1]); # Remove leading zero
        A[1] += 0; # Convert from string into number
        $2=sprintf("CUT %d", (A[1] / 3) + 1); # Calculate and truncate to integer
        NF=2 # Cut off third field
} 1' inputfile

junk,CUT 1
junk,CUT 1
junk,CUT 1
junk,CUT 1
junk,CUT 1
junk,CUT 1
junk,CUT 2
junk,CUT 2
junk,CUT 2
junk,CUT 2
junk,CUT 2
junk,CUT 2
junk,CUT 3
junk,CUT 3
junk,CUT 3
junk,CUT 3
junk,CUT 3
junk,CUT 3
junk,CUT 4
junk,CUT 4
junk,CUT 4
junk,CUT 4
junk,CUT 4
junk,CUT 4
junk,CUT 5
junk,CUT 5
junk,CUT 5
junk,CUT 5
junk,CUT 5
junk,CUT 5
junk,CUT 6
junk,CUT 6
junk,CUT 6
junk,CUT 6
junk,CUT 6
junk,CUT 6
junk,CUT 7
junk,CUT 7
junk,CUT 7
junk,CUT 7
junk,CUT 7
junk,CUT 7
junk,CUT 8
junk,CUT 8
junk,CUT 8
junk,CUT 8
junk,CUT 8
junk,CUT 8

$

# 6  
Old 03-24-2017
still not sure how exactly you're doing the CUTting, but....
Code:
awk -F_ '{print $1 FS $2, "CUT " FNR}' myFile

# 7  
Old 03-24-2017
what i meant was if the start time and end time is between 3 PM to 5 PM then what gets printed on the screen is 'CUT 1', if the time is between 6PM -10PM, then what gets printed on the screen is 'CUT 2' and finally if the time is after 00:00:00 hrs till 3AM, 'CUT 3' gets printed on the screen

something like below
Code:
cat infile|sed 's/^[15-17]/CUT1/g

@Corona688 i have not used much awk so don't know a lot about it, just here n there. I might give it a look now that you have suggested, but just that sed was something that came into mind as I needed a search and replace.

@RudiC @vgersh99 My argument is that instead of numbers(time stamps) I want constant characters in place of the time stamp so that I can sort and count it. Because of the different time stamps I am unable to sort them

hope I am clear
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep lines between last hour timestamp and current timestamp

So basically I have a log file and each line in this log file starts with a timestamp: MON DD HH:MM:SS SEP 15 07:30:01 I need to grep all the lines between last hour timestamp and current timestamp. Then these lines will be moved to a tmp file from which I will grep for particular strings. ... (1 Reply)
Discussion started by: nms
1 Replies

2. Homework & Coursework Questions

IP Range Assigning

THIS IS A SAMPLE PRACTICAL EXAM QUESTION, COMPLETE FILE HAS BEEN ATTACHED AS WELL. Hi All, I'm a bit confused about assigning IP address from IP Ranges. I am using this scenario below to understand. Scenario Adatum.com an international IT solutions company, is launching 12 new branches in a... (10 Replies)
Discussion started by: TryllZ
10 Replies

3. IP Networking

IP Range Assigning

Hi All, I'm a bit confused about assigning IP address from IP Ranges. I am using this scenario below to understand. Scenario Adatum.com an international IT solutions company, is launching 12 new branches in a new country where they currently have no existing branches. The sWin CIO has asked... (3 Replies)
Discussion started by: TryllZ
3 Replies

4. Shell Programming and Scripting

AIX : Need to convert UNIX Timestamp to normal timestamp

Hello , I am working on AIX. I have to convert Unix timestamp to normal timestamp. Below is the file. The Unix timestamp will always be preceded by EFFECTIVE_TIME as first field as shown and there could be multiple EFFECTIVE_TIME in the file : 3.txt Contents of... (6 Replies)
Discussion started by: rahul2662
6 Replies

5. Shell Programming and Scripting

To check timestamp in logfile and display lines upto 3 hours before current timestamp

Hi Friends, I have the following logfile. Currently time in india is 07/31/2014 12:33:34 and i have the following content in logfile. I want to display only those entries which contain string 'Exception' within last 3 hours. In this case, it would be the last line only I can get the... (12 Replies)
Discussion started by: srkmish
12 Replies

6. Programming

grepping a range of values

I need to return all records in a file starting with a row that says TABLE: <tabl name> lists of hexadecimal records TABLE: <some table> TABLe is a key word in the file. I know the name of the table I want to start with. I do not know the name of the table that I will end with. I just... (4 Replies)
Discussion started by: guessingo
4 Replies

7. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies
Login or Register to Ask a Question