Get date from a line of output

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Get date from a line of output
# 1  
Old 09-02-2016
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:
Code:
/Applications/Carbon\ Copy\ Cloner.app/Contents/MacOS/ccc -h | tail -1

Which outputs
Code:
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
Code:
/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
Code:
02/09/2016

Can this be done?

Last edited by rbatte1; 09-07-2016 at 12:42 PM.. Reason: Added CODE tags for expected output, just to be sure
# 2  
Old 09-02-2016
Try to pipe it into
Code:
grep -Eo '([0-9]{2}/){2}[0-9]{4}' file
02/09/2016

These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 09-02-2016
I'm not sure what you mean by piping it, but I've tried:
Code:
grep -Eo '([0-9]{2}/){2}[0-9]{4}' | /Applications/Carbon\ Copy\ Cloner.app/Contents/MacOS/ccc -h | tail -1

# 4  
Old 09-02-2016
Quote:
Originally Posted by $shell_Learner
I'm not sure what you mean by piping it, but I've tried:
Code:
grep -Eo '([0-9]{2}/){2}[0-9]{4}' | /Applications/Carbon\ Copy\ Cloner.app/Contents/MacOS/ccc -h | tail -1

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

This User Gave Thanks to wisecracker For This Post:
# 5  
Old 09-02-2016
Hi.
Quote:
Originally Posted by $shell_Learner
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.
Then supplying us with samples of the different inputs will help us help you.

Best wishes ... cheers, drl
# 6  
Old 09-05-2016
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:

Code:
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:

Code:
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
# 7  
Old 09-07-2016
Quote:
Originally Posted by wisecracker
Pipe INTO...
Code:
/Applications/Carbon\ Copy\ Cloner.app/Contents/MacOS/ccc -h | tail -1 | grep -Eo '([0-9]{2}/){2}[0-9]{4}'

Thankyou so much!
This User Gave Thanks to $shell_Learner For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Ksh: Read line parse characters into variable and remove the line if the date is older than 50 days

I have a test file with the following format, It contains the username_date when the user was locked from the database. $ cat lockedusers.txt TEST1_21062016 TEST2_02122015 TEST3_01032016 TEST4_01042016 I'm writing a ksh script and faced with this difficult scenario for my... (11 Replies)
Discussion started by: humble_learner
11 Replies

3. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

[Solved] Replace yesterday date with today's date except from the first line

Hello, I have a file like this: 2012112920121130 12345620121130msABowwiqiq 34477420121129amABamauee e7748420121130ehABeheheei in case the content of the file has the date of yesterday within the lines containing pattern AB this should be replaced by the current date. But if I use... (3 Replies)
Discussion started by: Lilu_CK
3 Replies

5. Shell Programming and Scripting

Replace date on a line with current date

Hi Guys, I have a file with following content From 20121014 : To 20121014 Number of days : 1 1234 1245 1246 1111 Everyday i run my script i want to modify "To" date on the first line with current date. I have set the current date in script as RUN_DATE=`date -u +%Y%m%d` So i want... (9 Replies)
Discussion started by: jakSun8
9 Replies

6. Shell Programming and Scripting

Faster Line by Line String/Date Comparison of 2 Files

Hello, I was wondering if anyone knows a faster way to search and compare strings and dates from 2 files? I'm currently using "for loop" but seems sluggish as i have to cycle through 10 directories with 10 files each containing thousands of lines. Given: -10 directories -10 files... (4 Replies)
Discussion started by: agentgrecko
4 Replies

7. UNIX for Dummies Questions & Answers

Comparing Output Date to Current System Date

Hi Guys, Anyone who knows how to compare the current date with the a file containing a date, say for example I have a file that looks like this: Command was launched from partition 0. ------------------------------------------------ Executing command in server server6 Fri Dec 16... (7 Replies)
Discussion started by: rymnd_12345
7 Replies

8. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

9. Shell Programming and Scripting

Converting line output to column based output

Hi Guys, I am trying to convert a file which has a row based output to a column based output. My original file looks like this: 1 2 3 4 5 6 1 2 3 1 2 3 (8 Replies)
Discussion started by: npatwardhan
8 Replies

10. Shell Programming and Scripting

how to make a line BLINKING in output and also how to increase font size in output

how to make a line BLINKING in output and also how to increase font size in output suppose in run a.sh script inside echo "hello world " i want that this should blink in the output and also the font size of hello world should be big .. could you please help me out in this (3 Replies)
Discussion started by: mail2sant
3 Replies
Login or Register to Ask a Question