filmov
tv
How to get the last character of a string in a shell - Field Separator - Linux Tips And Tricks

Показать описание
Learn how to get the last character of a string in a shell. we will be using field separator and will be demonstrating using the awk and perl command.
Suppose I have the string 1:2:3:4:5 and I want to get its last field (5 in this case). How do I do that using Bash? I tried cut, but I don't know how to specify the last field with -f.
Get last character
echo 1:2:3:4:5 | awk -F: '{print $NF}'
in the awk command, $NF denotes print the last field in a space delimited file.
Alternatively, you can use Perl.
echo 1:2:3:4:5 | perl -F: -wane 'print $F[-1]'
Suppose I have the string 1:2:3:4:5 and I want to get its last field (5 in this case). How do I do that using Bash? I tried cut, but I don't know how to specify the last field with -f.
Get last character
echo 1:2:3:4:5 | awk -F: '{print $NF}'
in the awk command, $NF denotes print the last field in a space delimited file.
Alternatively, you can use Perl.
echo 1:2:3:4:5 | perl -F: -wane 'print $F[-1]'