Skip to content

Instantly share code, notes, and snippets.

@mbigras
Last active March 21, 2025 07:29
Show Gist options
  • Save mbigras/3c855d54f7700dbd55e311a56ddbb5e2 to your computer and use it in GitHub Desktop.
Save mbigras/3c855d54f7700dbd55e311a56ddbb5e2 to your computer and use it in GitHub Desktop.
Experiments with different "e" scripts
# e - list files sorted by time, also filter and edit if only 1 match
# usage: e pattern1 pattern2 ...
pattern=$(echo "$@" | awk '
BEGIN { printf "//" }
{ for (i = 1; i <= NF; i++)
printf(" && /%s/", $i) }')
matches=$(ls -t | tac | awk "$pattern")
echo "$matches"
if test $(echo "$matches" | wc -l) -eq 1
then
ed "$matches"
fi
# e - list files sorted by time, also filter and edit if only 1 match
# usage: e pattern1 pattern2 ...
ls -t | awk '
BEGIN {
for (i = 1; ARGV[i] != "-"; i++) { # collect patterns
pat[++np] = ARGV[i]
ARGV[i] = ""
}
}
{ for (i = 1; i <= np; i++) {
if (!match($0, pat[i])) next
}
nm++; fn=$0; print
}
END { if (nm == 1)
system(sprintf("rlwrap ed </dev/tty \"%s\"", fn))
}
' "$@" -
# e - list files sorted by time, also filter and edit if only 1 match
# usage: e pattern1 pattern2 ...
f=$(mktemp)
m=$(mktemp)
printf '/%s/ && ' "" "$@" | awk '{ $NF=""; print }' >$f
ls -t | awk -f $f | tee $m
if test $(wc -l <$m) -eq 1
then
exec rlwrap ed "$(cat $m)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment