-
Notifications
You must be signed in to change notification settings - Fork 0
find replace
Sean Akahane-Bryen edited this page Jul 26, 2020
·
4 revisions
find-replace.kmmacros
(with backups)
find-replace-nb.kmmacros
(without backups)
Performs a find and replace operation on the content but not the titles of all notes.
To leave the modification times of edited files as they were, uncomment the relevant lines in the invoked shell script. This might be desirable if, in your note-taking software, you sort your notes by modification time and want to see the notes you've been working on recently at the top, rather than the notes updated by this macro. Note that if The Archive is open while this macro is invoked, it won't recognise that the edited files have changed, and you may as a result end up overwriting what this macro has changed.
cd "$KMVAR_Instance_Notes_Directory"
# Create a backup directory.
backups="$KMVAR_Instance_Backup_Directory/$(date "+%Y-%m-%d, %H.%M") - Replace \"$KMVAR_Instance_Find\" with \"$KMVAR_Instance_Replace\""
mkdir "$backups"
# Identify notes containing the string to be replaced.
grep -rl "$KMVAR_Instance_Find" . | while read -r f ; do
# Back up the notes.
cp -p "$f" "$backups/$f"
# Perform the replacement.
# Uncomment the commented lines in the following block if you prefer that the modification times of notes containing wikilinks are *not* updated. These lines save the modification timestamp against an empty temporary file. An alternative method would be to use `stat` to save the modification time to a variable.
# touch "$f.temp"
# touch -r "$f" "$f.temp"
find_escaped=$(sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$KMVAR_Instance_Find")
replace_escaped=$(sed 's/[&/\]/\\&/g' <<<"$KMVAR_Instance_Replace")
sed -i "" "s/$find_escaped/$replace_escaped/g" "$f"
# touch -r "$f.temp" "$f"
# rm "$f.temp"
done
Version | Date | Changes |
---|---|---|
1.06 | 2020-07-26 | Escape regex metacharacters before feeding sed
|
1.05 | 2020-07-26 | Add success notifications |
1.04 | 2020-07-24 | Remove default triggers; use simpler macro names |
1.03 | 2020-07-23 | Use consistent variable names |
1.02 | 2020-07-08 | Update modification times by default |
1.01 | 2020-07-07 | Use instance rather than global variables |
1.00 | 2020-07-02 | Initial commit |