If the last column is equal "R" then... Is it possible? In unix If the last column is equal "R" then... Is it possible? In unix unix unix

If the last column is equal "R" then... Is it possible? In unix


With awk you can try:

awk '$NF=="R"' <<< "$var"

Test:

$ var="this is a var with last as R"$ awk '$NF=="R"' <<< "$var"this is a var with last as R$ var1="This should not be printed"$ awk '$NF=="R"' <<< "$var1"$


The condition can be:

if [[ $value == *' 'R ]]then    echo $valuefi

No need for an external language, like awk.


Using the =~ binary operator:

$ var="Some arbitrary string ending in R"$ unset value$ [[ "$var" =~ $'R$' ]] && value=${var}$ echo $valueSome arbitrary string ending in R$ var="Some arbitrary string ending in Q"$ unset value$ [[ "$var" =~ $'R$' ]] && value=${var}$ echo $value