从此字符串中仅提取日期

时间:2016-10-25 08:58:42

标签: bash awk

我需要使用脚本从此字符串中提取日期,这是下面的脚本

#!/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我会得到以下几行

yellow open machine-apache.access-2016.10.25 5 1 13 0 63.5kb 63.5kb

1 个答案:

答案 0 :(得分:1)

可以使用下面的简单bash正则表达式来解决

$ 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