Avatar of libertyforall2
libertyforall2Flag for United States of America asked on

Changing a date stamp in Unix/linux within a file

Ok. I need to change ALL the date & hour stamps within a file. I have attached a sample file. I only want the date and times in column 1 (Jday) through column 7 (HR2) modified. I will then later modify the script in Perl. Unfortunately the Perl script requires a special module to be installed which I am having difficulty with. In Unix it is a simple thing to switch from UTC to local time but I need it to replace the current time stamp.  How would I do this? Again, the main date & time is column 1 (Jday) with the date and column (HR2). These need to be shifted back to local time HST which is always 10 hours earlier (no daylight savings time in Hawaii). So I could 1.  delete columns 2-5. 2. Read each row of data for columns Jday & HR2, then 3, change the date stamp & hour to local time.  For example, I look at three lines in the attached file,

12/03/2010 10 12 3 2 3 3 0.00 0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00
12/03/2010 10 12 3 5 3 6 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.02 0.00
12/03/2010 10 12 3 8 3 9 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

I would 1st delete the unwanted columns and it would look like this

12/03/2010  3 0.00 0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00
12/03/2010  6 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.02 0.00
12/03/2010  9 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

I would then change to time to look like this

12/02/2010  17 0.00 0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00
12/02/2010  20 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.02 0.00
12/02/2010  23 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

But this would be done for all lines of data in the file except the 1st line which names the columns
samplefile1.txt
Linux NetworkingLinux OS DevShell Scripting

Avatar of undefined
Last Comment
libertyforall2

8/22/2022 - Mon
TRW-Consulting

sed -n '2,$p' samplefile1.txt |
  while read jday yr mo da1 hr1 da2 hr2 and_the_rest
  do
    newtime=`date -d "$jday $hr2:00:00 10 hours ago" +'%m/%d/%Y %H'`
    echo "$newtime $and_the_rest"
  done
ASKER
libertyforall2

It did not quite work. The input file data looked like this

JDAY  YR  MO DA1 HR1 DA2 HR2 S00000037 S00000021 S00000002 S00000004 S00000034 S00000035 S00000075 S00000038 S00000044 S00000045 S00000046
12/06/2010 10 12 6 2 6 3 0 0 0 0 3 0 0 0 0 0 0
12/06/2010 10 12 6 5 6 6 0 0 0 0 2 0 0 0 0 0 0
12/06/2010 10 12 6 8 6 9 0 0 0 0 0 0 0 0 0 1 0
12/06/2010 10 12 6 11 6 12 0 0 3 0 3 0 0 0 0 1 0
12/06/2010 10 12 6 14 6 15 0 0 12 7 1 0 0 0 0 1 0
12/06/2010 10 12 6 17 6 18 0 0 0 1 1 0 0 0 0 11 0
12/06/2010 10 12 6 20 6 21 3 5 4 3 1 0 0 0 0 0 0
12/06/2010 10 12 6 23 7 0 0 0 29 1 0 0 0 0 0 8 0
12/07/2010 10 12 7 2 7 3 0 0 13 4 0 0 0 0 0 0 0
12/07/2010 10 12 7 5 7 6 0 0 4 6 0 0 1 0 0 0 0
12/07/2010 10 12 7 8 7 9 0 0 3 13 0 0 0 0 0 0 0
12/07/2010 10 12 7 11 7 12 0 0 9 10 0 0 19 0 0 0 0
12/07/2010 10 12 7 14 7 15 0 0 1 0 5 0 0 0 0 0 0
12/07/2010 10 12 7 17 7 18 0 0 1 0 1 0 0 0 0 0 0
12/07/2010 10 12 7 20 7 21 0 0 5 0 0 0 0 0 0 0 0
12/07/2010 10 12 7 23 8 0 0 0 16 1 0 0 0 0 4 5 0
12/08/2010 10 12 8 2 8 3 0 0 35 2 0 0 0 2 0 0 0
12/08/2010 10 12 8 5 8 6 0 0 11 4 0 0 0 0 0 0 0
12/08/2010 10 12 8 8 8 9 0 0 96 2 0 0 0 6 0 0 0
12/08/2010 10 12 8 10 8 11 0 0 1 32 0 0 0 0 0 0 0


The output data looked like this.


[rhuff@huina ~/test]$ sh timestamp.sh
12/05/2010 17 0 0 0 0 3 0 0 0 0 0 0
12/05/2010 20 0 0 0 0 2 0 0 0 0 0 0
12/05/2010 23 0 0 0 0 0 0 0 0 0 1 0
12/06/2010 02 0 0 3 0 3 0 0 0 0 1 0
12/06/2010 05 0 0 12 7 1 0 0 0 0 1 0
12/06/2010 08 0 0 0 1 1 0 0 0 0 11 0
12/06/2010 11 3 5 4 3 1 0 0 0 0 0 0
12/05/2010 14 0 0 29 1 0 0 0 0 0 8 0
12/06/2010 17 0 0 13 4 0 0 0 0 0 0 0
12/06/2010 20 0 0 4 6 0 0 1 0 0 0 0
12/06/2010 23 0 0 3 13 0 0 0 0 0 0 0
12/07/2010 02 0 0 9 10 0 0 19 0 0 0 0
12/07/2010 05 0 0 1 0 5 0 0 0 0 0 0
12/07/2010 08 0 0 1 0 1 0 0 0 0 0 0
12/07/2010 11 0 0 5 0 0 0 0 0 0 0 0
12/06/2010 14 0 0 16 1 0 0 0 0 4 5 0
12/07/2010 17 0 0 35 2 0 0 0 2 0 0 0
12/07/2010 20 0 0 11 4 0 0 0 0 0 0 0
12/07/2010 23 0 0 96 2 0 0 0 6 0 0 0
12/08/2010 01 0 0 1 32 0 0 0 0 0 0 0

As you can see, it deleted the first line which should remain as is. More importantly, it seemed to not work on certain lines because the day was incorrect. It looks like it  did not change the days correctly for hours 10 or less.
TRW-Consulting

If you want the first line, then add it:

sed -n '1p' samplefile1.txt
sed -n '2,$p' samplefile1.txt |
  while read jday yr mo da1 hr1 da2 hr2 and_the_rest
  do
    newtime=`date -d "$jday $hr2:00:00 10 hours ago" +'%m/%d/%Y %H'`
    echo "$newtime $and_the_rest"
  done


And you need to show me where it's not taking into consideration hours less than 10, because it looks like it's working perfectly fine to me.  For example, the first record is 12/6/2010 at 3 AM.  Subtract 10 hours from that at you would get 5 PM, which is 17:00, and you can see it is showing 12/5/2010 with the hour as 17.  How is that incorrect?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
libertyforall2

If you look at line
2/06/2010 11 3 5 4 3 1 0 0 0 0 0 0
12/05/2010 14 0 0 29 1 0 0 0 0 0 8 0

and
12/07/2010 11 0 0 5 0 0 0 0 0 0 0 0
12/06/2010 14 0 0 16 1 0 0 0 0 4 5 0

You see that it should read 12/06/2010 14 0 0 29 1 0 0 0 0 0 8 0
and 12/07/2010 14 0 0 16 1 0 0 0 0 4 5 0

Since all the lines increase by 3 hour increments.
TRW-Consulting

I think I see what's happening.  Midnight is shown as 0 and I'm not taking that into account. So I've added a line here to take care of that:


sed -n '1p' samplefile1.txt
sed -n '2,$p' samplefile1.txt |
  while read jday yr mo da1 hr1 da2 hr2 and_the_rest
  do
    [ $hr2 -eq 0 ] && jday=`date -d "$jday + 1 day" +'%m/%d/%Y'`
    newtime=`date -d "$jday $hr2:00:00 10 hours ago" +'%m/%d/%Y %H'`
    echo "$newtime $and_the_rest"
  done

Open in new window

ASKER
libertyforall2

Ok. Almost. The only issue is that the top line is exactly the same when YR  MO DA1 HR1 DA2 should be been deleted to correspond with the change in columns from the data below the top line. The top line should look like this (all one line if there was enough space in the comment box)
JDAY  HR2 S00000037 S00000021 S00000002 S00000004 S00000034 S00000035 S00000075 S00000038 S00000044 S00000045 S00000046
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
TRW-Consulting

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
libertyforall2

Great! This works. Please look at a related question I just posted. Thanks.

https://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_26667273.html
ASKER
libertyforall2

Great!