0

I would think this is easy but I'm blocking…

I have a directory with a series of log files (from minecraft server…):

2014-09-28-1.log.gz
2014-09-29-1.log.gz
2014-09-30-1.log.gz
2014-10-01-1.log.gz
2014-10-02-1.log.gz
latest.log

I would like to extract the date and time when users logged in. However, when I do

gzcat *.gz | grep 'logged in'

I get an output like this:

[19:26:03] [Server thread/INFO]: Foo[/ip address] logged in with entity id 193955 at (location)
[18:26:08] [Server thread/INFO]: Baz[/ip address] logged in with entity id 194873 at (location)
[01:16:38] [Server thread/INFO]: User[/ip address] logged in with entity id 198895 at (location)

etc.

Unfortunately, while I get the time stamp, I don't get the date. Ideally the output would be

2014-10-03 19:26:03 Foo logged in
2014-10-04 18:26:08 Baz logged in
2014-10-05 01:16:38 User logged in

I recognize I might need and awk script to get to that "ideal" output. But is there a shorthand (non awk) solution to get just the date interspersed with the listing? Something like this:

2014-10-03-1.log.gz
[19:26:03] [Server thread/INFO]: Foo[/ip address] logged in with entity id 193955 at (location)
2014-10-04-1.log.gz
[18:26:08] [Server thread/INFO]: Baz[/ip address] logged in with entity id 194873 at (location)
2014-10-05-1.log.gz
[01:16:38] [Server thread/INFO]: User[/ip address] logged in with entity id 198895 at (location)

(i.e. the name of the file before the grepped bits inside each file) would already be very helpful.

Note there are typically many other entries in each log file - there can be multiple logins, and other messages (which I am not interested in today).

In the shortest form, my question becomes:

Can I use gzcat in a way that it prints the name of every zip file before passing the contents of that zip file to grep for further processing?

I could write a script to do the whole thing; but I'm looking for something cleverer (I was wondering whether tee might be used to print file name to stderr as well as passing as argument to grep, perhaps.)

1 Answer 1

2

Is this gzcat provided by gzip package? On my openSUSE GNU/Linux machine, the binary is called zcat. If gzip is indeed installed on your machine, then you should also have another binary called zgrep, which is just a shell script that un-compresses files to stdout and invokes grep.

zgrep accepts (almost) the same set of arguments as grep. So you could do this:

$ zgrep -H 'logged in' *.gz`

This will return output in this format:

2014-10-03-1.log.gz:[19:26:03] [Server thread/INFO]: Foo[/ip address] logged in with entity id 193955 at (location)

You could may be replace .gz: with just a whitespace using either tr or sed to get something close to your ideal output.

1
  • This does the job very nicely. Thank you! I didn't realize there was a command that combined unzip and grep, and allowed me to include the file name as part of the output - brilliant. Congratulations on your first accepted answer, too!
    – Floris
    Oct 6, 2014 at 1:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .