My objective is to only modify the line that begins with data, which there is only one of. In this line, I am replacing the 51-80 characters and the 97-126 characters. I would rather have a one liner with sed because then I can use the -i
flag and it would then save the file on demand.
Here is the line in the file I am modifying:
data = '{\n"feature_name": "ALL",\n"start_date": "2020-06-07T08:34:00.000-06:00",\n"end_date": "2020-06-08T13:35:00.000-06:00",\n"product": "SAEP",\n"limit":1000\n}'
Here is my two awk statements that are modifying the start_date and end_date portions of the string. Ive included the two variables that I use to get the current time minus 15 minutes ago for the start_date as well as the current time for the end_date:
ctime=`date +%Y-%m-%dT%H:%M:%S.%3N-00:00`
fifteen_min_ago=`date -d "15 mins ago" +%Y-%m-%dT%H:%M:%S.%3N-00:00`
awk '$1 ~ /^data/{printf "%s%*s%s\n",substr($0,1,m-1),n-m,"'$fifteen_min_ago'",substr($0,n)}' m=51 n=80 file
awk '$1 ~ /^data/{printf "%s%*s%s\n",substr($0,1,m-1),n-m,"'$ctime'",substr($0,n)}' m=97 n=126 file
This works beautifully but I need to save the file. If I run two separate awk commands, it only modifies one at a time. So either I combine them and save with awk or will need a sed one-liner that I could then use -i
with.
Thanks.