Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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

share|improve this question
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
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
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

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
share|improve this answer

cut -c 1-10 -|sort -ru


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

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

Your Answer


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.