使用 watch -d 命令仅写入差异

使用 watch -d 命令仅写入差异

我必须使用watch以便在文件(doc.txt)中写入新版本文件和旧版本文件之间的差异。

我尝试过watch -t -d -n 10 "cat myfile.txt | tee doc.txt",但所有myfile.txt文本都是使用命令写入终端的,cat并且差异仅突出显示。并且在 中doc.txt,有与 myfile.txt 中相同的内容。

有任何想法吗?

答案1

看门人说

watch - execute a program periodically, showing output fullscreen

在哪里

 -n, --interval seconds
              Specify  update  interval.   The command will not allow quicker
              than 0.1 second interval, in which the smaller values are  con‐
              verted. Both '.' and ',' work for any locales.



 -d, --differences [permanent]
              Highlight the differences between successive  updates.   Option
              will read optional argument that changes highlight to be perma‐
              nent, allowing to see what has  changed  at  least  once  since
              first iteration.       
 -t, --no-title
              Turn off the header showing the interval, command, and  current
              time  at the top of the display, as well as the following blank
              line.

猫人说

 cat - concatenate files and print on the standard output

穿T恤的男人说

tee - read from standard input and write to standard output and files

这可能有效

每 10 秒观察 2 个版本的 doc.txt 之间的差异并将其写入文件 doc.txt

watch -t -d doc.txt -n 10 | tee new.txt

相关内容