split out to next.awk
This commit is contained in:
parent
b742baf020
commit
91b8272555
2 changed files with 41 additions and 97 deletions
35
bash/next.awk
Normal file
35
bash/next.awk
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
# awk script to increment the digits, preserve the zero prefix, add string descriptor if supplied, and add back extension
|
||||||
|
|
||||||
|
BEGIN {
|
||||||
|
# set field separator, blank by default
|
||||||
|
FS = "."
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
newnum = $1 + 1
|
||||||
|
nzeros = length($1) - length(int(newnum))
|
||||||
|
|
||||||
|
# could I do this with a simpler regex to select all leading zeros?
|
||||||
|
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
|
||||||
|
}
|
||||||
103
bash/next.sh
103
bash/next.sh
|
|
@ -1,12 +1,12 @@
|
||||||
# Get the file name in any numbered series of text files and open with emacs
|
# Get the file name in any numbered series of text files and open with emacs
|
||||||
|
|
||||||
# Usage:
|
# Usage:
|
||||||
# $ . next [descriptor]
|
# $ next [descriptor]
|
||||||
|
|
||||||
# Handles an optional string descriptor. Like this:
|
# Handles an optional string descriptor. Like this:
|
||||||
# $ ls
|
# $ ls
|
||||||
# 011.blah.md
|
# 011.blah.md
|
||||||
# $ . next foo
|
# $ next foo
|
||||||
# 012.foo.md
|
# 012.foo.md
|
||||||
|
|
||||||
# File format:
|
# File format:
|
||||||
|
|
@ -15,100 +15,9 @@
|
||||||
# in plain terms:
|
# in plain terms:
|
||||||
# [leadings numbers with consistent width].[optional string descriptor and . separator][md|txt]
|
# [leadings numbers with consistent width].[optional string descriptor and . separator][md|txt]
|
||||||
|
|
||||||
# what this does:
|
# NOTE the optional $1 arg passed to awk with -v:
|
||||||
# ls | grep [regex for number.*.md] | get last | awk script to increment the digits, preserve the zero prefix, add string descriptor if supplied, and add back extension
|
NEXTFILE=`$UMBASEPATH/last.sh | awk -f $UMBASEPATH/next.awk -v arg=$1`
|
||||||
|
|
||||||
# Note the egrep '^\d.*\.md|txt' (matches whole string) to account for an optional string between the initial number and the file extension. As in '435.blah.md'.
|
|
||||||
|
|
||||||
# old version, relied on python, which didn't handle leading zeros properly, but treated them as binary:
|
|
||||||
# ls | grep '\d.*\.md' | tail -n 1 | sed s/'\..*md'// | sed s/.*/'print & + 1'/ | python - | sed s/.*/'&.md/'
|
|
||||||
|
|
||||||
# BUGS
|
|
||||||
|
|
||||||
# 1. Emacs won't accept piped input as a file name. I'd rather keep this as a separate utility, which only printed the right filename, and then use an alias to pipe the filename to emacs, or invoke it manually.
|
|
||||||
|
|
||||||
# A.
|
|
||||||
# What I want, is this syntax:
|
|
||||||
# next [optional arg]
|
|
||||||
# To both get the file and open emacs with it.
|
|
||||||
|
|
||||||
# B.
|
|
||||||
# But this doesn't work:
|
|
||||||
# next | emacs
|
|
||||||
# Because it sees the stdin the pipe wants to feed it as a substitute for the tty in the new session, rather than as input for startup only, i.e. the filename.
|
|
||||||
|
|
||||||
# C.
|
|
||||||
# Wrapping the whole shell command here works, like this:
|
|
||||||
# emacs `ls | grep ...`
|
|
||||||
|
|
||||||
# But emacs doesn't like being run inside a child level shell, because it can't load .emacs properly. So you must run this file within the current shell using ".", like this:
|
|
||||||
# . next
|
|
||||||
# . next foo
|
|
||||||
|
|
||||||
# D.
|
|
||||||
# This does not work, because .profile must be executed, meaning that the "" expression is peeked by the shell:
|
|
||||||
# alias next="emacs `next.sh $1`"
|
|
||||||
|
|
||||||
# E.
|
|
||||||
# This does not work either, because the arg never gets filled out when evoked:
|
|
||||||
# alias next='emacs `next.sh $1`'
|
|
||||||
|
|
||||||
# F.
|
|
||||||
# This works and is the current solution:
|
|
||||||
# alias next='. next.sh'
|
|
||||||
|
|
||||||
# Which relies on 2 somewhat obscured facts:
|
|
||||||
# 1. This script calls emacs itself, wrapping the real work in ``
|
|
||||||
# 2. The alias is a simple substitution, which fortunately seems to happen before args are processed by the shell, meaning that the shell sees: ". next.sh arg"
|
|
||||||
# 3. NOTE: the symbolic link in the PATH should not be simply "next", because it
|
|
||||||
# may in certain situations conflict with the alias. The link should be "next.sh" -> ~/config/..
|
|
||||||
# 4. NOTE: I use 'e' rather than 'emacs', to use my clever emacs/emacsclient function.
|
|
||||||
|
|
||||||
|
|
||||||
NEXTFILE=`$UMBASEPATH/last.sh | awk '
|
|
||||||
|
|
||||||
BEGIN {
|
|
||||||
# set field separator, blank by default
|
|
||||||
FS = "."
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
newnum = $1 + 1
|
|
||||||
nzeros = length($1) - length(int(newnum))
|
|
||||||
|
|
||||||
# could I do this with a simpler regex to select all leading zeros?
|
|
||||||
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.
|
|
||||||
# single quote break to allow the shell to fill it out, double quotes so awk will treat it as a string
|
|
||||||
arg = "'$1'"
|
|
||||||
|
|
||||||
if (arg != "")
|
|
||||||
finalstr = sprintf("%s.%s", finalstr, arg)
|
|
||||||
|
|
||||||
# append last field by default
|
|
||||||
finalstr = sprintf("%s.%s", finalstr, $NF)
|
|
||||||
|
|
||||||
# previous loop to reassemble all fields. But I do not want the old middle string here
|
|
||||||
# Keeping it here for reference.
|
|
||||||
# for (i = 2; i <= NF; i++)
|
|
||||||
# finalstr = sprintf("%s.%s", finalstr, $i)
|
|
||||||
# }
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
END {
|
|
||||||
print finalstr
|
|
||||||
}'
|
|
||||||
`
|
|
||||||
|
|
||||||
|
# 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\"))"
|
emacsclient --eval "(progn (find-file \"$NEXTFILE\") (uldb-journal-header) (message \"creating $NEXTFILE\"))"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue