Out of neat one-liners I never use but sound good on paper?
timestamp() {
while IFS= read -r line; do printf '[%s] %s\n' "$(date '+%F %T.%4N')" "$line"; done
}
This prepends a timestamp in front of the output of a command, useful for long-running commands, and logging (e.g. "while sleep .1; do echo openssl rand -hex 8; done | timestamp").Out of scripts I actually use a lot? Boring stuff like "refactor":
sedescape_regex() {
printf '%s' "$@" | sed 's/[^^]/[&]/g; s/\^/\\^/g'
}
sedescape_subst() {
printf '%s' "$@" | sed 's/[&/\]/\\&/g'
}
regex="$(sedescape_regex "$1")"; shift
subst="$(sedescape_subst "$1")"; shift
grep -rwl \
--include='*.sh' \
--include='*.c' --include='*.h' \
--include='*.asm' --include='*.inc' \
--include='*.s' --include='*.i' \
--include='*.ld' \
--include='*.md' \
"$regex" "$@" | xargs -t sed -i "s/\<$regex\>/$subst/g"
This one is obviously less useful for languages with namespaces, hence why --include currently only really has C and a few Assembly flavors.I wish there was a CLI "refactor" tool that supported LSP, without needing a heavy editor to do that.