简体   繁体   English

带重定向的 tee bash 命令

[英]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:做:

  • execute tee with 3 arguments awk and '{print $1}' and - .使用 3 个参数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}'文件和另一个名为-文件。
  • Then the output of 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
  • Consequently | 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:做:

  • >(...)
    • Run 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) ------ -被解释为标准输入(并且可以省略)
    • ------ then the output of awk of redirected to file2.txt ------ 然后将awk的输出重定向到file2.txt
    • Then bash creates a fifo or a /dev/fd/something file然后bash创建一个 fifo 或一个/dev/fd/something文件
    • Then the output of that file is connected to stdin of awk process然后该文件的输出连接到awk进程的标准输入
    • And the >(awk ...) is substituted for the filename of the file, most probably for /dev/fd/something并且>(awk ...)替换为文件的文件名,很可能替换/dev/fd/something
  • tee >(...)
    • executes tee with one argument, like tee /dev/fd/something用一个参数执行 tee,比如tee /dev/fd/something
    • The /dev/fd/something is connected to awk process on the other side /dev/fd/something连接到另一端的 awk 进程
    • So tee writes to /dev/fd/something and awk reads the data from stdin on the other side所以tee写入/dev/fd/something并且awk从另一端的 stdin 读取数据
    • the output of 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM