{"id":3249,"date":"2018-07-02T22:52:58","date_gmt":"2018-07-03T03:52:58","guid":{"rendered":"http:\/\/lisa.rushworth.us\/?p=3249"},"modified":"2018-07-02T22:52:58","modified_gmt":"2018-07-03T03:52:58","slug":"sorting-grep-results-when-log-file-names-are-incremented-integers","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=3249","title":{"rendered":"Sorting Grep Results When Log File Names Are Incremented Integers"},"content":{"rendered":"<p>While rotated log files can have a timestamp like YYYYMMDDHHmmss appended, a lot of log files are rotated with incremented integers (i.e. file.3 is removed, file.2 becomes file.3, file.1 becomes file.2, file becomes file.1, and file is a new file for current log &#8216;stuff&#8217;). We had some &#8230; challenges changing the log4j settings to use the timestamp format. When you grep\u00a0<em>all<\/em> of the log files, the results are organized by alpha-sorted file names. Problem is that alpha sorted file names are 1, 10, 11, 12, &#8230;, 19, 2, 20, 21 &#8230; which doesn&#8217;t produce results in ascending or descending date order. You get:<\/p>\n<p>zwave.log.1:2018-07-01 22:51:21.450 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 58: Node not awake!<br \/>\nzwave.log.1:2018-07-01 22:51:21.450 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 55: Node not awake!<br \/>\nzwave.log.11:2018-07-02 00:47:05.375 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 70: Node not awake!<br \/>\nzwave.log.11:2018-07-02 00:47:05.376 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 46: Node not awake!<br \/>\nzwave.log.12:2018-07-02 01:01:12.850 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 82: Node not awake!<br \/>\nzwave.log.13:2018-07-02 01:11:22.465 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 48: Node not awake!<br \/>\nzwave.log.13:2018-07-02 01:11:22.478 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 82: Node not awake!<br \/>\nzwave.log.13:2018-07-02 01:11:22.478 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 49: Node not awake!<br \/>\nzwave.log.14:2018-07-02 01:25:22.632 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 112: Node not awake!<br \/>\nzwave.log.14:2018-07-02 01:25:22.632 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 212: Node not awake!<br \/>\nzwave.log.15:2018-07-02 01:40:29.053 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 58: Node not awake!<br \/>\nzwave.log.15:2018-07-02 01:40:29.053 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 70: Node not awake!<br \/>\nzwave.log.16:2018-07-02 01:55:01.702 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 50: Node not awake!<br \/>\nzwave.log.16:2018-07-02 01:55:01.702 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 122: Node not awake!<br \/>\nzwave.log.17:2018-07-02 02:08:48.818 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 67: Node not awake!<br \/>\nzwave.log.17:2018-07-02 02:08:48.818 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 212: Node not awake!<br \/>\nzwave.log.18:2018-07-02 02:23:53.281 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 55: Node not awake!<br \/>\nzwave.log.18:2018-07-02 02:23:53.281 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 46: Node not awake!<br \/>\nzwave.log.19:2018-07-02 02:44:23.567 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 48: Node not awake!<br \/>\nzwave.log.19:2018-07-02 02:44:23.567 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 86: Node not awake!<br \/>\nzwave.log.19:2018-07-02 02:44:23.567 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 50: Node not awake!<br \/>\nzwave.log.2:2018-07-01 23:03:36.704 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 57: Node not awake!<br \/>\nzwave.log.2:2018-07-01 23:03:36.704 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 64: Node not awake!<br \/>\nzwave.log.2:2018-07-01 23:03:36.704 [DEBUG] [ng.zwave.internal.protocol.ZWaveTransactionManager] &#8211; NODE 68: Node not awake!<\/p>\n<p>So I put together a quick sequence of commands that will produce results sorted by date. This process strips off the file names (I could create a more complex command set to shuffle the file name off to the end, but I didn&#8217;t need the file name. I just needed to scroll through the times at which an event occurred).<\/p>\n<p>grep &#8220;Node not awake&#8221; zwave.log* | sed -r &#8216;s\/^zwave.log.*:2018\/2018\/&#8217; | sort<\/p>\n<p>The grep results are piped to sed to have the file name prefix removed. The subsequent results are then piped to sort to be, well,\u00a0<em>sorted<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While rotated log files can have a timestamp like YYYYMMDDHHmmss appended, a lot of log files are rotated with incremented integers (i.e. file.3 is removed, file.2 becomes file.3, file.1 becomes file.2, file becomes file.1, and file is a new file for current log &#8216;stuff&#8217;). We had some &#8230; challenges changing the log4j settings to use &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[44,396],"class_list":["post-3249","post","type-post","status-publish","format-standard","hentry","category-system-administration","tag-openhab","tag-system-administration"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/3249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3249"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/3249\/revisions"}],"predecessor-version":[{"id":3250,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/3249\/revisions\/3250"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}