Zcat and zgrep

UPDATE 2016-04-26: thanks for article here http://unix.stackexchange.com/questions/77296/is-there-a-tool-that-combines-zcat-and-cat-transparently Forget my lcat function, just use the already provided zgrep and zcat commands.

FORMAT:

Examples (all of these work):

Note: dont forget the — with zcat,  zgrep seemed to have worked without the —

Note: put all of your grep options before the double dashes –. After the — should follow pattern and filenames. Double dash tells bash that the single and double dashes arguments have been completed (so that filenames or PATTERN space doesnt expand to a dash)


Old Lcat Article

lcat – reads through all gzipped and regular logs in chronological order. I provide them as a script and also as function.

Common files that this will be useful for: syslog, messages, samba logs, anything that gets rotated.

NOTE: code for lcat is half way down the page

We have all seen logs that look like this (especially in Ubuntu). There is probably an app that does what this script/article is about to show, but either way here is my implementation. (if you know of such a method, please leave it in the comments)

Now lets say I want to look through the logs but I dont just wanna look thru

or

or better yet:

Note: how syslog.1 comes first as its chronologically first

Similarily you can read the gzipped ones with zcat (which on the fly uncompresses gzips and cats the contents – good for single files that got gzipped)

or in order

Or if you want to read all of them:

Now thats annoying, how do I just do a single command.

lcat script below

The above will do good as script that you can run like this

For reverse sort

Or if your just wanting a one liner without a script:

Anyhow this will be a golden script for looking thru logs that look like this:

To read the various logs here:

lcat2 script below – lcat2 differs from lcat by adding two features; 1. it tells you what it will read and in what order at the top of the output; 2. also puts a header before reading each file (useful for reading logged dmesg files – such as the dmesg listed above)

UPDATE 2016-04-18: provide functions of lcat

Lcat function

If you dont want to write a script. You can use bash functions. Just copy paste this into your .bashrc or directly into your running shell and you will have the lcat function installed (which is used just like a command). if your sick of it just run unset lcat and it will no longer be a function. Anytime you enter a new bash  you will not have lcat (unless its in your bashprofile or bashrc file) you will have to redo it.

To use just run “cd /var/log/; lcat messages;

Here is the other version of lcat which has headers, lcat2:

This is used the same way “cd /var/log/; lcat messages;

 

One thought on “zcat/zgrep & lcat, rotated log read, Read thru the gzipped & ascii logs chronologically

  1. I use zgrep and zless a lot.

    I never understood the widespread use of cat. I mean I’ll use it for files that are like a few lines (or on ones I want to use my mouse wheel to scroll through after they output.)

    But generally less and grep when I want to parse out things and have a general idea of what I’m looking for.

Leave a Reply

Your email address will not be published. Required fields are marked *