leah blogs

July 2013

18jul2013 · Summer of Scripts: frep

frep is a pretty special tool, a mix of cut and uniq. I use it to get an overview of very similar data. For example, given a log file:

% cat log
2013-06-15 foo
2013-06-15 bar
2013-07-16 foo
2013-07-16 bar
2013-07-17 baz

We only want to see one line per day, so we filter consecutive duplicates of the first field (or, rather, we print when the first field changes):

% frep 1 < log
2013-06-15 foo
2013-07-16 foo
2013-07-17 baz

And sometimes we want to see the last line for each duplicate value:

% frep -l 1 < log
2013-06-15 bar
2013-07-16 bar
2013-07-17 baz

Finally, we also can refine the field seperator and for example, only list one line per month:

% frep -F- 1 2 < log
2013-06-15 foo
2013-07-16 foo

Another example would be to list which program uses most CPU, for each user:

% ps -eo pid,user,pcpu,comm --sort user,-pcpu | frep 2 
  PID USER     %CPU COMMAND
 3479 chris     2.8 firefox
  971 dbus      0.0 dbus-daemon
25256 dovecot   0.0 anvil
  973 mpd       0.0 mpd
  995 ntp       0.0 ntpd
 8724 postfix   0.0 qmgr
 2474 root      1.2 X
 4040 unbound   0.0 unbound

NP: Cameron Boucher—Life in Vain (Daniel Johnston Cover)

Copyright © 2004–2022