0
点赞
收藏
分享

微信扫一扫

学习笔记-第07天-命令合集6

沈芏 2023-05-25 阅读 57

重定向

       1. 标准输出重定向,用数字1表示

       2. 标准错误输出重定向,用数字2表示

       3. 标准输入重定向,用数字0来表示。

1)标准[输出]重定向:

>或1>,数据流向就是朝右.

[root@localhost ~]# echo oldboy
oldboy
[root@localhost ~]# echo oldboy >oldboy.txt
[root@localhost ~]# cat oldboy.txt
oldboy
[root@localhost ~]# echo oldboy 1>oldboy.txt
[root@localhost ~]# cat oldboy.txt
oldboy

学习笔记-第07天-命令合集6_输出重定向

1.输出oldboy。

2.清空oldboy.txt

3.把oldboy写入到oldboy.txt

常用清空文件命令:

[root@localhost ~]# >oldboy.txt
[root@localhost ~]# cat oldboy.txt
[root@localhost ~]# cat /dev/null>oldboy.txt

学习笔记-第07天-命令合集6_bash_02

2) 标准[错误输出]重定向:

2>接收执行命令的报错

       作用:接收执行命令的报错。

[root@localhost ~]# cat oldboy.txt
[root@localhost ~]# ls >oldboy.txt
[root@localhost ~]# cat oldboy.txt
oldboy.txt
[root@localhost ~]# ls
oldboy.txt
[root@localhost ~]# ls >oldboy.txt
[root@localhost ~]# ls 2>oldboy.txt
oldboy.txt
[root@localhost ~]# cat oldboy.txt
[root@localhost ~]# 2>oldboy.txt
[root@localhost ~]# cat oldboy.txt
[root@localhost ~]# lss
-bash: lss: command not found
[root@localhost ~]# cat oldboy.txt
[root@localhost ~]# lss 2>oldboy.txt
[root@localhost ~]# cat oldboy.txt
-bash: lss: command not found
[root@localhost ~]# ls >oldboy.txt 2>oldgirl.txt
[root@localhost ~]# cat oldboy.txt
oldboy.txt
oldgirl.txt
[root@localhost ~]# cat oldgirl.txt
[root@localhost ~]# lss >oldboy.txt 2>oldgirl.txt
[root@localhost ~]# >oldboy.txt
[root@localhost ~]# cat oldboy.txt
[root@localhost ~]# cat oldgirl.txt
-bash: lss: command not found

学习笔记-第07天-命令合集6_重定向_03

3) 标准[输入]重定向,0来表示:

     0<或< 数据流向朝左。

     tr 按字符替换:

[root@localhost ~]# cat oldboy.txt
oldboy
oldboy.txt
oldgirl.txt
[root@localhost ~]# tr "old" "abc"<oldboy.txt
abcbay
abcbay.txt
abcgirb.txt
[root@localhost ~]# tr "a-g" "1-7"<oldboy.txt
ol42oy
ol42oy.txt
ol47irl.txt

学习笔记-第07天-命令合集6_重定向_04

Xargs (从标准输入接收数据)

-n  分组:

学习笔记-第07天-命令合集6_bash_05

4) 标准【追加】输出重定向:

    >>或1>>,数据流向就是朝右,标准【追加】输出重定向:

[root@localhost ~]# echo oldboy >>oldboy.txt
[root@localhost ~]# cat oldboy.txt
oldboy
oldboy.txt
oldgirl.txt
Oldboy

学习笔记-第07天-命令合集6_输出重定向_06

解释:

1. 输出oldboy;

2. 把oldboy追加到oldboy.txt结尾。

5) 标准错误【追加】输出重定向:

     >>或2>>,数据流向就是超右,标准【追加】输出重定向:

[root@localhost ~]# lss
-bash: lss: command not found
[root@localhost ~]# lss 2>a.txt
[root@localhost ~]# cat a.txt
-bash: lss: command not found
[root@localhost ~]# dfff 2>a.txt
[root@localhost ~]# cat a.txt
-bash: dfff: command not found
[root@localhost ~]# lss 2>>a.txt
[root@localhost ~]# cat a.txt
-bash: dfff: command not found
-bash: lss: command not found
[root@localhost ~]# dfff 2>>a.txt
[root@localhost ~]# cat a.txt
-bash: dfff: command not found
-bash: lss: command not found
-bash: dfff: command not found

学习笔记-第07天-命令合集6_输出重定向_07

6) 标准输入【追加】重定向,0来表示

    0<<或<< 数据流向朝左

(大段内容非交互式编辑)

[root@localhost ~]# cat  <<eof
>      ddd
> fff
> eof
     ddd
fff
[root@localhost ~]# cat  >a.txt  <<eof
> ddd
> fff
> eof
[root@localhost ~]# cat a.txt
ddd
fff

学习笔记-第07天-命令合集6_bash_08

特殊情况:希望用一个文件同时接受手机错误和正确的信息。

方法一:

echo oldboy >>oldboy.txt 2>>oldboy.txt

[root@localhost ~]# echo oldboy >>oldboy.txt 2>>oldboy.txt
[root@localhost ~]# cat oldboy.txt
oldboy
oldboy.txt
oldgirl.txt
oldboy
oldboy
[root@localhost ~]# cho oldboy >>oldboy.txt 2>>oldboy.txt
[root@localhost ~]# cat oldboy.txt
oldboy
oldboy.txt
oldgirl.txt
oldboy
oldboy
-bash: cho: command not found

学习笔记-第07天-命令合集6_重定向_09

方法二:

        cho oldboy &>>oldboy.txt

[root@localhost ~]# cho oldboy &>>oldboy.txt
[root@localhost ~]# cat oldboy.txt
oldboy
oldboy.txt
oldgirl.txt
oldboy
oldboy
-bash: cho: command not found
-bash: cho: command not found

学习笔记-第07天-命令合集6_bash_10

方法三:

      cho oldboy 1>>oldboy.txt 2>&1

[root@localhost ~]# cho oldboy 1>>oldboy.txt 2>&1
[root@localhost ~]# cat oldboy.txt
oldboy
oldboy.txt
oldgirl.txt
oldboy
oldboy
-bash: cho: command not found
-bash: cho: command not found
-bash: cho: command not found

学习笔记-第07天-命令合集6_输出重定向_11

别名永久生效:

全局环境变量文件:

/etc/profile

/etc/bashrc

用户环境变量文件:

~/.bash_profile

~/.bashrc

[root@localhost ~]# vim /etc/profile

按键盘大写GG到命令行最后一行,按o进行编辑自己所写的变量内容,按键盘esc,wq!进行保存强制退出。

[root@localhost ~]# a=2
[root@localhost ~]# echo $a
2

uniq  去重:

-c 计数

[root@localhost ~]# cat  >test.txt  <<EOF
10.0.0.1
10.0.0.11
10.0.0.1
10.0.0.1
10.0.0.2
10.0.0.8
10.0.0.8
10.0.0.16
EOF
[root@localhost ~]# uniq test.txt  #相邻的相同行去重
10.0.0.1
10.0.0.11
10.0.0.1
10.0.0.2
10.0.0.8
10.0.0.16

学习笔记-第07天-命令合集6_输出重定向_12

-c 计数:

[root@localhost ~]# uniq -c test.txt  #相邻的相同行去重并且计数。
      1 10.0.0.1
      1 10.0.0.11
      2 10.0.0.1
      1 10.0.0.2
      2 10.0.0.8
      1 10.0.0.16

学习笔记-第07天-命令合集6_重定向_13

sort 排序:

[root@localhost ~]# sort test.txt
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.11
10.0.0.16
10.0.0.18
10.0.0.2
10.0.0.8
10.0.0.8
10.0.0.8
10.0.0.8
10.0.0.8
[root@localhost ~]# sort test.txt >u.txt   #按数字顺序排序
[root@localhost ~]# uniq -c u.txt        #整个文件实现了去重并且计数
      6 10.0.0.1
      1 10.0.0.11
      1 10.0.0.16
      1 10.0.0.18
      1 10.0.0.2
      5 10.0.0.8

学习笔记-第07天-命令合集6_bash_14

-n  按数字排序

-t  指定分隔符

-k  指定列

[root@localhost ~]# sort -n test.txt
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.11
10.0.0.16
10.0.0.18
10.0.0.2
10.0.0.8
10.0.0.8
10.0.0.8
10.0.0.8
10.0.0.8
[root@localhost ~]# sort -n -t. -k4 test.txt
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.2
10.0.0.8
10.0.0.8
10.0.0.8
10.0.0.8
10.0.0.8
10.0.0.11
10.0.0.16
10.0.0.18

学习笔记-第07天-命令合集6_bash_15

-r  指倒序排序

[root@localhost ~]# sort -rn -t. -k4 test.txt
10.0.0.18
10.0.0.16
10.0.0.11
10.0.0.8
10.0.0.8
10.0.0.8
10.0.0.8
10.0.0.8
10.0.0.2
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
[root@localhost ~]# sort -rn -t. -k4 test.txt >ab.txt
[root@localhost ~]# uniq -c ab.txt
      1 10.0.0.18
      1 10.0.0.16
      1 10.0.0.11
      5 10.0.0.8
      1 10.0.0.2
      6 10.0.0.1
[root@localhost ~]# uniq -c ab.txt >b.txt
[root@localhost ~]# sort -rn b.txt
      6 10.0.0.1
      5 10.0.0.8
      1 10.0.0.2
      1 10.0.0.18
      1 10.0.0.16
      1 10.0.0.11

学习笔记-第07天-命令合集6_重定向_16

|管道:

[root@localhost ~]# cat oldboy.txt |grep oldboy
oldboy
oldboy.txt
oldboy
oldboy

学习笔记-第07天-命令合集6_重定向_17

用管道处理上序IP排序问题:

[root@localhost ~]# sort test.txt|uniq -c|sort -rn
      6 10.0.0.1
      5 10.0.0.8
      1 10.0.0.2
      1 10.0.0.18
      1 10.0.0.16
      1 10.0.0.11
[root@localhost ~]# sort test.txt|uniq -c|sort -rn -t. -k4
      1 10.0.0.18
      1 10.0.0.16
      1 10.0.0.11
      5 10.0.0.8
      1 10.0.0.2
      6 10.0.0.1

学习笔记-第07天-命令合集6_重定向_18





举报

相关推荐

0 条评论