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

Mark Henderson
  • 68,823
  • 31
  • 180
  • 259
takeshin
  • 1,471
  • 3
  • 21
  • 28

4 Answers4

3

You can do the following:

$ <iso_command> | awk '{print $1}' | sort -u | xargs -n 1 mycommand --withargs
Tekhne
  • 299
  • 1
  • 6
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
Zoredache
  • 130,897
  • 41
  • 276
  • 420
0

cut -c 1-10 -|sort -ru

or

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

Chris Lercher
  • 4,152
  • 9
  • 35
  • 41
0

Assuming date from GNU coreutils,

program > dates
while read isodate; do
    ymd=$(date -d "$isodate" +'%Y-%m-%d')
    mycommand --withargs "$ymd"
done < dates
ephemient
  • 1,470
  • 1
  • 11
  • 8