1752806 Members
5690 Online
108789 Solutions
New Discussion

Help needed

 
SOLVED
Go to solution
hamedel-azzamy
Occasional Contributor

Help needed

Hello,

 

  i'm writing a script but i need your support as follow:

 

I have a file as follow  :

 

20141219232332 QL-KHARKANIA-3G--01 warning
20141219102332 QL-KHARKANIA-3G-03 warning
20141219152334 QL-KHARKANIA-3G-02 warning
20141219112341 Kafr-Hamam-3G-01 warning
20141219162341 Kafr-Hamam-3G-02 warning
20141219202341 Kafr-Hamam-3G-03 warning

............................

...........................

..........................etc

 

the first coloum is represent the date but in string formate(year month day hour min second) and in UTC time and in need to add 2 hour to meet my local time...what i need from you that can any help on change this formate to be in format of (year/Month/day hour/min/sec) and replace it in the same location and to be my loacal time as add 2 hour to itas follow.

 

2014/12/20 01:23:32 QL-KHARKANIA-3G--01 warning
2014/12/19 12:23:32 QL-KHARKANIA-3G-03 warning
2014/12/19 17:23:34 QL-KHARKANIA-3G-02 warning
2014/12/19 13:23:41 Kafr-Hamam-3G-01 warning
2014/12/19 19:23:41 Kafr-Hamam-3G-02 warning
2014/12/19 22:23:41 Kafr-Hamam-3G-03 warning

 

Thanks

 

6 REPLIES 6
hamedel-azzamy
Occasional Contributor

Re: Help needed

the script is under HP-UX not linux

Dennis Handly
Acclaimed Contributor
Solution

Re: Help needed (formating timestamp)

Something like:

awk '
BEGIN {
   days[1] = 31; days[2] = 28; days[3] = 31; days[4] = 30
   days[5] = 31; days[6] = 30; days[7] = 31; days[8] = 31
   days[9] = 30; days[10] = 31; days[11] = 30; days[12] = 31
}
{
ts = substr($0, 1, 14)
rest = substr($0, 15)
yy = substr(ts, 1, 4)
mm = substr(ts, 5, 2)
dd = substr(ts, 7, 2)
hh = substr(ts, 9, 2)
MM = substr(ts, 11, 2)
ss = substr(ts, 13, 2)

# Add 2 hours for local time
hh += 2


# Handle carries
if (hh >= 24) {
   hh -= 24
   ++dd
   # convert mm to number
   mm += 0
   dom = days[mm]
   # Handle leap years
   if (mm == 2 && yy % 4 == 0 && (yy % 100 != 0 || yy % 400 == 0)) {
#      print "leap year:", yy
      dom = 29
   }
   if (dd > dom) {
      dd = 1
      ++mm
      if (mm > 12) {
         mm = 1
         ++yy
      }
   }
}

printf "%d/%.2d/%.2d %.2d:%.2d:%2d%s\n", yy, mm, dd, hh, MM, ss, rest
}' input-file

hamedel-azzamy
Occasional Contributor

Re: Help needed (formating timestamp)

dude...you are brilliant it working....as expected...thanks and appreicate...

Dennis Handly
Acclaimed Contributor

Re: Help needed (formating timestamp)

>thanks and appreciate

 

If you're happy with the answer, please click on the kudos thumbs-up.

 

I noticed I made a mistake in the leap year check for 2000 and next in 2400.  It should be:

if (mm == 2 && yy % 4 == 0 && (yy % 100 != 0 || yy % 400 == 0)) {

Patrick Wallek
Honored Contributor

Re: Help needed (formating timestamp)

 >>I noticed I made a mistake in the leap year check for 2000 and next in 2400

 

Really?!  You're that concerned about the leap year check for the year 2400?  ;)

 

 

Dennis Handly
Acclaimed Contributor

Re: Help needed (formating timestamp)

>You're that concerned about the leap year check for the year 2400?  ;)

 

Having worked on Y2K and Y2K7, I didn't want to be hung in effigy nor kicked out of those new-fangled flying cars.  :-)