OK, so this week I posted two other blog posts about doing quick and dirty log analysis and some of the techniques I use. This one also covers converting column logs to CSV.
After the great response, I wanted to drop one last tip for the week.
Several folks asked me about re-sorting and processing the column-based data in different ways and to achieve different analytical views.
Let me re-introduce you to my friend and yours, sort.
In this case, instead of using the sort -n -r like before (numeric sort, reverse order), we can use:
- sort -k# -n input_file (where # is the number of the column you’d like to sort by and the input file is the name of the file to sort)
- You can use this inline by leveraging the pipe (|) again – i.e.: cat input.txt | sort -k3 -n (this types the input file and sends it to sort for sorting on the third column in numeric order) (-r would of course, reverse it…)
- You can write the output of this to a file with redirects “> filename.txt”, i.e.: cat input.txt | sort -k3 -n -r > output.txt
- You could also use “>>” as the redirect in order to create a file if it doesn’t exist OR append to a file if it does exist… i.e..: cat input.txt | sort -k3 -n -r >> appended_output.txt
That’s it! It’s been a fun week sharing some simple command line processing tips for log files. Drop me a line on Twitter (@lbhuston) and let me know what you used them for, or which ones are your favorite. As always, thanks and have a great weekend!