Search code examples
unixif-statementawkkshequals-operator

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


I need to find the last column from a variable that contains some fields. I need to write something like:

    if [ #the last column = "R" ];
    then
    value=`echo "'$value'"`
    fi

Is it possible?


Solution

  • More universal code assuming separation by spaces:

    case $var in
      (*\ R) printf "%s\n" "$var"
    esac
    

    Or:

    if [ "${var##* }" = R ]; then
      printf "%s\n" "$var"
    fi