Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a program, which produces output like this (iso format dates):

2010-06-04 15:48:17 +0200
2010-06-04 12:34:39 +0200
2010-02-01 14:02:44 +0200
2010-06-04 12:21:19 +0200
2010-06-04 12:21:04 +0200
2009-05-02 15:38:14 +0200
2009-03-02 15:38:09 +0200
2010-06-04 14:45:00 +0200
...

How do I convert the output to unique, sorted dates:

2010-06-04
2010-02-01
2009-05-02
2009-03-02

And then, on each unique date run: mycommand --withargs [yyyy-mm-dd]?

share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

You can do the following:

$ <iso_command> | awk '{print $1}' | sort -u | xargs -n 1 mycommand --withargs
share|improve this answer
    
Thanks, man awk was what I needed :) –  takeshin Jun 4 '10 at 19:49
1  
Watch out for timezones with this. If you get data that has multiple time zones you may have problems. –  Zoredache Jun 4 '10 at 20:00
1  
I would use something like: while read line; do date +%Y-%m-%d -d "$line"; done date can parse many formats and also take care of the timezones –  Daniel Jun 4 '10 at 20:05
add comment

Using a simple text parsing tool may not be the best method if you are ever going to be getting data from altnerate time zones.

It may be better to past the formated string to the date command and then use it reformat however you like. A simple awk or cut could return results that are not entirely accurate. For example if you where in PST but the data you got was in UTC the date would be off by one 7 out of 24 hours during the data.

$echo '2010-06-04 15:48:17 +0200' | xargs -I var_d date --date='var_d' +%Y-%m-%d
2010-06-04
share|improve this answer
add comment

cut -c 1-10 -|sort -ru

or

cut -c 1-10 /path/to/myfile |sort -ru

share|improve this answer
    
... and complete that with xargs ... from Tekhne's answer! –  Chris Lercher Jun 4 '10 at 19:05
add comment

Assuming date from GNU coreutils,

program > dates
while read isodate; do
    ymd=$(date -d "$isodate" +'%Y-%m-%d')
    mycommand --withargs "$ymd"
done < dates
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.