by a3_nm · 14y ago · 0 comments
cut -d ' ' -f1 log | sort | uniq -c | sort -nr | head
Say you need to follow accesses to a particular file? The following quick and dirty one-liner probably works well enough: tail -f log | grep --line-buffered file.pdf
How do you do that with json?Granted, as soon as your logs stop being a sequence of records (lines) with a fixed sequence of neatly delimited records, you will need something more than text. However, I still don't know of tools to work with json from the command line that are as concise, efficient, flexible and robust as the standard unix utilities for text.
Depends on the schema used, but it would probably be something like
jshon -a -e "ip" -u < log | sort | uniq -c | sort -nr | headI love plain-text logs, but that apache logformat is a bad example of "neatly delimited records", given the unstable mixture of spaces between and within fields (some of which are quoted, some not, and one even quoted with square brackets!).
In your current pipeline just replace `cut` with an XPath expression, everything else stays practically the same.
Of course current XML ecosystem has different tools that makes your case trivial, though that’s something new to learn.