如果没有重复行,Bash会添加到文件末尾(>>)

Pez*_*kow 3 bash logging

通常我会在我的服务器上运行的进程使用类似的东西

./runEvilProcess.sh >> ./evilProcess.log
Run Code Online (Sandbox Code Playgroud)

但是我现在正在使用Doxygen,它会产生大量的重复输出

示例输出:

QGDict::hashAsciiKey: Invalid null key
QGDict::hashAsciiKey: Invalid null key
QGDict::hashAsciiKey: Invalid null key
Run Code Online (Sandbox Code Playgroud)

所以你最终会得到一个非常混乱的日志

有没有办法我只能将行添加到日志文件中,如果该行不是添加的最后一行.

一个糟糕的例子(但不确定如何在bash中做)

$previousLine = ""
$outputLine = getNextLine()
if($previousLine != $outputLine) {
  $outputLine >> logfile.log
  $previousLine = $outputLine
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*Mat 5

如果进程连续返回重复的行,则通过uniq以下方式管理进程的输出:

 $ ./t.sh 
one
one
two
two
two
one
one
 $ ./t.sh | uniq
one
two
one
Run Code Online (Sandbox Code Playgroud)

如果将日志发送到标准错误流,您还需要重定向:

 $ ./yourprog 2>&1 | uniq >> logfile
Run Code Online (Sandbox Code Playgroud)

(如果重复项来自程序的多次运行,这将无济于事 - 但是uniq当您查看它时,您可以通过管道传输日志文件.)