0

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]?

3

You can do the following:

$ <iso_command> | awk '{print $1}' | sort -u | xargs -n 1 mycommand --withargs
3
  • 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
2

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
0

cut -c 1-10 -|sort -ru

or

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

1
  • ... and complete that with xargs ... from Tekhne's answer! Jun 4 '10 at 19:05
0

Assuming date from GNU coreutils,

program > dates
while read isodate; do
    ymd=$(date -d "$isodate" +'%Y-%m-%d')
    mycommand --withargs "$ymd"
done < dates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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