Makistos
10/28/2013 - 8:00 AM

This is a simple trick to match a string to a regular string in the Bourne shell. In this example, files are searched. This will find the la

This is a simple trick to match a string to a regular string in the Bourne shell. In this example, files are searched. This will find the last file that has "my_string" in it's name. #regexp #bash

TO_MATCH=my_string
for i in `ls`; do
  if [ 'echo $i | grep -c ${TO_MATCH}` -eq "1" ]; then
    FILE=$i
  fi
done