1

How to use regexp/pattern-searching under gunzipped files. For instance ummm... let's use -

/usr/share/doc/linux-image-4.8.0-1-amd64$ zcat changelog.gz | less

Now the way I use is when reading the contents via less, use the / to locate the name or whatever term I use but this doesn't work/scale well if name/term is repeated many a time.

I also tried -

 /usr/share/doc/linux-image-4.8.0-1-amd64$ zcat changelog.gz | grep $search-term | less

I do get the names/search-term but without the surrounding context as of date and other things.

Is there a way to get the search-term highlighted even if it duplicated n number of times while reading the changelog.gz

An example of what I mean https://gist.github.com/shirishag75/e1238c16d2d372c4cfc3f62e25da335a

As can be seen I do get the search term/regexp but without date-time context it is and can be somewhat meaningless without knowing when the changes happened.

  • 2
    Is it really relevant that the file is gziped for your question? You can use zgrep instead of zcat | grep. – jordanm Dec 3 '16 at 21:07
3

Use zgrep:

zgrep 'PATTERN' file.gz

Then you may pipe that on to a pager such as less if you wish.

On GNU systems (Linux), zgrep will behave just like GNU grep with regards to colouring etc.

To get context around each match use something like -C 5 (five lines of context).


less also does highlighting of matches (by default, unless turned off with -G):

zless -p 'PATTERN' file.gz

This will open the decompressed file in less and search for PATTERN. Use n to skip to the next match in the file, or N to go back to the previous match.

  • I still get the search -terms but again without date/time context. Editing the question to have more clarity. – shirish Aug 24 '17 at 2:10
  • @shirish I have updated my answer. – Kusalananda Aug 24 '17 at 6:16
1

To uncompress and see the matches in colour, in context, make use of the grep -C and --color arguments. Example below.

gzip -dc changelog.gz | grep -C 5 --color $search-term

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.