Guest User

Generate Dates for Markdown Files

a guest
Aug 23rd, 2022
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. #!/bin/bash
  2. # Contents of .git/hooks/pre-commit
  3. # Replace `last_modified_at` timestamp with current time
  4.  
  5.  
  6. for b in $(find ./ -name '*.md'); do
  7. if [ $(cat $b | head -n 1 | grep '\-\-\-') ]; then
  8. echo "already have meta"
  9. else
  10. cat $b | sed '1s/^/---\n---\n/' > tmp
  11. mv tmp $b
  12. fi
  13.  
  14. if grep -q last_modified_at $b; then
  15. echo "no change"
  16. else
  17. cat $b | sed '1 {/---.*/,/---.*/{0,/---/{s/---/---\nlast_modified_at:/}}}' > tmp
  18. mv tmp $b
  19. fi
  20.  
  21. cat $b | sed "/---.*/,/---.*/s/^last_modified_at:.*$/last_modified_at: $(date -r $b "+%Y-%m-%dT%H:%M:%S")/" > tmp
  22. mv tmp $b
  23. done
Add Comment
Please, Sign In to add comment