2

I have rolling log files that are numbered such that most recent queries are in x.log until that file hits a threshold size, then it's renamed to x.log.1 and a new x.log starts.

I am writing a basic parser that goes through these files to extract some information, in chronological order. Which means I need to read the files in backwards order. So start with file x.log.10 process it line by line, then x.log.9 etc. until I finally process x.log which has the latest log entries.

I have a small python script for this purpose, I am just wondering how to invoke the command on the zsh CLI. I know I can use brace expansion x.log.{10..1} but naturally this would not include the most recent file x.log

Any ideas?

3
  • 2
    Wouldn't it be simpler to use the actual file mtime (which you can do in zsh via the (Om) glob qualifier)? – steeldriver Oct 13 '20 at 12:28
  • @steeldriver interesting idea, I had not thought of that. How would that work in practice? – posdef Oct 13 '20 at 12:44
  • I've added a brief illustration below – steeldriver Oct 13 '20 at 12:49
4

Zsh has glob qualifiers that may be used to modify the sort order. So for example, you could use

 print -rl x.log*(nOn)

to print the files with the numeric glob flag enabled (n), Ordered by name (On). This appears to place the non numeric x.log last in my locale.

Alternatively, instead of relying on the numeric order of the names, use the files' actual modification times:

print -rl x.log*(Om)

You may wish to add the N (null glob) qualifier as well. See man zshexpn for details.

4
  • Hmm I get zsh: unknown file attribute: 0 in both cases – posdef Oct 13 '20 at 12:56
  • 1
    @posdef it should be letter O not digit 0 – steeldriver Oct 13 '20 at 13:23
  • 1
    If you add N, you'll probably want to use print -rC1 instead of print -rl as well. I'd recommend info zsh qualifiers (and possibly install zsh-doc) instead of man zshexpn. – Stéphane Chazelas Oct 13 '20 at 16:34
  • 2
    @StéphaneChazelas thanks - my assumption is the OP will be using the glob in a shell loop like for log in x.log*(Om) so the print was only intended for illustration here – steeldriver Oct 13 '20 at 16:42

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.