Extract a part of variable/line content in a file

I have a variable and assigned the following values
***XYZ_201519_20150929140642_20150929140644_211_0_0_211

I need to read this variable from backward and stop read when I get first underscore (_)

In this scenario I should get 211

Thanks
Kris

Please use code tags as required by forum rules!

bash ? Try

echo ${var##*_}
211

Hello Kris,

You could try following too.

echo "***XYZ_201519_20150929140642_20150929140644_211_0_0_211" | awk -F"_" '{print $NF}'

Output will be as follows.

211

In case you need to read from a Input_file then following may help.

cat Input_file
***XYZ_201519_20150929140642_20150929140644_211_0_0_211

awk -F"_" '{print $NF}' Input_file

Thanks,
R. Singh

grep

echo ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 | grep -o "[^_]\+\$"