Several Text Processing Functions for Taking Notes

function newlog() {
	##################
	# SETTINGS
	##################
	
	text_editor_of_choice=vim
	
	location_of_files="/path/to/Research_and_Project_Log_Notes"
	
	file_extension_for_new_files="mkd" # Do not put a dot (.) in front of this. This can be txt, md, mkd, etc.
	
	##################
	# END OF SETTINGS
	##################
	
	cd "$location_of_files"
	
	current_date=$(date +%F) # This will produce a date in YYYY-MM-DD format
	current_date_filename=$current_date.$file_extension_for_new_files
	if [ -n "$1" ] # If an argument (a filename is expected) was given...
	then
		previous_todo_entries=$(grep -ir "^Todo:.*$" "$1")
	else
		previous_todo_entries=""
	fi
	echo -e "---\nDate: $current_date\n$previous_todo_entries\n---\n\n" >> "$current_date_filename" # '\n' is a newline. '>>' will append the output to "$current_date_filename" (withouth overwriting anything that's already there)
	$text_editor_of_choice "$current_date_filename"
}

Since this code is adapted from a post on StackExchange. Since all code posted on StackExchange is (for better or worse) released under a Creative Commons BY-SA license, this derived code is also released under a CC BY-SA license, in accordance of the original license terms:

# Adapted from http://unix.stackexchange.com/a/184344, for showing matching line and all indented child lines of that line.
function grep-section(){
	# To be used as a piped function (e.g., 'cat test.txt | grep-section tester')
	# The first $1 is the search pattern (expected to be the first argument to the function here) (the $1 that comes after 'length' is the length of the regex capture group).
	# What I did to amend this from the original: a) Change spaces to tabs ('^( *)' to '^(\t*)'); Make it work in a bash function by combining a bash and perl variables (double-quoting the whole thing will make bash interpret everything that starts with '$', breaking the function); c) Added the 'i' (ignore case) option
	regex_to_search_for="$1"
	echo "Searching for '$regex_to_search_for'..."
	perl -ne '/^(\t*)/; $i = length $1; $j && $i >= $j and print, next; $j = 0; /'$regex_to_search_for'/i and $j = $i + 1, print'
}

and strip-tag-delimiters() (which I wrote, and amended from a previous AdUnumDatum post introducing Markdown Mapper)

function strip-tag-delimiters(){
	# This function takes a string, and takes away (hash)tag delimiters that you can set. I wrote this, and first published it at http://adunumdatum.org/introducing-markdown-mapper.html
	# $1 and $2 are the first and second arguments to the function, respectively. $2 is optional. Note that you may need to backslash-escape them (put a backslash (\) in front) if the characters have special regex meaning.
	# This is meant to be used as a piped function (e.g., 'cat test.txt | strip-tag-delimiters '{'')
	# A real-world example: 'cat 2015-07-22.mkd| head | strip-tag-delimiters '\[\[' '\]\]'' will take out [[tag]] delimiters.
	beginning_delimiter="$1"
	optional_ending_delimiter="$2"
	perl -p -e 's/'$1'(.*?)'$2'/\1/g'
}

Related

Next
Previous