#!/bin/bash # Search ~/Notes/journal for a term and return as a markdown list sorted by date. 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}') printf "${colourWhite}=%.0s" $(seq $termsize) echo # very bad way to check the params passed in... [[ -z "$2" ]] && [[ -n "$1" ]] && searchterm="$1" [[ -n "$2" ]] && [[ -n "$1" ]] && searchterm="$1" && flag="$2" # 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 -e "$(grep -R $flag "$searchterm" /home/"$USER"/Notes/journal/)" > "$grepped_results" # more confirmatory text echo "Command: 'grep -R $flag $searchterm /home/"$USER"/Notes/journal/'" 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(.*)' while IFS= read -r line; do if [[ $line =~ $re ]]; then year=${BASH_REMATCH[1]} month=${BASH_REMATCH[2]} day=${BASH_REMATCH[3]} time=${BASH_REMATCH[4]} note=${BASH_REMATCH[5]} fi out_line="${colourGreen}"$year"-"$month"-"$day"${colourGray}T${colourLightGreen}"$time":${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"