Search code examples
bashlogfile

Bash Terminal: Write only specific lines to logfile


I'm running simulation with lots of terminal output, which would exceed my disc space, if I'd save it to a logfile (e.g by "cmd > logfile"). Now I would like to follow the entire terminal output, but at the same time I would like to save specific data/lines/values from this output to a file.

1) Is there a way to that in bash?

2) Or as alternative: Is it possible to save the logfile, extract the needed data and then delete the processed lines to avoid generating a huge logfile?


Solution

  • If you want to save into logfile only the output containing mypattern but you want the see all the output at the terminal, you could issue:

    cmd 2>&1 | tee /dev/tty | grep 'mypattern' > logfile
    

    I also assumed that the output of cmd may be directed to the standard output stream as well as to the standard error stream, by adding 2>&1 after cmd.