Linux pipeline, standard input and output

Standard input, output, redirection, pipes under Linux

In the Linux system, there are 4 special symbols, <, >, |, -, which are important but confusing when we deal with input and output.

By default, the results of Linux commands are all output to standard output, and error messages (such as command not found or file format recognition error, etc.) are output to standard error, and both standard output and standard error are displayed on the screen by default.

Indicates redirecting the standard output, > filenamethat is, storing the standard output in the file filename. Standard error will still be displayed on the screen.

2>&1Indicates redirecting standard error to standard output. The Linux terminal uses 2standard error 1to represent standard output.

-(Short horizontal line): Indicates standard input, generally used when a program requires multiple inputs.

Standard input, followed by commands that can generate output, is generally used when a program requires multiple inputs.

The pipe symbol means that the output of the previous command is used as the input of the next command. There are also some examples shown above. It is used for data transmission between different commands, and the purpose is to reduce the loss of hard disk access.

stdout_error.shLet's explain the above text through a program , as follows:

#!/bin/bash

echo "I am std output" 

# 下面是随便写的一个理论上不存在的命令, 理论上会报错的。
unexisted_command

Run this script:

# 标准输出和标准错误默认都会显示到屏幕上
$ bash stdout_error.sh 
I am std output
stdout_error.sh: line 5: unexisted_command: command not found


# >把结果输入到了文件;标准错误还显示在屏幕上
$ bash stdout_error.sh > stdout_error.stdout
stdout_error.sh: line 5: unexisted_command: command not found
$ cat stdout_error.stdout
I am std output


# > 把结果输入到了文件; 2> 把标准错误输入到了另一个文件
$ bash stdout_error.sh > stdout_error.stdout 2> stdout_error.stderr
$ cat stdout_error.stderr
stdout_error.sh: line 5: unexisted_command: command not found


# 标准输出和标准错误写入同一个文件
$ bash stdout_error.sh > stdout_error.stdout 2>&1
$ cat stdout_error.stdout
I am std output
stdout_error.sh: line 5: unexisted_command: command not found

Let's look at the use of pipe characters and standard input:

# 管道符的使用
# 第一个命令的输出作为第二个的输入
# 前面的例子中也有使用
# tr: 是用于替换字符的,把空格替换为换行,文字就从一行变为了一列
$ echo "1 2 3" | tr ' ' '\n'
1
2
3


# cat命令之前也用过,输出一段文字
# diff是比较2个文件的差异的,需要2个参数
# - (短横线)表示上一个命令的输出,传递给diff
# < 表示其后的命令的输出,也重定向给diff
$ cat <<END | diff - <(echo "1 2 3" | tr ' ' '\n')
> 2
> 3
> 4
> END
0a1
> 1
3d3
< 4


# 如果不使用管道和重定向标准输入,程序是这么写的
# 先把第一部分存储为1个文件
$ cat <<END >firstfile
2
3
> 4
> END
$ less firstfile 

# 再把第二部分存储为1个文件
$ echo "1 2 3" | tr ' ' '\n' >secondfile

# 然后比较
$ diff firstfile secondfile 
0a1
> 1
3d3
< 4

More applications of the pipe character:

$ echo  "actg aaaaa cccccg" | tr ' ' '\n' | wc -l
3


# sed =:先输出行号,再输出每行的内容
ct@ehbio:~$ echo  "a b c" | tr ' ' '\n' | sed =  
1
a
2
b
3
c


# 后面这个命令不太好解释
# sed = 同时输出行号
# N: 表示读入下一行;sed命令每次只读一行,加上N之后就是缓存了第2行,所有的操作都针对第一行;
# s: 替换;把换行符替换为\t
$ echo  "a b c" | tr ' ' '\n' | sed = | sed 'N;s/\n/\t/' 
1    a
2    b
3    c


# 后面这个命令不太好解释
# sed = 同时输出行号
# N: 表示读入下一行;sed命令每次只读一行,加上N之后就是缓存了第2行,所有的操作都针对第一行;
# s: 替换;把读取的奇数行行首加一个'>'(偶数行相当于被隐藏了)
ct@ehbio:~$ echo  "a b c" | tr ' ' '\n' | sed = | sed 'N;s/^/>/' 
>1
a
>2
b
>3
c


# 把多条序列转成FATSA格式
# sed = 同时输出行号
# N: 表示读入下一行;sed命令每次只读一行,加上N之后就是缓存了第2行,所有的操作都针对第一行;
# s: 替换;把读取的奇数行行首加一个'>'(偶数行相当于被隐藏了)
# 于是FASTA格式序列就出来了
ct@ehbio:~$ echo  "actg aaaaa cccccg" | tr ' ' '\n' | sed = | sed 'N;s/^/>/' 
>1
actg
>2
aaaaa
>3
cccccg

Guess you like

Origin blog.csdn.net/summer_fish/article/details/128414410