0

I am a new learner in this field. I want to subtract few seconds from date_time. I used this code to extract data and then subtract the seconds. BUT I can not save this output into a variable.

Could you please help me to save this?

for stnm in H33
   do
        cd $stnm
         for file in $input_dir/$stnm/2018/350.hyd
         echo $file
    
          do
          dat=`saclst kzdate f $file | awk '{print substr($2,1,10)}'`
          time=`saclst kztime f $file| awk '{print substr($2,1,11)}'`
          echo $dat $time "############################"
          # new_time= date -d "$(date -Iseconds -d "$dat $time" )  - 2 minutes - 0.05 seconds"
          new_time= date -d " $dat $time Z - 2 minutes - 0.05 seconds" +%Y/%m/%d_%H:%M:%S | 
              awk '{print substr($1,1,24)}'

          echo $dat $time $new_time  "####################"
         done
done

Output

/NAS2/Abbas/TS14_OBS/H33/2018/350.hyd
2018/12/16 00:00:00.00 ############################
2018/12/15_23:57:59
2018/12/16 00:00:00.00 ####################
5
  • 2
    You used command substitutions (the backticked commands) to set both dat and time, but you avoided using it to set new_time. Are you working on someone else's code? Why are you looping over a single filename? Unrelated, but should be fixed: There are multiple variable expansions that need double quoting in your code. The output that you present does not seem to correspond to the code that you show (the pathname on the first line of output is not outputted by the code).
    – Kusalananda
    May 15 at 8:07
  • @Kusalananda if you take the first line of "output" as being the filename that's being processed rather than actual output, it does seem to be plausible. The bad assignment in the middle of one of the outputs, for example
    – roaima
    May 15 at 8:22
  • Thanks for your comment. actually, It is not someone code but I am new here. for dat and time, the output is good and the output for new time is ok as you can see in the third line of output. I am just confused that why it cant be saved in new_variable. May 15 at 9:15
  • ``` new_time=date -d "$(date -Iseconds -d "$dat $time") - 2 minutes - 0.05 seconds" +%Y/%m/%dT%H:%M:%S ``` it works May 15 at 9:32
  • AqeelAbbas please would you edit your question to add detail. Don't put it in the comments. While you are editing, please explain where the first line of your output comes from, as I can't see anywhere in your code that prints a filename
    – roaima
    May 15 at 9:53
1

it works:

new_time=`date -d "$(date -Iseconds -d "$dat $time") - 2 minutes - 0.05 seconds"  +%Y/%m/%dT%H:%M:%S`
1
1

You can use below method too and its works

new_time=$(date -d "$(date -Iseconds -d "$dat $time") - 2 minutes - 0.05 seconds"  +%Y/%m/%dT%H:%M:%S)
2
  • how can we get seconds till 4 decimal for e.g 23:59:55.3245. if I use S, it will give me no values after 23:59:59 but if i use "s", it will give more than 9 digits e.g 23:59:59.6746386 May 15 at 12:45
  • i am getting out of %S date +%d-%m-%Y-%H-%M-%S 15-05-2021-18-38-20 May 15 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.