2

I'm re-phrasing the question because it was highly confusing and was badly phrased... )-:

I have a docker container and I'm reading logs from it like this:

docker exec -it my-container sh -c "export TERM=xterm && ls -t /logs/my-log.* | xargs zless -R"

now, I want to use 'grep' on the output and I am facing a problem because this one is working:

docker exec -it my-container sh -c "export TERM=xterm && ls -t /logs/my-log.* | xargs zless -R | grep sometext"

while this one is not working:

docker exec -it my-container sh -c "export TERM=xterm && ls -t /logs/my-log.* | xargs zless -R" | grep sometext

(note that the 1st working command includes the grep within the command I send and in the 2nd one I run grep on the output)

it is important to me because I am running the command in a script and I want to allow running grep on this script's output.

  • What do you mean by multiple piped ones? – countermode Feb 7 '17 at 15:05
  • Something like that: scrpt my-container my-file | grep sometext | head -n 1 – Hovav Feb 7 '17 at 18:16
  • you could just mount external volumes onto your container for this logging then perform a simpler grep directly from the host to avoid having to docker exec onto the container ... I realize this may not always be possible or advisable yet its an option – Scott Stensland Oct 18 '18 at 11:05
2

the script is running 'exec -it', 't' stands for tty

zless is "thinking" it has a tty in front of it without the additional pipe...

removing the 't' solved the problem (although it is now acts like a zcat and not zless but since piping is working I can simply pipe it into less...)

1

I don't really see the point of the pipeline. zless is an interactive program, and the output of ls is to be looked at, not parsed.

Instead:

... sh -c 'zgrep PATTERN /logs/my-log.*'

or, if there are too many files,

... sh -c 'for pathname in /logs/my-log.*; do zgrep PATTERN "$pathname"; done'

or, more efficiently,

... sh -c 'find /logs/ -maxdepth 1 -type f -name "my-log.*" -exec zgrep PATTERN {} +'

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.