0

I would like to monitor the output of a command and save parts of its output as it prints using a filter. Using grep would be a way to filter the output but I can only get it to show the matches, but it's not what I want.

In my case I am running curl on a list of URLs and would like to see its results, while only saving the results that has a status code of 000 or 200 to a text file.

MrU
  • 129
  • 1
  • 8

1 Answers1

4

Use tee /dev/stderr to duplicate output.

for example:

seq 9 | tee /dev/stderr | grep "[42]" > result.txt

seq 9 outputs 1 to 9, tee /dev/stderr duplicate output to stderr, grep "[42]" to filter output and save to file result.txt

or, using process substitution

seq 9 | tee >(grep "[42]" > result.txt)

We can treat >(grep "[42]" > result.txt) as a file. So tee >(grep "[42]" > result.txt) will duplicate output to stdout and save to this special file. But the file is a process, which is grep "[42]" > result.txt.

Feng
  • 3,592
  • 2
  • 15
  • 14