[英]I want to pipe an output to multiple files in bash
I looked up how to do it but don't understand how to use tee
. 我抬头看怎么做,但不知道如何使用
tee
。 It is a little confusing because I am using an output from awk
. 这有点令人困惑,因为我正在使用
awk
的输出。 Here is what I have so far: (keep in mind that I am a beginner) 这是我到目前为止的内容:(请记住,我是一个初学者)
num1=2
num2=4
awk 'if (/'$num1'/ == /'$num2'/)
{
print "Hello"
}
else
{
print "Goodbye"
}' | tee file1.txt file2.txt
I don't know why the output from awk
is not being printed in the text files. 我不知道为什么
awk
的输出未在文本文件中打印。
Your awk command has syntax errors. 您的awk命令包含语法错误。 @anubhava has given you a fix for that.
@anubhava已为您解决了这一问题。
To demonstrate tee
, let's do something simpler: 为了演示
tee
,让我们做一些简单的事情:
$ date | tee file1.txt file2.txt
Thu Jun 12 10:44:26 EDT 2014
$ cat file1.txt
Thu Jun 12 10:44:26 EDT 2014
$ cat file2.txt
Thu Jun 12 10:44:26 EDT 2014
To conclude: your invocation of tee
is fine. 结论:您对
tee
的调用很好。 Your output files do not contain any text because your awk
program is only printing it's error messages to stderr and nothing to stdout. 您的输出文件不包含任何文本,因为您的
awk
程序仅将其错误消息打印到stderr而不输出到stdout。 If you want all of awk's output to go into the files, redirect awk's stderr to stdout: 如果要将awk的所有输出都放入文件中,请将awk的stderr重定向到stdout:
awk '...' 2>&1 | tee ...
I'll leave that explanation as an exercise 我将把这个解释留作练习
Use awk -v name=value
option to pass shell variables to awk: 使用
awk -v name=value
选项将shell变量传递给awk:
num1=2
num2=4
awk -v n1=$num1 -v n2=$num2 'BEGIN {if (n1 == n2) print "hello"; else print "Goodbye"}'
| tee file1.txt
To redirect output from awk
itself: 要从
awk
本身重定向输出:
awk -v n1=$num1 -v n2=$num2 'BEGIN {
if (n1 == n2) print "hello"; else print "Goodbye"}' > output.txt
Piping output to tee
will write the output to the file you indicate and write the output to stdout. 将输出管道传输到
tee
会将输出写入您指定的文件,并将输出写入stdout。 Thus, if you want to write the output to multiple files, you can just pipe to another tee
like so: 因此,如果要将输出写入多个文件,则可以通过管道将其传输到另一个
tee
如下所示:
bbbco $ echo "hello world" | tee /tmp/helloworld1.txt | tee /tmp/helloworld2.txt
hello world
bbbco $ cat /tmp/helloworld1.txt
hello world
bbbco $ cat /tmp/helloworld2.txt
hello world
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.