3 ms·
> grep "^$1," database | sed -e "s/^$1,//" | tail -n 1 FWIW. Shell pipelines are very useful, but sometimes, you don't need them. Case in point: - sed can do
by glandium 9y ago
> grep "^$1," database | sed -e "s/^$1,//" | tail -n 1
FWIW.
Shell pipelines are very useful, but sometimes, you don't need them.
Case in point:
- sed can do the matching:
sed -n -e "/^$1,/s/^$1,//p" | tail -1
- sed can just take the last match:
sed -n -e "/^$1,/h;\${g;s/^$1,//p}"
- sed can be cryptic, so you might prefer awk:
awk -F, '$1 == "'"$1"'" { val=$2 } END { print val }'
although the fact that it's in a shell function makes the "'"$1"'" thing kind of confusing. It would be less confusing if using a named environment variable...
maybe less bad with explicit escapes...
awk -F, "\$1 == \"$1\" { val=\$2 } END { print val }"