2

I run an application and pipe the output to grep. This works. However, I wish to show the output on both screen and write to file.

So I am piping the grep result to tee:

./app | grep something | tee file.txt

However, this is not showing (or writing) results, even when there are 100% results (I've confirmed this with two running instances).

How do I grep the app's output, write to file and show on screen?

1 Answers1

2

The command, as you wrote it, should work. There may be several reasons for the incorrect function of the command.

  1. The app sends the output text to another output than standard.
  2. The character chain for search with the grep command is incorrectly entered.

Solution of the case #1

  1. Check manually that the app sends the desired text to the output to be searched. ./app
  2. Redirect the output text to a file ./app > file.txt and check the contents of the file: cat file.txt
  3. If the file does not contain the desired text, it is likely that the application sends the output to the error output, rather than the standard output. So try redirecting to a file in a different way
    ./app 2 > file.txt and check the contents of the file again: cat file.txt
  4. If the content in the file is correct this time, the solution is easy. You only need to redirect the error output to the standard output.
./app 2>&1 | grep "something" | tee file.txt

Solution of the case #2

  1. Carefully check the string of characters you are using as a search pattern. Enclose it in quotation marks or apostrophes to exclude random misinterpretations of wildcard characters, $variablename variables, regular expressions, etc.
  2. Start by testing the search using the grep for very simple strings made up only of letters or numbers that cannot be misinterpreted.
netbat
  • 1,221