如何备份每个管道程序的输出?

如何备份每个管道程序的输出?

我每天都使用管道程序进行数据分析:
alias analyze='fetch_data | prog1 | prog2 | prog3 > result.txt'
这个脚本大部分情况下运行良好,但失败的概率约为 1%。由于一遍又一遍地运行是相当耗时的,所以我希望能够备份每个过程的结果,例如:

/tmp/2017-10-31-10am/fetch_data.txt
/tmp/2017-10-31-10am/prog1.txt
/tmp/2017-10-31-10am/prog2.txt
/tmp/2017-10-31-10am/prog3.txt

答案1

要让别名根据今天的日期以格式/tmp/YYYY-MM-DD-HH(后跟 am 或 pm)在 /tmp 中生成目录,然后将所有中间输出保存到单独的文件中:

alias analyze='d=$(date +"/tmp/%Y-%m-%H%p"); mkdir "$d" && fetch_data | tee "$d/fetch_data.txt" | prog1 | tee "$d/prog1.txt" | prog2 | tee "$d/prog2.txt" | prog3 > "$d/result.txt"'

或者,分成多行:

alias analyze='
  d=$(date +"/tmp/%Y-%m-%H%p")
  mkdir "$d" && 
  fetch_data | tee "$d/fetch_data.txt" | 
  prog1 | tee "$d/prog1.txt" | 
  prog2 | tee "$d/prog2.txt" | 
  prog3 > "$d/result.txt"
  '

这依赖于date生成目录名称/模板和tee用于将传入数据复制到文件并将其沿管道传递的实用程序。

运行示例(用编造的程序代替 fetch_data 等):

alias a='
  d=$(date +"/tmp/%Y-%m-%H%p")
  mkdir "$d" && 
  seq 10 | tee "$d/seq.out" | 
  head -6 | tee "$d/head.out" | 
  sed s/3/j/ | tee "$d/sed.out" | 
  tail -4 > "$d/result.txt"
  '

结果:

$ ls /tmp/2017-10-19PM/
head.out  result.txt  sed.out  seq.out

$ cat /tmp/2017-10-19PM/seq.out
1
2
3
4
5
6
7
8
9
10

$ cat /tmp/2017-10-19PM/head.out
1
2
3
4
5
6

$ cat /tmp/2017-10-19PM/sed.out
1
2
j
4
5
6

$ cat /tmp/2017-10-19PM/result.txt
j
4
5
6

答案2

我不确定你在问什么。如果您想保存每个程序的输出,请执行以下操作

fetch_data | tee f_d.txt | prog1 | tee prog1.txt | prog2 | tee prog2.txt | prog3 > result.txt

(我缩写fetch_data.txtf_d.txt只是为了便于阅读。)不会prog3.txt与 相同吗result.txt

相关内容