Q:

extract only date from this string

I need to extract date from this string  using script here is the script below
#!/bin/bash

DAYSAGO=`date --date="1 days ago" +%Y.%m.%d`

FETCHINDEX=`/usr/bin/curl -s -u admin:admin -XGET http://127.0.0.1:9200/_cat/indices?v | grep machine`

echo "$FETCHINDEX" | while read FORMATLINE

do

  TOGETDATE=`echo $FORMATLINE ##<Want to write to get only date> `  

 if [ "$FORMATEDLINE" -lt "$DAYSAGO" ]  ## getting error {{ integer expression expected }}

  then

    TODELETE=`echo $FORMATLINE | awk '{ print $3 }'`

    echo "http://localhost:9200/$TODELETE"

  fi

done

echo $FORMATLINE i will get below lines
yellow open machine-apache.access-2016.10.25 5 1 13 0 63.5kb 63.5kb

A:

Can be solved using a plain bash regex like below
$ string="yellow open machine-apache.access-2016.10.25 5 1 13 0 63.5kb 63.5kb"
$ [[ $string =~ .*-(([[:digit:]]{4}).([[:digit:]]{2}).([[:digit:]]{2})).* ]] && NUM=${BASH_REMATCH[1]}
$ printf "%s\n" "$NUM"
2016.10.25

