Using shell to get the last character in a line

Good day ladies and gents,

I'm trying to write shell code that can give ignore everything else in a line bar the last character but not having much luck.

Any ideas? I have been looking at cut and sed but not making any progress.

> echo "hello" | awk '{print substr($0,length,1)}'
o
> echo "hello smiley" | awk '{print substr($0,length,1)}'
y

Works a treat, thanks very much.

I usually look at awk and feel a bit ill :wink:

sed 's/.*\(.\)$/\1/' input

[GNU and some implementations of New AWK]

awk '{print $NF}' FS= input
perl -nle'print/(.)$/' input

or

perl -lne'print chop' input
ruby -ne'puts$_[/.$/].to_s' input
while IFS= read -r; do printf "%s\n" "${REPLY#${REPLY%?}}";done<input

With recent versions of bash:

while IFS= read -r; do printf "%s\n" "${REPLY[ -1]}";done<input

With zsh:

while IFS= read -r;do print -- $REPLY[-1];done<input

If rev and process substitution are available:

cut< <(rev input) -c1

Or with GNU grep (this won't display empty lines):

grep -Eo '.$' input