A command to print only the last 3 characters in a string
I know that the cut
command can print the first n
characters of a string but how to select the last n
characters?
If I have a string with a variable number of characters, how can I print only the last three characters of the string. eg.
"unlimited" output needed is "ted" "987654" output needed is "654" "123456789" output needed is "789"
Best Answer
Why has nobody given the obvious answer?
sed 's/.*\(...\)/\1/'
… or the slightly less obvious
grep -o '...$'
Admittedly, the second one has the drawback that lines with fewer than three characters vanish; but the question didn’t explicitly define the behavior for this case.