Get date from a line of output

Is there a way to retrieve the date (02/09/2016), wherever it's located in the line?
I'm writing a script that checks Carbon Copy Cloner backups.

This was my first step which returns details of the last backup:

/Applications/Carbon\ Copy\ Cloner.app/Contents/MacOS/ccc -h | tail -1

Which outputs

CCC Backup Task|System HD|computer clone|02/09/2016, 12:13|13:33|1.25 GB|Success

I've tried this for testing purposes: Sample input

/Applications/Carbon\ Copy\ Cloner.app/Contents/MacOS/ccc -h | tail -1 | awk '{print $5}' | cut -c 7- | cut -c -10

Although this works on my machine, I've tested this on other machines and it's not very efficient because the date is in a different place.

Expected output

02/09/2016

Can this be done?

Try to pipe it into

grep -Eo '([0-9]{2}/){2}[0-9]{4}' file
02/09/2016
2 Likes

I'm not sure what you mean by piping it, but I've tried:

grep -Eo '([0-9]{2}/){2}[0-9]{4}' | /Applications/Carbon\ Copy\ Cloner.app/Contents/MacOS/ccc -h | tail -1

Pipe INTO...

/Applications/Carbon\ Copy\ Cloner.app/Contents/MacOS/ccc -h | tail -1 | grep -Eo '([0-9]{2}/){2}[0-9]{4}'
1 Like

Hi.

Then supplying us with samples of the different inputs will help us help you.

Best wishes ... cheers, drl

As a general remark: "a date" is not a standardised (or standardisable) data format. You can search (as RudiC suggested) for this regex, but be aware that it doesn't search for "a date" but for two digits, followed by a slash, followed by two more digits, followed by another slash, followed by four more digits.

In most cases, a string matching that will be a date indeed, but it might be something else either. Further more, this: 1/1/2016 is perhaps a date too, but wouldn't be found by the regex. On the other hand you could remove the restrictions RudiC has perhaps put in for safety and simplify the regexp to:

grep '[0-9][0-9]*/[0-9][0-9]*/[0-9][0-9][0-9]*'

but in this case it would find a "date" in the following sample text, which denotes the common way in Austria to tell the street number, apartment number and the floor on which it is located:

i live in apartment number 10 on the second floor in thisstreet number 15: 
Adress: 12345 Somewhere, Thisstreet 15/2/10

Bottom line is: you will have to be careful to watch if what you place as restrictions to dates will lead to all the correct things being found and all the incorrect ones being thrown out. But this is a slippery slope and there is no final algorithm which separates all "dates" from everything "not a date".

I hope this helps.

bakunin

1 Like