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

Jul*_*lia 1 grep tee

我想查看脚本的整个输出,但只将匹配“ vlim”的行保存到新文件。我几乎想通了,但不能完全按照我想要的方式工作。我目前在使用之间陷入困境:

python gmakemovie.test movie.cfg.test --overwrite | tee >(grep vlim) /output.txt
grep vlim output.txt > vlim.txt
Run Code Online (Sandbox Code Playgroud)

要么

python gmakemovie.test movie.cfg.test --overwrite | grep vlim | tee  /output.txt
Run Code Online (Sandbox Code Playgroud)

最上面的选项显示我的整个输出,但也将所有输出复制到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]
Run Code Online (Sandbox Code Playgroud)

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

vlim: [-6.59883562 -4.09629962]
vlim: [-0.46031536  0.83438775]
Run Code Online (Sandbox Code Playgroud)

我在Mac上的终端上工作

Kus*_*nda 5

这将是最容易的

$ datagenerator | tee outfile-complete | grep 'pattern' >outfile-filtered &
$ less outfile-complete
Run Code Online (Sandbox Code Playgroud)

如果它是一个长期运行的脚本,这将允许你以监测进展情况less(使用Fless得到它的像tail -f),而作业运行作为后台作业。

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

$ datagenerator | tee >( grep 'pattern' ) output
Run Code Online (Sandbox Code Playgroud)

只需很小的改动就可以实现您的预​​期目标:

$ datagenerator | tee >( grep 'pattern' >outfile-filtered ) output-complete
Run Code Online (Sandbox Code Playgroud)

grep在子shell是写在标准输出。更改意味着已过滤的数据将转至文件,而完整的输出将转至控制台和output-complete

因此,现在您有两种解决方案,它们的功能略有不同。