Search code examples
regexbashdatecut

More robust way of extracting date field from string in shell script


I have a string which described the active directory expiration date of my account. I want to use/parse this string and do something when expiration date is coming soon. Now I have a problem extracting the date-part of the complete string. The string is:

Password expires             1-4-2015 15:41:05

I want to extract 1-4-2014 from it, but when the date is 20-12-2015, the mechanism should also work. Unfortunately I'm unable to config the source system so that it outputs leading zero's. I have tried piping the string to cut -c30-39, but obviously this does not work with strings which are in total longer (like 20-12-2015).

So I'm looking for a more robust solution, something like extracting extracting until the last 4 digits AFTER the 2nd --character.


Solution

  • You could use grep,

    $ echo 'Password expires             1-4-2015 15:41:05' | grep -o '\b[0-9]\{1,2\}-[0-9]\{1,2\}-[0-9]\{4\}\b'
    1-4-2015
    $ echo 'Password expires             20-12-2015 15:41:05' | grep -o '\b[0-9]\{1,2\}-[0-9]\{1,2\}-[0-9]\{4\}\b'
    20-12-2015
    

    To grep only the year.

    $ echo 'Password expires             20-12-2015 15:41:05' | grep -oP '^(?:[^-]*-){2}\K\d{4}\b'
    2015
    

    To get only the day

    $ echo 'Password expires             1-4-2015 15:41:05' | grep -oP '\b[0-9]{1,2}(?=-[0-9]{1,2}-[0-9]{4}\b)'
    1
    

    To get only the month

    $ echo 'Password expires             1-4-2015 15:41:05' | grep -oP '\b[0-9]{1,2}-\K[0-9]{1,2}(?=-[0-9]{4}\b)'
    4