使用 tee 和 grep 将某些输出行保存到文件中

Using tee and grep to save certain lines of output to a file

我想查看脚本的全部输出,但只将匹配 "vlim" 的行保存到新文件中。我几乎已经弄明白了,但还不能完全按照我想要的方式工作。我目前无法使用:

python gmakemovie.test movie.cfg.test --overwrite | tee >(grep vlim) /output.txt
grep vlim output.txt > vlim.txt

python gmakemovie.test movie.cfg.test --overwrite | grep vlim | tee  /output.txt

顶部选项显示了我的整个输出,但也将所有输出复制到 output.txt。底部选项仅复制 "vlim" 但不显示我的其余输出,因此我不知道我在脚本中的位置。

为清楚起见,我的输出如下所示:

imported pygad v0.4.32+ga1eacb4.dirty
from parameter file (fragmentary):
  snaps:      200 - 200
  width:      1000.0 [kpc]
  pixel:      500 x 500
  x - y:      x - y
  softening:  [  0.2    0.45   2.52  20.     0.2    0.2 ] [ckpc h_0**-1]
====================================================
get orientation from trace file "filepath":
  L:       [ 241.01309204  544.96875    -366.44366455] [1e+10 ckpc h_0**-1      Msol h_0**-1 km s**-1]
get first non-zero center from trace files
  from snapshot 200:
  center:  [ 36821.75390625  35120.65625     33730.06640625] [ckpc h_0**-1]
====================================================
run in serial
====================================================
read snapshot "filepath"
  z = 2.84615386294
  t = 2.33634681236 [Gyr]
get center and orientation from trace file "filepath":
  center:  [ 36821.75390625  35120.65625     33730.06640625] [ckpc h_0**-1]
  R200:    47.4815177362 [kpc]
center snapshot
orientate snapshot
render...
  axis 1: stars...
  im_lum = np.log10(im_lum)
vlim: [-6.59883562 -4.09629962]
  np.ndarray.__idiv__(self, x)
  axis 2: gas...
vlim: [-0.46031536  0.83438775]

我希望输出文件看起来像:

vlim: [-6.59883562 -4.09629962]
vlim: [-0.46031536  0.83438775]

我正在 mac

的终端工作

最简单的就是

$ datagenerator | tee outfile-complete | grep 'pattern' >outfile-filtered &
$ less outfile-complete

如果它是一个长 运行 脚本,这将允许您使用 less 监控进度(使用 less 中的 F 使其表现得像tail -f) 而该作业 运行 作为后台作业。

编辑: 实际上,仔细观察您的第一个命令:

$ datagenerator | tee >( grep 'pattern' ) output

只需微小的更改即可实现您的预​​期:

$ datagenerator | tee >( grep 'pattern' >outfile-filtered ) output-complete

子 shell 中的 grep 正在写入标准输出。更改意味着过滤后的数据改为转到文件,完整的输出转到控制台和 output-complete

所以现在你有两个解决方案,它们做的事情略有不同。