3 ms·
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')" "$l
by mid-kid 3y ago
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.
- swores 3y agoYou may not see this two weeks later comment, but in case you do have reply notifications or check the threads page: Thanks for the timestamp function! I can't believe it never occurred to me to do something like that before, and unlike you I think I actually will be using it now and then :)
- darnir 3y agoIf you like the timestamp function, this is available as a tool in the `moreutils` package as `ts`. You can simply pipe the output of any command to ts and have a timestamp prepended to each line it outputs. Extremely useful for streaming logs
- UI_at_80x24 3y ago> timestamp() {...etc...} I prefer that info to be in my PS1. PS1="\[\e[0;36m\[\e[m\] \[\e[0;36m\][\w]\[\e[m\]\n\t \u@\h \[\e[0;36m\]\$ \[\033[0m\]" Double-tapping Enter before I run a command where I'll want to know the time I launched it as, solves the "when did I launch this?" question. Example output: [~] 10:22:16 username@hostname $