0

I want to use sed to replace the current time in the code below

time0=$(date '+%Y-%m-%d')
time1=$(date '+%H:%M:%S')

time=$(echo ""$time0"T"$time1"Z")

replace $time on first line

{
    "X-Apple-I-Client-Time" = "2020-07-08T12:55:08Z";
    "X-Apple-I-Locale" = "en_US";
    "X-Apple-I-MD" = "AAAABQAAABDBy+H0j9QAnYGIzrmKeh9DAAAAAw==";
    "X-Apple-I-MD-M" = "6Q3cWbCm2lI9rrTIyrAsku5zxqO/ZAv9HsW5kHo2thiPqFINp0/OsOz++KS/2vc0ImbI2iMVUbYCZjnS";
    "X-Apple-I-MD-RINFO" = 50660608;
    "X-Apple-I-TimeZone" = "GMT+7";
}

Tks All

Kusalananda
  • 333,661
toilaai
  • 11

2 Answers2

1
now=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
sed 's/\("X-Apple-I-Client-Time" = \).*/\1"'"$now"'";/' file

This computes the current UTC timestamp and saves it in the shell variable now. It then finds any line in the file called file that contains the exact string "X-Apple-I-Client-Time" = and replaces everything after it with the timestamp in quotes and with a trailing ;.

The result is written to standard output. If you want to make the change directly in the original file, you may want to use

now=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
cp file file.tmp &&
sed 's/\("X-Apple-I-Client-Time" = \).*/\1"'"$now"'";/' file.tmp >file &&
rm -f file.tmp

(This would clobber file.tmp)

If your system's sed implementation has -i for "in-place" edits, then you may obviously use that instead. Not that sed -i on FreeBSD and macOS requires an empty argument to the -i option to edit without a backup file (see How can I achieve portability with sed -i (in-place editing)?).

Kusalananda
  • 333,661
0
  1. The Z at the end of a formatted date means that the time zone is UTC, aka +0000, so you should use date --utc, not just date.

    It looks like whatever this file is requires X-Apple-I-Client-Time to be in UTC, with the actual time zone in X-Apple-I-TimeZone.

  2. why use $time0 and $time1 when date can generate the full time with just a single format string (e.g. +%FT%TZ. I can't remember if %F and %T are GNU extensions to date or not, so you might have to use '+%Y-%m-%dT%H:%M:%SZ' instead).

  3. if you need $time0 and $time1 in separate variables, then using command substitution and echo to combine them for $time is not only unnecessary, it is probably the worst way you could do it. Use something like this instead:

    time="${time0}T${time1}Z"
    
  4. To insert the timestamp into the file, you could do something like:

    time=$(date --utc +%FT%TZ)   # or maybe use '+%Y-%m-%dT%H:%M:%SZ' instead
    sed -i -e '/X-Apple-I-Client-Time/s/ = "[^"]*"/ = "'"$time"'"/' filename.txt
    

    or (if you need $time0 and $time1 elsewhere in your script):

    time0=$(date --utc '+%Y-%m-%d')
    time1=$(date --utc '+%H:%M:%S')
    

    time="${time0}T${time1}Z"

    sed -i -e '/X-Apple-I-Client-Time/s/ = "[^"]*"/ = "'"$time"'"/' filename.txt

  5. Personally, I'd avoid using a variable called $time because it's the same as the built-in time command and /usr/bin/time. Accidentally adding a space before the = in an assignment (like time = "${time0}T${time1}Z" instead of time="${time0}T${time1}Z") will run the time` command instead.

    I'd probably use $t if it was a one-use throwaway variable, or something more descriptive and a little longer otherwise.

cas
  • 78,579