0

I get the following from spamdb, where the third field represents the time in seconds since the Epoch.

Cns# spamdb | fgrep TRAPPED
TRAPPED|113.163.117.129|1360836903
TRAPPED|113.171.216.201|1360837481
TRAPPED|122.177.159.61|1360844596
TRAPPED|36.231.9.231|1360865649
TRAPPED|37.146.207.209|1360832096
TRAPPED|212.156.98.210|1360837015
TRAPPED|59.99.160.62|1360839785
TRAPPED|86.127.116.162|1360840492
TRAPPED|92.83.139.194|1360843056
TRAPPED|219.71.12.150|1360844704

I want to sort this table by the time, and print the time field with date -r, such that it's presentable and clear when the event has occurred.

How do I do this in tcsh on OpenBSD?

Sorting with sort is easy, and so is editing with sed; but how do I make sed execute date -r or equivalent?

cnst
  • 25,870
  • 6
  • 90
  • 122

1 Answers1

0

There are indeed a few obstacles here: first, you basically have to separate the data, and then one part of it is presented as-is, whereas another part has to be passed down to date -r for date formatting, prior to being presented to the user.

Another obstacle is making sure the output is aligned: apparently, it's quite difficult to handle the tab character in the shell, possibly only on the BSDs:

Also, as we end up piping this to sh for execution, we have to use a different separator for the fields other than the pipe character, |.

So far, this is the best snippet I could come up with, it seems to work great in my tcsh:

Cns# spamdb | fgrep TRAPPED | sort -n -t '|' -k 3 | sed -E -e 's#\|#@#g' \
    -e 's#^([A-Z]+)@([0-9.]+)@([0-9]+)$#"echo -n \2_"; "date -r \3"#g' | \
    xargs -n1 sh -c | awk '{gsub("_","\t",$0); print;}'
37.146.207.209  Thu Feb 14 00:54:56 PST 2013
113.163.117.129 Thu Feb 14 02:15:03 PST 2013
212.156.98.210  Thu Feb 14 02:16:55 PST 2013
113.171.216.201 Thu Feb 14 02:24:41 PST 2013
59.99.160.62    Thu Feb 14 03:03:05 PST 2013
86.127.116.162  Thu Feb 14 03:14:52 PST 2013
92.83.139.194   Thu Feb 14 03:57:36 PST 2013
122.177.159.61  Thu Feb 14 04:23:16 PST 2013
219.71.12.150   Thu Feb 14 04:25:04 PST 2013
36.231.9.231    Thu Feb 14 10:14:09 PST 2013
Community
  • 1
  • 1
cnst
  • 25,870
  • 6
  • 90
  • 122