1

How can I match a more complex pattern when using cat or zcat? For example, I know I can do this:

zcat /var/log/nginx/access.log.*.gz

Which will output all the gzipped access logs to stdin.

What if I want a more complex pattern? Say, all the log files that are between 1-15, e.g. something like this pattern:

zcat /var/log/nginx/access.log.([1-9]|1[0-5]).gz

This results in an unexpected token which is obvious, but I'm not sure how I'd escape the regex in this situation? Maybe I need to pipe ls output to zcat instead?

Bartek
  • 15,269
  • 2
  • 58
  • 65
  • The pattern you used can just be modified very slightly, to `@([1-9]|1[0-5])`, to make it an extglob. See https://wiki.bash-hackers.org/syntax/pattern#extended_pattern_language -- btw, that's not cat/zcat doing the pattern match, it's the shell; globs are extended _by the shell_ before whatever program they're starting is run (on UNIX; on Windows it's done the other way around). – Charles Duffy Jun 03 '20 at 01:10
  • BTW -- if you want fancier matching that's [a job for `find`](https://mywiki.wooledge.org/UsingFind#Searching_based_on_names), which supports both glob-style patterns and explicit regular expressions; [`ls` is explicitly not suited to the task](https://mywiki.wooledge.org/ParsingLs). – Charles Duffy Jun 03 '20 at 01:13
  • (note that `shopt -s extglob` needs to be run before extended globbing expressions will work). – Charles Duffy Jun 03 '20 at 01:15

1 Answers1

2

It depends of course on specifically what pattern you want to match. For the example you have given of log files 1-15, you could use something like

cat /var/log/nginx/access.log.{1..15}.gz

which will complain to stderr if any of those numbers don't exist, but it will still concatenate the rest to stdout.

This technique is a "sequence expression" if you want to look it up - it's a part of brace expansion.

lxop
  • 7,596
  • 3
  • 27
  • 42