How to add missing date and time in a bash script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add missing date and time in a bash script?
# 1  
Old 01-13-2016
How to add missing date and time in a bash script?

Hi Again,

I have a file that contains date and time for the past 2 hours. What i need is add missing date and time in a file.

INPUT
Code:
2016-01-13 01:33 10
2016-01-13 01:31 10
2016-01-13 01:30 10
2016-01-13 01:29 10
2016-01-13 01:28 10
2016-01-13 01:27 10
2016-01-13 01:26 10
2016-01-13 01:06 10
2016-01-13 01:05 10
2016-01-13 01:04 10
2016-01-13 01:03 10
2016-01-13 01:02 10
2016-01-13 01:00 10
2016-01-13 00:59 10
2016-01-13 00:58 10
2016-01-13 00:57 10
2016-01-13 00:56 10
2016-01-13 00:55 10
2016-01-13 00:54 10
2016-01-13 00:53 10
2016-01-13 00:52 10
2016-01-13 00:51 10
2016-01-13 00:50 10
2016-01-13 00:49 10
2016-01-13 00:48 10
2016-01-13 00:47 10
2016-01-13 00:46 10
2016-01-13 00:45 10
2016-01-13 00:44 10
2016-01-13 00:43 10
2016-01-13 00:33 10
2016-01-13 00:30 10
2016-01-13 00:29 10
2016-01-13 00:28 10
2016-01-13 00:27 10
2016-01-13 00:26 10
2016-01-13 00:25 10
2016-01-13 00:24 10
2016-01-13 00:23 10
2016-01-13 00:22 10
2016-01-13 00:21 10
2016-01-13 00:20 10
2016-01-12 21:33 10

OUTPUT
Code:
2016-01-13 01:33 10
2016-01-13 01:31 10
2016-01-13 01:30 10
2016-01-13 01:29 10
2016-01-13 01:28 10
2016-01-13 01:27 10
2016-01-13 01:26 10
2016-01-13 01:25 0
2016-01-13 01:24 0
2016-01-13 01:23 0
2016-01-13 01:22 0
2016-01-13 01:21 0
2016-01-13 01:20 0
2016-01-13 01:19 0
2016-01-13 01:18 0
2016-01-13 01:17 0
2016-01-13 01:16 0
2016-01-13 01:15 0
2016-01-13 01:14 0
2016-01-13 01:13 0
2016-01-13 01:12 0
2016-01-13 01:11 0
2016-01-13 01:10 0
2016-01-13 01:09 0
2016-01-13 01:08 0
2016-01-13 01:07 0
2016-01-13 01:06 10
2016-01-13 01:05 10
2016-01-13 01:04 10
2016-01-13 01:03 10
2016-01-13 01:02 10
2016-01-13 01:00 10
2016-01-13 00:59 10
2016-01-13 00:58 10
2016-01-13 00:57 10
2016-01-13 00:56 10
2016-01-13 00:55 10
2016-01-13 00:54 10
2016-01-13 00:53 10
2016-01-13 00:52 10
2016-01-13 00:51 10
2016-01-13 00:50 10
2016-01-13 00:49 10
2016-01-13 00:48 10
2016-01-13 00:47 10
2016-01-13 00:46 10
2016-01-13 00:45 10
2016-01-13 00:44 10
2016-01-13 00:43 10
2016-01-13 00:42 0
2016-01-13 00:41 0
2016-01-13 00:40 0
2016-01-13 00:39 0
2016-01-13 00:37 0
2016-01-13 00:36 0
2016-01-13 00:35 0
2016-01-13 00:34 0
2016-01-13 00:33 10
2016-01-13 00:32 0
2016-01-13 00:31 0
2016-01-13 00:30 10
2016-01-13 00:29 10
2016-01-13 00:28 10
2016-01-13 00:27 10
2016-01-13 00:26 10
2016-01-13 00:25 10
2016-01-13 00:24 10
2016-01-13 00:23 10
2016-01-13 00:22 10
2016-01-13 00:21 10
2016-01-13 00:20 10
2016-01-13 00:19 0
2016-01-13 00:18 0
2016-01-13 00:17 0
2016-01-13 00:16 0
2016-01-13 00:15 0
2016-01-13 00:14 0
2016-01-13 00:13 0
2016-01-13 00:12 0
2016-01-13 00:11 0
2016-01-13 00:10 0
2016-01-13 00:09 0
2016-01-13 00:08 0
2016-01-13 00:07 0
2016-01-13 00:06 0
2016-01-13 00:05 0
2016-01-13 00:04 0
2016-01-13 00:03 0
2016-01-13 00:02 0
2016-01-13 00:01 0
2016-01-13 00:00 0
2016-01-12 23:59 0
2016-01-12 23:58 0
2016-01-12 23:57 0
2016-01-12 23:56 0
2016-01-12 23:55 0
2016-01-12 23:54 0
2016-01-12 23:53 0
2016-01-12 23:52 0
2016-01-12 23:51 0
2016-01-12 23:50 0
2016-01-12 23:49 0
2016-01-12 23:48 0
2016-01-12 23:47 0
2016-01-12 23:46 0
2016-01-12 23:45 0
2016-01-12 23:44 0
2016-01-12 23:43 0
2016-01-12 23:42 0
2016-01-12 23:41 0
2016-01-12 23:42 0
2016-01-12 23:41 0
2016-01-12 23:40 0
2016-01-12 23:39 0
2016-01-12 23:38 0
2016-01-12 23:37 0
2016-01-12 23:36 0
2016-01-12 23:35 0
2016-01-12 23:34 0
2016-01-12 21:33 10

Thanks.
# 2  
Old 01-13-2016
Any attempts from your side?
# 3  
Old 01-13-2016
Hi Rudic,

Yes, have tried this one
Code:
awk '{split($1,N,":"); while (++L < N[1]) printf "%02d:00\t0 <--\n", L; L=N[1]} 1'

but it is not what i am looking for.

OUTPUT
Code:
[dev:~]$ awk '{split($1,N,":"); while (++L < N[1]) printf "%02d:00\t0 <--\n", L; L=N[1]} 1' test123.txt 

01:00	0 <--
02:00	0 <--
2016-01-13 11:59 0
2016-01-13 09:59 0

Need help.
# 4  
Old 01-13-2016
Try (requires a recent shell):
Code:
read FD <file
FS=$(date -d"${FD% *}" +%s)
for DT in {0..7200..60}; do printf "%(%F %H:%M)T 0\n" $(( FS - DT)); done |
awk '
NR == FNR       {T[$1,$2] = $3
                 next
                }
($1,$2) in T    {$3 = T[$1,$2]  
                 delete T[$1,$2]
                }
1
END             {for (t in T) print t, T[t]
                }   
' SUBSEP=" " file -
2016-01-13 01:33 10
2016-01-13 01:32 0
2016-01-13 01:31 10
2016-01-13 01:30 10
.
.
.
2016-01-12 23:33 0
2016-01-12 21:33 10

# 5  
Old 01-13-2016
Hi,

I am getting error.

Code:
bash: printf: `(': invalid format character
bash: printf: `(': invalid format character
bash: printf: `(': invalid format character
bash: printf: `(': invalid format character

# 6  
Old 01-13-2016
As noted: recent! shell... What be your bash version?
# 7  
Old 01-13-2016
Hi,

Here is the bash version


[dev:~]$ echo $BASH_VERSION
4.1.2(1)-release
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Find gaps in time data and replace missing time value and column 2 value by interpolation in awk

Dear all, I am kindly seeking assistance on the following issue. I am working with data that is sampled every 0.05 hours (that is 3 minutes intervals) here is a sample data from the file 5.00000 15.5030 5.05000 15.6680 5.10000 16.0100 5.15000 16.3450 5.20000 16.7120 5.25000... (4 Replies)
Discussion started by: malandisa
4 Replies

2. Shell Programming and Scripting

Add current date and time

i have file 1.txt asdas|csada|13|03|10|04|23|A1|canberra sdasd|sfdsf|13|04|26|23|28|A1|sydney i want to add today's date and time in the end of each row expected output asdas|csada|13|03|10|04|23|A1|canberra|130430|1358 sdasd|sfdsf|13|04|26|23|28|A1|sydney|130430|1358 todays date... (10 Replies)
Discussion started by: radius
10 Replies

3. Shell Programming and Scripting

Simple date and time calulation in BASH

There is a closed Thread: <url>Here will be the url to the original post once I have 5 posts in this forum...</url> But a small bug had found his way into this very cool and simple code. #!/bin/bash date2stamp () { date --utc --date "$1" +%s } stamp2date (){ date --utc --date... (2 Replies)
Discussion started by: frood
2 Replies

4. Shell Programming and Scripting

date time stamps in bash

I'm looking for a way to have the "date" command output the date in a specific format. I'm not familiar with the different ways to use the date command at all. i read up on it, but i dont get how to manipulate it. i know that i can get the date format to give me a format like: 2012-10-13... (6 Replies)
Discussion started by: SkySmart
6 Replies

5. Shell Programming and Scripting

Converting date/time and generating offsets in bash script

Hi all, I need a script to do some date/time conversion. It should take as an input a particular time. It should then generates a series of offsets, in both hour:minute form and number of milliseconds elapsed. For 03:00, for example, it should give back 04:02:07 (3727000ms*) 05:04:14... (2 Replies)
Discussion started by: emdan
2 Replies

6. Shell Programming and Scripting

add up time with xx:yy format in bash how?

Hi, How can I add up a series of string like: and get a total as hour and minutes in a format like this: (3 Replies)
Discussion started by: bashily
3 Replies

7. Shell Programming and Scripting

Bash script - im missing something and cant's figure out what

I just put together a script for work that will essentially automate the migration of our Windows fileserver to my newly created Debian based SAMBA server. My script will create the necessary directories then copy the data over to my new server, after that it will set the ACL's by using... (3 Replies)
Discussion started by: binary-ninja
3 Replies

8. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

9. Shell Programming and Scripting

Bash script error: missing destination file name operand.

Ok, i've been messing around in debian the past few days, setting up programs like subversion, mysql and logrotate. The purpose of this script is to use subversion to backup the binary logs. It runs in the cron every 2 hours or so (although I can't get my script to run properly atm, which is why... (1 Reply)
Discussion started by: cganly
1 Replies

10. Tips and Tutorials

Simple date and time calulation in BASH

The GNU date command in full of goodies but not when it comes to calculate a date or time difference. Here is what I came up with after looking to more than one solution. Code should be self explaining. #!/bin/bash date2stamp () { date --utc --date "$1" +%s } stamp2date (){ ... (0 Replies)
Discussion started by: ripat
0 Replies
Login or Register to Ask a Question