4.1 wc paste grep tar 命令

115 阅读2分钟

1.wc命令 统计

wc -l   #行数

wc -w #单词数 (tab 空格 算单词的分隔符)

wc -c #字符数

2.三种显示形式

标注输入:用键盘输入的信息。

标准输出:电脑反馈的正确信息。

错误输出:电脑反馈给你的非正确信息。

cmd1|cmd2

左边要有标准输出

右边要支持标准输入

3.通配符与正则表达式

通配符:【a-z】 除了Z其他字母都有

正则表达式:【a-z】 小写字母

^a 表示匹配以a为开头的内容

a$ 表示匹配以a为结尾的内容

^a$ 表示匹配字母a的内容

^$ 表示空行

4.paste命令 水平合并

[root@localhost c]# touch a b   /创建文件 a b
[root@localhost c]# vim a   
[root@localhost c]# vim b
[root@localhost c]# cat a
a
b
c
[root@localhost c]# cat b
1
2
3

[root@localhost c]# paste a b >c     /水平合并a b,并传输结果到文件c
[root@localhost c]# cat c
a	1
b	2
c	3


4.grep命令 过滤文本中你感兴趣的内容

grep 选项 匹配式(正则表达式) 文件或标准输入 注意:匹配式在前 文件或标准输入在后!

grep -i #忽略大小写

grep -n#显示行号
[root@localhost mnt]# cat a/a3       查看a/a3下的内容
abc
ABC
ASD
asd
123
[root@localhost mnt]# grep -in ^abc$ a/a3     在a/a3中不区分大小写显示行号匹配abc
1:abc
2:ABC


grep -vin#取反
[root@localhost mnt]# grep -vn ^abc$ a/a3     在a/a3中显示行号取反匹配abc
2:ABC
3:ASD
4:asd
5:123


grep -r#递归匹配
[root@localhost mnt]# tree a
a
├── a1
├── a2
└── a3

0 directories, 3 files
[root@localhost mnt]# cat a/a1
123
qwe
asd
zxc
[root@localhost mnt]# cat a/a3
abc
ABC
ASD
asd
123
[root@localhost mnt]# grep -r ^123 a         在a文件夹中中递归匹配123
a/a1:123
a/a3:123


grep -w#单词匹配


grep -f#以文件作为匹配的条件 
[root@localhost a]# cat a1
123
qwe
asd
zxc
[root@localhost a]# cat a3
abc
ABC
ASD
asd
123
[root@localhost a]# grep -f a1 a3       匹配a1和a3文件中相同的部分
asd
123

5.tar命令 归档(解压缩)

tar 选项 自定义名称 要压缩的文件

tar 选项含义
-c建立归档文件
-f使用归档(使用tar命令必须加)
-x解压缩
-C指定解压释放的文件夹
-j调用bzip2压缩
-z调用gzip压缩
-v显示过程
-t不解压查看内容
[root@localhost mnt]# tree 
.
├── a
│   ├── a1
│   ├── a2
│   └── a3
├── b
└── c

3 directories, 3 files
[root@localhost mnt]# tar -zcvf test.gz a    /对文件a进行归档gzip压缩取名位test.gz,并显示过程
a/
a/a1
a/a2
a/a3
[root@localhost mnt]# ls                   
a  b  c  test.gz
[root@localhost mnt]# tar -jcvf test.jz b          /对文件b进行归档gzip压缩取名位test.gz,并显示过程
b/
[root@localhost mnt]# ls
a  b  c  test.gz  test.jz
[root@localhost mnt]# mkdir   ysb   /建立文件夹ysb
[root@localhost mnt]# ls
a  b  c  test.gz  test.jz  ysb
[root@localhost mnt]# tar -xf test.gz -C ysb    /将压缩包test.gz指定解压到文件夹ysb中
[root@localhost ysb]# tree
.
└── a
    ├── a1
    ├── a2
    └── a3

1 directory, 3 files
[root@localhost ysb]#