diff options
author | Matthew Lemon <chaffinach+git@protonmail.ch> | 2022-04-19 11:11:59 +0100 |
---|---|---|
committer | Matthew Lemon <chaffinach+git@protonmail.ch> | 2022-04-19 11:11:59 +0100 |
commit | 1ca23d7427d216737eb62160dcc60cdd6ca28673 (patch) | |
tree | d5d8fd9fde24eddfbf803728f4d2266975790707 | |
parent | 401b29df7d137747b289e0463e329aa64cb074c2 (diff) |
creating bsd-versions of bash scripts
-rwxr-xr-x | _tj | 1 | ||||
-rwxr-xr-x | _tj_openbsd | 27 | ||||
-rwxr-xr-x | batnote_openbsd | 56 | ||||
-rwxr-xr-x | grepjournal_openbsd | 130 | ||||
-rwxr-xr-x | grepnote_openbsd | 23 | ||||
-rwxr-xr-x | lem_openbsd | 21 | ||||
-rwxr-xr-x | process_hsbc_transactions_openbsd.sh | 37 |
7 files changed, 294 insertions, 1 deletions
@@ -15,7 +15,6 @@ else touch "$TODAY_JOURNAL" header_date="$(date +'%A %d %b %Y')" { echo -e "# $header_date\n" - echo -e "## Tasklog\n" echo -e "## Journal\n" } >> "$TODAY_JOURNAL" while read -r line diff --git a/_tj_openbsd b/_tj_openbsd new file mode 100755 index 0000000..7b97641 --- /dev/null +++ b/_tj_openbsd @@ -0,0 +1,27 @@ +#!/usr/local/bin/bash + +# Our target file +TODAY_JOURNAL=~/Notes/journal/$(date +\%Y-\%m-\%d).md + +# Test whether it already exisits or not +if [[ -a $TODAY_JOURNAL ]] +then + while read -r line + do + JLINE="- $(date +'%H:%M'): $line" + echo "$JLINE" >> "$TODAY_JOURNAL" + done < "${1:-/dev/stdin}" +else + touch "$TODAY_JOURNAL" + header_date="$(date +'%A %d %b %Y')" + { echo -e "# $header_date\n" + echo -e "## Journal\n" + } >> "$TODAY_JOURNAL" + while read -r line + do + JLINE="- $(date +'%H:%M'): $line" + echo "$JLINE" >> "$TODAY_JOURNAL" + done < "${1:-/dev/stdin}" +fi + + diff --git a/batnote_openbsd b/batnote_openbsd new file mode 100755 index 0000000..9a0dea9 --- /dev/null +++ b/batnote_openbsd @@ -0,0 +1,56 @@ +#!/usr/local/bin/bash + +# a script for FZFing through my Notes folder for quick reading. +# Uses bat if installed, otherwise will use less. + +NOTES=/home/$USER/Notes +FZF_BIN=/home/$USER/.fzf/bin/fzf +BAT=/usr/bin/bat +LESS=/usr/bin/less +VIM=/usr/local/bin/vim + +CMD=$LESS + +# instead of viewing with less, we want to edit in Vim +if ! [[ -x $FZF_BIN ]]; then + echo "You need to have FZF installed for this to work." + exit 1 +fi + +if [[ $1 = "-v" ]] +then + echo "Using vim..." + CMD=$VIM + # Thanks to https://stackoverflow.com/a/1489405 for the find command to omit .git + $CMD "$(find $NOTES -name '.git*' -type d -prune -o -type f -print|$FZF_BIN)" + exit +fi + +if [[ $1 = "-m" ]] +then + echo "Searching mod files only..." + NOTES=$NOTES/modzet + if [[ $2 = "-v" ]] + then + echo "Using vim..." + CMD=$VIM + fi + # Thanks to https://stackoverflow.com/a/1489405 for the find command to omit .git + $CMD "$(find $NOTES -name '.git*' -type d -prune -o -type f -print|$FZF_BIN)" + exit +fi + +# if [[ -z $1 ]]; then +# echo "You must provide a file name as the argument to this command." +# exit 1 +# else +# TARGET=$1 +# fi + + +if [[ -x $BAT ]]; then + CMD=$BAT +fi + +# Thanks to https://stackoverflow.com/a/1489405 for the find command to omit .git +clear; $CMD "$(find $NOTES -name '.git*' -type d -prune -o -type f -print|$FZF_BIN)" diff --git a/grepjournal_openbsd b/grepjournal_openbsd new file mode 100755 index 0000000..fbd72e8 --- /dev/null +++ b/grepjournal_openbsd @@ -0,0 +1,130 @@ +#!/usr/local/bin/bash + +# Search ~/Notes/journal for a term and return as a markdown list sorted by date. + +# keep track of the last executed command +trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG +# echo an error message before exiting +trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT + +function usage { + echo + echo "Usage: grepjournal [WORD] [-i (case insensitve)] [-h (display this help)]" + echo " Searches for WORD within all files within ~/Notes/journal. Displayed sorted by date." +} + +if [[ $# -eq 0 ]]; then + usage; exit 1 +fi + +while [[ $# -gt 0 ]]; do + if [[ "$1" = "-i" ]]; then + flag="-i" + shift + elif [[ "$1" = "-h" ]] || [[ "$1" = "--help" ]]; then + usage + exit + else + searchterm=$1 + shift + fi +done + +_get_weekday() { + local monday="# (Monday)" + local tuesday="# (Tuesday)" + local wednesday="# (Wednesday)" + local thursday="# (Thursday)" + local friday="# (Friday)" + local saturday="# (Saturday)" + local sunday="# (Sunday)" + while IFS= read -r fileline; do + echo $fileline + if [[ $fileline =~ $monday ]]; then + echo "${BASH_REMATCH[1]}" + elif [[ $fileline =~ $tuesday ]]; then + echo "${BASH_REMATCH[1]}" + elif [[ $fileline =~ $wednesday ]]; then + echo "${BASH_REMATCH[1]}" + elif [[ $fileline =~ $thursday ]]; then + echo "${BASH_REMATCH[1]}" + elif [[ $fileline =~ $friday ]]; then + echo "${BASH_REMATCH[1]}" + elif [[ $fileline =~ $saturday ]]; then + echo "${BASH_REMATCH[1]}" + elif [[ $fileline =~ $sunday ]]; then + echo "${BASH_REMATCH[1]}" + fi + done < "$1" +} + +colourWhite="\033[38;2;255;255;255m" +colourGreen="\033[38;2;0;255;0m" +colourLightGreen="\033[38;2;231;252;179m" +colourGray="\033[38;2;100;100;100m" +colourOrange="\033[38;2;249;130;44m" +colourCyan="\033[38;2;0;255;255m" +txBold="\033[1m" +txReset="\033[0m" + +# Automatic cleanup +trap 'rm -f "$grepped_results"' EXIT +grepped_results=$(mktemp) || exit 1 + +trap 'rm -f "$output_file"' EXIT +output_file=$(mktemp) || exit 1 + +# nice line across the top +termsize=$(stty size| awk '{print $2}') +# DOESNT WORK IN BSD printf "${colourWhite}=%.0s" $(seq $termsize) +echo + +# some confirmatory echoing +echo -e "${colourWhite}Search term: ${txBold}$searchterm${txReset}" +[[ -v flag ]] && echo "Flag: $flag" + +if [[ $flag != "-i" ]]; then + flag="" +fi + +# do the business, starting with using grep to get the pertinent lines +echo "Starting to search" +echo -e "$(grep -R $flag "$searchterm" /home/"$USER"/Notes/journal/)" > "$grepped_results" + +# more confirmatory text +echo "Command: 'grep -R $flag $searchterm /home/"$USER"/Notes/journal/'" + +# DOESN'T WORK IN BSD printf "=%.0s" $(seq $termsize) +echo "" + +# because I can't get the regex right, I am searching for http or https to indicate a link in a line +urlregex="https?" +re='(^/home/lemon/Notes/journal/([0-9]{4})-([0-9]{2})-([0-9]{2})\.md):-\s([0-9]{2}:[0-9]{2}):\s(.*)' +cat $grepped_results +while IFS= read -r line; do + echo $line + if [[ $line =~ $re ]]; then + path=${BASH_REMATCH[1]} + year=${BASH_REMATCH[2]} + month=${BASH_REMATCH[3]} + day=${BASH_REMATCH[4]} + time=${BASH_REMATCH[5]} + note=${BASH_REMATCH[6]} + else + echo "Problem matching line from grepped_results" + exit 1 + fi + output_day=$(_get_weekday "$path") + out_line="${colourGreen} "$year"-"$month"-"$day"${colourGray}T${colourLightGreen}"$time": ${output_day} ${txReset} ${note}" + if [[ $out_line =~ $urlregex ]]; then + out_line=${out_line/${BASH_REMATCH[0]}/${colourCyan}${BASH_REMATCH[0]}${txReset}} + fi + if [[ $out_line =~ $searchterm ]]; then + out_line=${out_line/$searchterm/"${colourOrange}${txBold}$searchterm${txReset}"} + fi + echo -e "$out_line" >> "$output_file" +done < "$grepped_results" + +# output +sort < "$output_file" + diff --git a/grepnote_openbsd b/grepnote_openbsd new file mode 100755 index 0000000..36cb8c6 --- /dev/null +++ b/grepnote_openbsd @@ -0,0 +1,23 @@ +#!/usr/local/bin/bash + +NOTES=/home/$USER/Notes +RG_BIN=/usr/local/bin/rg +GREP=/usr/bin/grep +CMD="$RG_BIN --heading -w -i -g !"*fish-history*" -g !"*.html*" -g !"*apt-packages*" -g !"*.json" -g !"*-fish-history*" -g !"*backup*"" + +if [[ -z $1 ]]; then + echo "You must provide a search term as the argument to this command." + exit 1 +else + TARGET="$1" +fi + +# test for rg +if ! [[ -x $RG_BIN ]]; then + CMD="$($GREP -n -H -r)" +fi + +# rg --heading -i "joanna" ~/Notes/journal/ +$CMD "$TARGET" $NOTES + + diff --git a/lem_openbsd b/lem_openbsd new file mode 100755 index 0000000..1eb7e92 --- /dev/null +++ b/lem_openbsd @@ -0,0 +1,21 @@ +#!/usr/local/bin/bash + +NOTES=/home/$USER/Notes +TARGET_DIR=$NOTES/homezet +VIM=/usr/local/bin/vim + +if [[ "$1" = "-m" ]]; then + msg="${*:2}" + TARGET_DIR=$NOTES/modzet + shift +else + msg="$@" + shift +fi + + +STAMP="$(date +%G%m%d%H%M%S)" +F_PATH="$TARGET_DIR/$STAMP-$msg.md" +echo $F_PATH + +$VIM "$F_PATH" diff --git a/process_hsbc_transactions_openbsd.sh b/process_hsbc_transactions_openbsd.sh new file mode 100755 index 0000000..e2c1e29 --- /dev/null +++ b/process_hsbc_transactions_openbsd.sh @@ -0,0 +1,37 @@ +#!/usr/local/bin/bash + +tf="TransactionHistory.csv" + +# Automatic cleanup +trap 'rm -f "$tmpfile"' EXIT +tmpfile=$(mktemp) || exit 1 + +trap 'rm -f "$ledgertmp"' EXIT +ledgertmp=$(mktemp) || exit + +# Check for presence of transaction file +if [[ -a ~/Downloads/$tf ]]; then tpath=~/Downloads/$tf +elif [[ -a "$(pwd)/$tf" ]]; then tpath="$(pwd)/$tf" +else "Cannot find $tf. Expecting it either in ~/Downloads or in current folder."; exit 1 +fi + +# remove the BOM (https://stackoverflow.com/questions/45240387/how-can-i-remove-the-bom-from-a-utf-8-file) +sed -i $'1s/^\uFEFF//' "$tpath" + +# read the file and process lines +while read -r line; do + line=${line//"-"/"£"} + echo -e "$line\n$(cat "$tmpfile")" > "$tmpfile" +done < "$tpath" + +# how to append to a text file in bash +echo -e "Date,Description,Amount\n$(cat "$tmpfile")" > "$tmpfile" + +# use ledger to convert to ledger format +ledger convert --input-date-format "%d/%m/%Y" "$tmpfile" > "$ledgertmp" + +# Remove the erroneous Equity lines +sed -i '/Equity:Unknown/d' "$ledgertmp" + +# dump to sdout for further redirection +cat "$ledgertmp" |