Help with the idea of a script to modify time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with the idea of a script to modify time
# 1  
Old 10-05-2010
Question Help with the idea of a script to modify time

Hello friends,
I need an idea or a ready solution for a problem
i have the following lines in text file:

1 20100920140122 object4 MOVE IN
2 20100920150012 object4 MOVE OUT
-- cut --
the second column is the date and time:
20100920140122 = 2010 09 20 14:01.22
what I need to do is to add 40 seconds to each line so
20100920140122 should be 20100920140202

Your help is highly appreciated !!!

Regards
Peter N.
# 2  
Old 10-05-2010
bash + GNU date

bash code:
  1. #!/bin/bash
  2. S2D ()  {   date -u --date "19700101 $1 sec" "+%Y-%m-%d %T";} # Converts timestamp to readable date
  3. while read A B C
  4. do
  5.    T=$(date -d "${B::8} ${B:8:2}:${B:10:2}:${B:12:2}" +%s)
  6.    echo "$A $(date -d "$(S2D $((T+40)))" +%Y%m%d%H%M%S) $C"
  7. done <file
Don't be afraid by the syntx colors : the quotes inside command substitution are correctly interprated.
if you want to write to a file, just redirect the ouput of the loop (line 7.) like
Code:
done <file >newfile

This User Gave Thanks to frans For This Post:
# 3  
Old 10-05-2010
frans !
Thank you very much, u saved me a lot of brain usage.
I just googled and found about --date option then I read your message.
god bless you ! Smilie
# 4  
Old 10-05-2010
FYI: man date and info date
# 5  
Old 10-05-2010
long way, but still showing :

Code:
use strict;
use warnings;
use Date::Calc qw(Add_Delta_DHMS);

open (FH, '<file') or die "unable to open file: $!";

while (<FH>) {
    chomp;                  
     my ($f1, $_timeStamp, $_remaining) = split(/ /,$_,3);
	 my @_charSet = split (//,$_timeStamp);
	my ($year, $month, $day, $hh, $mm, $ss) = Add_Delta_DHMS(
							$_charSet[0].$_charSet[1].$_charSet[2].$_charSet[3],   # year
							$_charSet[4].$_charSet[5],   # month			
							$_charSet[6].$_charSet[7],   # days
							$_charSet[8].$_charSet[9],   # hrs
							$_charSet[10].$_charSet[11], # mins
							$_charSet[12].$_charSet[13], # # secs
							0, 0, 0, 40); # 0 days, 0 hrs, 0 min, 40 sec
    $_timeStamp = sprintf("%04d%02d%02d%02d%02d%02d", $year,$month,$day,$hh,$mm,$ss);	 
   print "$f1 $_timeStamp $_remaining\n";
} 
close FH;

This User Gave Thanks to clx For This Post:
# 6  
Old 10-05-2010
@frans

Could you pls explain the below part of the code

Code:
date -u --date "19700101 $1 sec" "+%Y-%m-%d %T"

Code:
T=$(date -d "${B::8} ${B:8:2}:${B:10:2}:${B:12:2}" +%s)

# 7  
Old 10-05-2010
Quote:
Originally Posted by michaelrozar17
Could you pls explain the below part of the code
Code:
date -u --date "19700101 $1 sec" "+%Y-%m-%d %T"

That adds $1 (the parameter passed to the function) seconds to the initial date (start point) of Unix timestamp (which is 1970-01-01). That function is used to convert timestamp to a readable date.
-u stands for UTC time
--date option is longname of -d (i could write -d instead).

Code:
T=$(date -d "${B::8} ${B:8:2}:${B:10:2}:${B:12:2}" +%s)

Shell expansion ${<variable>:<start>:<length>} returns a <length> string from <start> character (0 is the first) from <variable>.
0's can be ommited so the ${B::8} is like ${B:0:8}
That is used to extract dat from original date (which isn't recognized by the date command) and the +%s is the format of the Unix timestamp. So it converts the date in the file to timestamp (which is needed for calculation).

Hope this helps.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

AIX idea needed to check the logs updated date and time

Hi with the help of Gabriel canepa, i have just edited filename only in his code. The help which i got and he helped is 1) I have around 22 logs and each log should be updated in the last 24 hours from the current timestamp. 2) It should check for ERROR message (not error,Error) in the log and... (2 Replies)
Discussion started by: Kalaihari
2 Replies

2. Shell Programming and Scripting

I have no idea what is wrong with my simple script.

I am doing a simple "recycle bin" script. It is to check and see if the directory .TRASH exists within the home directory. If not then it makes the directory then appends a date and time to file and then finally moves the file in. However, when I run this script, it is not making the directory as... (5 Replies)
Discussion started by: iamdeman
5 Replies

3. Shell Programming and Scripting

Modify different files at the same time

Hi, I'm having the following problem. I have some files to modify, between a large number of files. I thought the following code aux2=`grep -l 2.2.17 *` print "$aux2" aux3=`ls -l *.ksh | wc -l` print "$aux3" while ; do print "The counter is $aux3" #Add the sed here ... (3 Replies)
Discussion started by: radicaled
3 Replies

4. Shell Programming and Scripting

need a new idea for a script

Hi all, I now have project in UNIX Solaris and I want to have some new ideas to execute it, so I hope you help me finding new ideas in scripting or some infrastructure .bye (1 Reply)
Discussion started by: hard_revenge
1 Replies

5. Shell Programming and Scripting

Bash script idea using cUrl -- possible?

I have an alias already in my .bash_profile to download files using cUrl's -o (output to file, user provides the file name) option. I find I'm using it quite a bit, so I wanted to write a script to run "curl -o", taking the necessary inputs - file name and URL from which to download - and then... (3 Replies)
Discussion started by: SilversleevesX
3 Replies

6. Shell Programming and Scripting

List files and display last modify time in a particular format

hi everyone, can someone suggest how i can list the contents of a directory and display their corresponding last modify time in the format yyyymmddhhmm? thanks in advance! (16 Replies)
Discussion started by: Deanne
16 Replies

7. Shell Programming and Scripting

Script Idea's / Help

Hi there, I'm pretty new to scripting and wondering if anyone had any idea's, scripts or snippets on how I can do the following. I basically want a shell script that will look at all the files in a directory and find all the names and addresses in them then output them to the screen nicely... (12 Replies)
Discussion started by: mrpugster
12 Replies

8. Shell Programming and Scripting

idea for script - cheking passwords

Hi All, I am looking for scripts where i need check normal user password and root password for more 100 servers from single server...! let me explin it what exacltly i need...! i need to do password audit for more than 600 boxes... :o for one normal user and root password also...... (5 Replies)
Discussion started by: bullz26
5 Replies

9. Shell Programming and Scripting

Limitations of awk? Good idea? Bad idea?

Keeping in mind that I'm relatively comfortable with programming in general but very new to unix and korn/bourne shell scripts.. I'm using awk on a CSV file, and then performing calculations and operations on specific fields within specific records. The CSV file I'm working with has about 600... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question