Redirect only the last line of STDOUT to a file

前端 未结 5 1129
清歌不尽
清歌不尽 2020-12-16 17:41

I\'m compiling Scala code and write the output console output in file. I only want to save the last line of the STDOUT in a file. Here is the command:

scalac         


        
相关标签:
5条回答
  • You could use tail:

    scalac -Xplugin:divbyzero.jar Example.scala | tail -1 >> output.txt
    
    0 讨论(0)
  • 2020-12-16 18:32
    scalac ... | awk 'END{print>>"output.txt"}1'
    

    This will pipe everything through to stdout and append the last line to output.txt.

    0 讨论(0)
  • 2020-12-16 18:34

    Just pipe stdout through tail -n 1 to your file

    0 讨论(0)
  • 2020-12-16 18:35

    Just a small precision regarding this tail command. If the program output on standard error, you have to redirect it

    Example:

    apachectl -t 2>&1 | tail -n 1
    

    Redirections: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html

    0 讨论(0)
  • 2020-12-16 18:36

    In Bash and other shells that support process substitution:

    command | tee  >(tail -n 1 > outputfile)
    

    will send the complete output to stdout and the last line of the output to the file. You can do it like this to append the last line to the file instead of overwriting it:

    command | tee  >(tail -n 1 >> outputfile)
    
    0 讨论(0)
提交回复
热议问题