« Back to Index

[Sed Date Insertion]

View original Gist on GitHub

Tags: #sed #bash #date

Sed Date Insertion.sh

# for date command read...
# https://gist.github.com/Integralist/82539776c218e590d64126d58edc5e38

# linux example (no backup file created)
echo '"foo": "{{date}}"' > foo.txt
cat foo.txt | sed "s/{{date}}/$(date -d "today 1 hour" +%s)/"
sed -i "s/{{date}}/$(date -d "today 1 hour" +%s)/" foo.txt

# macOS example (backup file mandatory: requires manual removal)
echo '"foo": "{{date}}"' > foo.txt
cat foo.txt | sed "s/{{date}}/$(date -u -v +1H +%s)/"
sed -i ".backup" "s/{{date}}/$(date -u -v +1H +%s)/" foo.txt