[英]tee bash command with redirection
I have the following file:我有以下文件:
file1.txt文件1.txt
geek
for
geeks
I am using the tee
command to perform two operations on the output.我正在使用
tee
命令对输出执行两个操作。 My question is about the the redirection character after the first tee
.我的问题是关于第一个
tee
之后的重定向字符。 I want to get the first column of file1.txt and write it to file2.txt.我想获取file1.txt的第一列并将其写入file2.txt。 When I run the following command, I don't receive an error but it does not give me the first column:
当我运行以下命令时,我没有收到错误,但它没有给我第一列:
wc -l file1.txt |tee awk '{print $1}' - > file2.txt | sed 's/4/6/g' > file3.txt
However, the following command works as expected.但是,以下命令按预期工作。 What does the
>
is doing here? >
在这里做什么?
wc -l file1.txt |tee >(awk '{print $1}' - > file2.txt) | sed 's/4/6/g' > file3.txt
tee awk '{print $1}' - > file2.txt
does:做:
tee
with 3 arguments awk
and '{print $1}'
and -
.awk
和'{print $1}'
和-
执行tee
。tee
will create a file named awk
, another file named '{print $1}'
and yet another file named -
. tee
将创建一个名为awk
的文件、另一个名为'{print $1}'
文件和另一个名为-
文件。tee
will be redirected to file2.txt
tee
的输出将被重定向到file2.txt
tee
will duplicate input to those 3 files and will output to file2.txt
tee
将复制输入到这 3 个文件并输出到file2.txt
| sed
| sed
| sed
will receive no input, because the output of tee
is redirected to the file and the subshell outputs nothing. | sed
将不会收到任何输入,因为tee
的输出被重定向到文件并且子 shell 不输出任何内容。tee >(awk '{print $1}' - > file2.txt)
does:做:
>(...)
awk
with two arguments '{print $1}'
and -
'{print $1}'
和-
运行awk
'{print $1}'
is interpreted as a script '{print $1}'
被解释为脚本-
is interpreted as stdin (and could be omitted) -
被解释为标准输入(并且可以省略)awk
of redirected to file2.txt
awk
的输出重定向到file2.txt
bash
creates a fifo or a /dev/fd/something
filebash
创建一个 fifo 或一个/dev/fd/something
文件awk
processawk
进程的标准输入>(awk ...)
is substituted for the filename of the file, most probably for /dev/fd/something
>(awk ...)
被替换为文件的文件名,很可能替换为/dev/fd/something
tee >(...)
tee /dev/fd/something
tee /dev/fd/something
/dev/fd/something
is connected to awk process on the other side /dev/fd/something
连接到另一端的 awk 进程tee
writes to /dev/fd/something
and awk
reads the data from stdin on the other sidetee
写入/dev/fd/something
并且awk
从另一端的 stdin 读取数据tee
is redirected to | sed
tee
的输出被重定向到| sed
| sed
What does the > is doing here?
> 在这里做什么?
The first occurrence is used to introduce a process substitution.第一次出现用于引入进程替换。 The second occurrence is used to redirect output of
awk
command to a file named file2.txt
.第二次出现用于将
awk
命令的输出重定向到名为file2.txt
的文件。 The third occurrence is used to redirect the output of sed
command to file named file3.txt
.第三次出现用于将
sed
命令的输出重定向到名为file3.txt
文件。
Here, Process substitution is used to capture output that would normally go to a file在这里,进程替换用于捕获通常会转到文件的输出
The Bash syntax for writing to a process is >(command)
写入进程的 Bash 语法是
>(command)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.