Grep only specific lines ordered by column/date

Hi everybody,
I'd like to think I've been through the search tool not only on this site, but also on google too, but I haven't been able to find what I was looking for. If I might've missed something on this forum, please slap me in the face with a link that you consider useful for my query :smiley:

I got a bunch of backup jobs that I need to order and grep only for those with errors:

926717 Image De   Done      0                                                                                                                       08/28/11 07:58:30  08/28/11 08:01:08  000:02:38              1
926709 Image De   Done      0                                                                                                                       08/28/11 05:40:05  08/28/11 05:43:03  000:02:58              1
926679 Image De   Done      0                                                                                                                       08/28/11 03:51:07  08/28/11 03:53:15  000:02:08              1
938526   Backup   Done    196      ADDICTION_TRADEFIN_EXPORT                       Full                     addiction                               09/13/11 20:00:00  09/13/11 22:00:03  002:00:03              1
933442   Backup   Done    196      ADDICTION_TRADEFIN_EXPORT                       Full                     addiction                               09/06/11 20:00:00  09/06/11 22:00:03  002:00:03              1
928458   Backup   Done    196      ADDICTION_TRADEFIN_EXPORT                       Full                     addiction                               08/30/11 20:00:00  08/30/11 22:00:03  002:00:03              1
945927   Backup   Done    196       SQL_SERVER_FALCON_BACKUP                       Full                           BAZ                               09/23/11 21:00:00  09/23/11 23:00:03  002:00:03  

Notice there are jobs which finish with status code 0 (on column 4 - meaning they're successful) and those with 196 (un-successful). I would like to print out only the lines containing jobs that DON'T finish successfully and order them by date. What I've managed to do was to use the sort -k 4,7 (column 7 is the starting date of the job) but I would also need to exclude from the printed lines those that don't matter to me (for example jobs that have failed 2 days ago... if we're on Sept 25, I'd like to have only lines containing failed jobs from Sept 25, NOT Sept 23rd). I think this is easy for most of the gurus here, but I'm just a storage guy, so do forgive my lack of knowledge on this :o

It would be simpler if the date was stored in YYYYMMDD format.

Some of your field numbers were wrong. Field "2", for example, is actually two fields in some cases. It's possible that the actual output is tab-delimited, but you just didn't paste it properly here?

This will add an extra field with that data format, then sorting is easier...

$ awk '$2=="Backup" && $4 {split( $8, D, "/"); T=$9; gsub( /:/, "", T); print D[3]D[1]D[2]T, $0}' file1 | sort -nk1
110830200000 928458   Backup   Done    196      ADDICTION_TRADEFIN_EXPORT                       Full                     addiction                               08/30/11 20:00:00  08/30/11 22:00:03  002:00:03              1
110906200000 933442   Backup   Done    196      ADDICTION_TRADEFIN_EXPORT                       Full                     addiction                               09/06/11 20:00:00  09/06/11 22:00:03  002:00:03              1
110913200000 938526   Backup   Done    196      ADDICTION_TRADEFIN_EXPORT                       Full                     addiction                               09/13/11 20:00:00  09/13/11 22:00:03  002:00:03              1
110923210000 945927   Backup   Done    196       SQL_SERVER_FALCON_BACKUP                       Full                           BAZ                               09/23/11 21:00:00  09/23/11 23:00:03  002:00:03

The last bit about getting the last two days I leave to you (or anyone else) :).

1 Like

I think I got what I wanted by doing just a little bit more search :slight_smile:

bpdbjobs | sort -rk 4,7 | awk '{ if ($4 == "196") print $0 }' | sort -rk 8

That gives me something like this:

946571   Backup   Done    196                          thumb           Differential-Inc                         thumb                               09/24/11 18:35:00  09/25/11 00:00:08  005:25:08              1
946570   Backup   Done    196                          thumb           Differential-Inc                         thumb                               09/24/11 18:35:00  09/25/11 00:00:08  005:25:08              1
946569   Backup   Done    196                          thumb           Differential-Inc                         thumb                               09/24/11 18:35:00  09/25/11 00:00:08  005:25:08              1
946278   Backup   Done    196                     dollar_new                       Full                        Dollar                               09/24/11 04:01:27  09/24/11 06:00:03  001:58:36              1
946277   Backup   Done    196                     dollar_new                       Full                        Dollar                               09/24/11 04:01:27  09/24/11 06:00:03  001:58:36              1
946276   Backup   Done    196                     dollar_new                       Full                        Dollar                               09/24/11 04:01:27  09/24/11 06:00:03  001:58:36              1
946275   Backup   Done    196                     dollar_new                       Full                        Dollar                               09/24/11 04:01:27  09/24/11 06:00:03  001:58:36              1
946274   Backup   Done    196                     dollar_new                       Full                        Dollar                               09/24/11 04:01:27  09/24/11 06:00:03  001:58:36              1
946273   Backup   Done    196                     dollar_new                       Full                        Dollar                               09/24/11 04:01:27  09/24/11 06:00:03  001:58:36              1
946272   Backup   Done    196                     dollar_new                       Full                        Dollar                               09/24/11 04:01:27  09/24/11 06:00:03  001:58:36              1
946271   Backup   Done    196                     dollar_new                       Full                        Dollar                               09/24/11 04:01:27  09/24/11 06:00:03  001:58:36              1
946270   Backup   Done    196                     dollar_new                       Full                        Dollar                               09/24/11 04:01:27  09/24/11 06:00:03  001:58:36              1
945954   Backup   Done    196                 atm_monitoring                       Full atm_monitoring.alkhaliji.bank                               09/23/11 21:25:00  09/24/11 06:00:03  008:35:03              1
945953   Backup   Done    196                 atm_monitoring                       Full atm_monitoring.alkhaliji.bank                               09/23/11 21:25:00  09/24/11 06:00:03  008:35:03              1
945952   Backup   Done    196                 atm_monitoring                       Full atm_monitoring.alkhaliji.bank                               09/23/11 21:25:00  09/24/11 06:00:03  008:35:03     

Oh, yes, I should have been looking for "196", than assuming "not 0" meant error.

The odd American date format might save you this year, but not sure how well that sort will hold up when 2012 comes along.

Which would come first?

09/23/11
01/01/12

At least with most other date formats, you'd only have to wait until October for it to break - less time to forget what you did!

23/09/11
01/10/11

But the international format will never break

20120101
20110923

Did I labour that point too much? :slight_smile:

1 Like

If predictions are right, it's not only the American date format that will suffer, but all of us... :wink: and yeah, thanks for the effort Scott, but you went way beyond what I actually needed... It's like in string theory: it's absolutely wonderful mathematically, but no one can prove it right...:slight_smile: