0

We want to search string by grep to both files:

The files are

/confluent/logs/server.log
/confluent/logs/server.log.1

But we not want to match the other files as

/confluent/logs/server.log.2
/confluent/logs/server.log.3 

etc

So instead to do double grep as

grep log.retention.bytes  /confluent/logs/server.log
grep log.retention.bytes  /confluent/logs/server.log.1

we want to find the match of log.retention.bytes on both files on the same time

we try to do

grep log.retention.bytes   /opt/mcspace/confluent/logs/server.log.*[1]

but this is wrong

9
grep log.retention.bytes server.log{,.1}

In order to keep log entries (appended) in chronological order, you might want to reverse the order of files:

grep log.retention.bytes server.log{.1,}

which is of course equivalent to:

grep log.retention.bytes server.log.1 server.log

as the brace expansion is done by the shell before executing the grep command.

Moreover, with zsh shell you can easily automatically glob for the last N files matching a pattern with:

grep log.retention.bytes server.log*(Om[-2,-1])

where Om means order by mtime descending and [-2,-1] fetches 2 last rows. This trick is worth remembering if you watch to search more files and do not want to type them manually.

| improve this answer | |
  • with your answer you can get score 100 or more – yael Jul 20 at 12:32
  • Please note that although accidental matches are unlikely in this case, I would recommend to escape the . in the regular expressions since they are to match literal . in the files ... – AdminBee Jul 20 at 12:35
  • Shell expansion doesn't use regexps (at least by default), so there's no need to escape dots. – Tomasz Pala Jul 20 at 12:37
  • 1
    Yes, but grep searches based on regexps, so at least the log\.retention\.bytes may warrant escaping. – AdminBee Jul 20 at 12:50
  • Oh, the grep pattern - yes, this might be the subject of escaping, however since I don't know what the OP was exactly searching for (maybe log-retention_bytes as well?) this is out of the scope of my answer, I focus on shell globing for appropriate files selection as an argument to any command. – Tomasz Pala Jul 20 at 12:54
4

The simplest way to grep in two files, is to give both files to grep:

grep log.retention.bytes /confluent/logs/server.log /confluent/logs/server.log.1

When you have more files, you can either list them all manually or use a glob that matches only your specific files. For example, in bash:

shopt -s extglob
grep log.retention.bytes /confluent/logs/server.log?(.1)

Finally, and probably most simple in your case, you can use brace expansion as shown in Tomassz's answer.

| improve this answer | |
  • thank you about the effort -:) – yael Jul 20 at 14:31

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.