shell/ not bash/

This commit is contained in:
bth 2024-01-14 11:57:10 -08:00
commit 4870919531
15 changed files with 0 additions and 0 deletions

23
shell/cat.sh Executable file
View file

@ -0,0 +1,23 @@
# cat for Markdown in my ultralight writing database
# wherein concatenated filed need a "\n---\n\n" between them.
#############################################################################
# cat with file headers | replace with section string | remove first instance
# NOTE: only need to add one newline, because the header inserted by tail
# implicitly adds two : \n==> foo <==\n
tail -n +1 "$@" | sed 's/^==> .* <==$/---\n/g' | tail -n +3
##############################
# failed approaches
# find . -type f -name "$@" -exec cat {} \; -printf "\n---\n\n"
# this leaves one trailing, could cat the whole thing and then cut the tail off
# MDCATALL=`for i in "$@";
# do
# cat $i
# echo '\n---\n\n'
# done`
# echo $MDCATALL | head -n +2

9
shell/last.sh Executable file
View file

@ -0,0 +1,9 @@
# Get the last file name of any numbered series of text files.
# file format:
# in regex terms:
# [0-9]+\.[.*\.]*md|txt
# in plain terms:
# [leading numbers with consistent width].[optional string descriptor and . separator][md|txt]
ls | egrep '^[[:digit:]].*\.md|txt' | tail -n 1

38
shell/next.awk Normal file
View file

@ -0,0 +1,38 @@
# awk script to increment the digits, preserve the zero prefix, add string descriptor if supplied, and add back extension
BEGIN {
# Builtin variable, "field seperator", blank by default. The neatness of this is
# the main reason I use awk to parse the filename.
FS = "."
}
{
# another reason why awk is great for this:
newnum = $1 + 1
# could I do this with a simpler regex to select all leading zeros?
nzeros = length($1) - length(int(newnum))
leadingzeros = ""
if (nzeros > 0)
for (i = 1; i <= nzeros; i++)
leadingzeros = sprintf("%s%s", leadingzeros, "0")
if (nzeros > 0)
newnum = sprintf("%s%s", leadingzeros, newnum)
# start by prefixing the number, now with leading zeros
finalstr = newnum
# allow an arg input to fill out descriptor string.
if (arg != "")
finalstr = sprintf("%s.%s", finalstr, arg)
# append last field by default
finalstr = sprintf("%s.%s", finalstr, $NF)
}
END {
print finalstr
}

23
shell/next.sh Executable file
View file

@ -0,0 +1,23 @@
# Get the file name in any numbered series of text files and open with emacs
# Usage:
# $ next [descriptor]
# Handles an optional string descriptor. Like this:
# $ ls
# 011.blah.md
# $ next foo
# 012.foo.md
# File format:
# in regex terms:
# [0-9]+\.[.*\.]*md|txt
# in plain terms:
# [leadings numbers with consistent width].[optional string descriptor and . separator][md|txt]
# NOTE the optional $1 arg passed to awk with -v:
NEXTFILE=`$UMBASEPATH/last.sh | awk -f $UMBASEPATH/next.awk -v arg=$1`
# HACK: emacs won't accept piped input as a file name, so we use --eval to open
# the file via find-file.
emacsclient --eval "(progn (find-file \"$NEXTFILE\") (uldb-journal-header) (message \"creating $NEXTFILE\"))"

27
shell/rename.awk Normal file
View file

@ -0,0 +1,27 @@
# generates a string of "oldname newname" from 2 records, for use with mv. Main
# problem solved here generating the newname string using the number prefix and
# filetype affix.
BEGIN {
FS="."
oldname = ""
num = ""
ext = ""
newname = ""
}
NR == 1 {
oldname = $0
num = $1
# important that this is the last field, not numbered, to allow for a file
# without a string descriptor.
ext = $NF
}
NR == 2 {
newname = sprintf("%s.%s.%s", num, $0, ext)
}
END {
print oldname, newname
}

17
shell/rename.sh Executable file
View file

@ -0,0 +1,17 @@
# rename : renames the string descriptor used in numbered series.
# Usage:
# > rename 100.md bar
# or
# > rename 100.foo.md bar
# is equivalent to:
# > mv 100.md 100.bar.md
# > mv 100.foo.md 100.bar.md
# split the args by a newline so that awk reads them as separate "records"
mvcmd=`echo "$1
$2" | awk -f $UMBASEPATH/rename.awk`
mv $mvcmd

2
shell/tag.sh Executable file
View file

@ -0,0 +1,2 @@
# just list the files so we can pipe it.
grep -l "\+ $@" *md

0
shell/test/001.md Executable file
View file

0
shell/test/002.blah.md Executable file
View file

0
shell/test/004.foo.md Executable file
View file

0
shell/test/005.md Executable file
View file

0
shell/test/009.boo.md Executable file
View file

0
shell/test/098.baa.md Executable file
View file

0
shell/test/099.poo.md Executable file
View file

27
shell/um Executable file
View file

@ -0,0 +1,27 @@
#!/bin/bash
# export so that we can use it in subscripts.
export UMBASEPATH=`realpath $0 | xargs dirname`
# catch first arguments with $1
case "$1" in
next)
$UMBASEPATH/next.sh $2
;;
last)
$UMBASEPATH/last.sh
;;
cat)
# all args but first
$UMBASEPATH/cat.sh "${@:2}"
;;
rename)
$UMBASEPATH/rename.sh "${@:2}"
;;
tag)
$UMBASEPATH/tag.sh "${@:2}"
;;
*)
echo "usage: um (next|last|cat|rename|tag)"
;;
esac