提交 c13929bf 编写于 作者: F feilong

增加管道

上级 41a8a686
{
"node_id": "gml-e66e052f98274a60b0eb83273cd089e7",
"keywords": [],
"keywords_must": [["linux", "管道"],["linux","pipeline"]],
"keywords_forbid": [],
"children": [],
"export": [
"exercise_01.json",
"exercise_02.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"author": "幻灰龙",
"source": "exercise_01.md",
"notebook_enable": false,
"depends": [],
"exercise_id": "10a7e5e1b1c040efb38ea71f15872ba0"
}
\ No newline at end of file
# 实用Linux命令掌握20%: 管道(pipeline)
我们学习了许多个Linux命令,但是只靠单个命令还只能发挥出Linux命令有限的能力。管道是unix 哲学的核心之一,Linux管道设计源自Unix。通过管道(pipeline)可以组合多个命令来完成一系列的操作。
**本节知识**
* 通过管道符号`"|"`,可以组合多个管道命令。
* 例如,使用`cat`命令读取全文,接着使用`grep`命令过滤出含有字符串`"hello"`的行: `cat 1.txt|grep hello`
* 管道符连接的前后两个命令,前面的命令的输出需要是标准输出,后面的命令要需要能接受标准输入
**本节任务**
1. 请在[线上Linux环境](https://edu.csdn.net/lab/36675?targetLesson=2692)里练习管道命令。
2. 以下对管道使用错误的是?
## 答案
```bash
命令 `vi` 的输出不是标准输出
从而,可以编辑并过滤文本:
`vi test.txt|grep hello`
```
## 选项
### A
```bash
`ll`命令可以显示目录下的文件信息,每个文件一行
`wc -l`命令可以统计函数
那么,统计目录下文件个数的管道命令组合是:
`ll|wc -l`
```
### B
```bash
`tail -f`命令可以监控某个日志文件的最新增加的日志
那么,可以监控日志文件里最新增加的含有"Error"的日志
`tail -f 1.log|grep Error`
```
### C
```bash
`sort` 命令可以对文本行排序
那么,可以搜索并排序结果
`grep hello *.log|sort`
```
{
"type": "code_options",
"author": "softwareteacher",
"source": "exercise_02.md",
"notebook_enable": false,
"depends": [],
"exercise_id": "bfb2721539e54164b330744a5b4cc1fa"
}
\ No newline at end of file
# 如何用 awk sed 命令统计一个 Unix/Linux 系统中文件大小的分布情况?
![](https://img-ask.csdnimg.cn/upload/1617766280958.jpg?x-oss-process=image/auto-orient,1/resize,w_320,m_lfit)
**问题**
[SoftwareTeacher](https://blog.csdn.net/SoftwareTeacher?type=blog)看了《Unix 传奇》一书,想起很久以前读 Unix 文件系统设计思想的时候,里面提到Unix 的很多文件大小在 4K 以下。他提了一个问题
“怎么能用Unix 的awk, sed 和其它 shell 命令统计出你当前 Unix/Linux 系统上 所有文件大小的分布情况? (小于1K, 1K - 4K, 4K - 1M, 1M - 10M, 10M - 1G, 1G 以上)”
**本节任务**
请在[线上Linux环境](https://edu.csdn.net/lab/36675?targetLesson=2692)里练习命令。
下面是基本实现思路:
1. 使用命令列出目录下的所有文件信息
2. 使用命令过滤出文件名和文件大小的列
3. 使用命令统计不同文件大小的个数并计算百分比打印
4. 使用管道把上述1/2/3组合起来
以下代码根据一位回答者[Brentbin](https://blog.csdn.net/Brentbin?type=ask)的实现改编,以下实现正确的是?
## 答案
```bash
ls -Rla | awk '{print $9, $5}' | awk '
BEGIN{
size[0] = " 0K-1K"
size[1] = " 1K-4K"
size[2] = " 4K-1M"
size[3] = " 1M-10M"
size[4] = "10M-1G"
size[5] = " 1G+ "
total = 0
}
($2 <= 1024) {a[0]++}
(1024 < $2 && $2 <= 4096) {a[1]++}
(4096 < $2 && $2 <= 1048576) {a[2]++}
(1048576 < $2 && $2 <= 10485760) {a[3]++}
(10485760 < $2 && $2 <= 1073741824) {a[4]++}
(1073741824 < $2 ) {a[5]++}
{total++}
END {
for(i=0;i<length(a);++i)
print size[i], "文件个数:", a[i], "百分比:", (a[i]/total)*100,"%"
}'
```
## 选项
### A
```bash
ls -Rla | awk '{print $9, $5}' | awk '
BEGIN{
size[0] = " 0K-1K"
size[1] = " 1K-4K"
size[2] = " 4K-1M"
size[3] = " 1M-10M"
size[4] = "10M-1G"
size[5] = " 1G+ "
total = 0
}
($2 <= 1024) {a[0]++}
(1024 < $2 && $2 <= 4096) {a[1]++}
(4096 < $2 && $2 <= 1048576) {a[2]++}
(1048576 < $2 && $2 <= 10485760) {a[3]++}
(10485760 < $2 && $2 <= 1073741824) {a[4]++}
(1073741824 < $2 ) {a[5]++}
END {
for(i=0;i<length(a);++i)
print size[i], "文件个数:", a[i], "百分比:", (a[i]/total)*100,"%"
}'
```
### B
```bash
ls -Rl | awk '{print $9, $5}' | awk '
BEGIN{
size[0] = " 0K-1K"
size[1] = " 1K-4K"
size[2] = " 4K-1M"
size[3] = " 1M-10M"
size[4] = "10M-1G"
size[5] = " 1G+ "
total = 0
}
($2 <= 1024) {a[0]++}
(1024 < $2 && $2 <= 4096) {a[1]++}
(4096 < $2 && $2 <= 1048576) {a[2]++}
(1048576 < $2 && $2 <= 10485760) {a[3]++}
(10485760 < $2 && $2 <= 1073741824) {a[4]++}
(1073741824 < $2 ) {a[5]++}
{total++}
END {
for(i=0;i<length(a);++i)
print size[i], "文件个数:", a[i], "百分比:", (a[i]/total)*100,"%"
}'
```
### C
```bash
ls -Rla | awk '{print $9, $5}' | awk '
BEGIN{
size[0] = " 0K-1K"
size[1] = " 1K-4K"
size[2] = " 4K-1M"
size[3] = " 1M-10M"
size[4] = "10M-1G"
size[5] = " 1G+ "
total = 0
}
($2 <= 1024) {a[0]++}
(1024 < $2 && $2 <= 4096) {a[1]++}
(4096 < $2 && $2 <= 1048576) {a[2]++}
(1048576 < $2 && $2 <= 10485760) {a[3]++}
(10485760 < $2 && $2 <= 1073741824) {a[4]++}
{total++}
END {
for(i=0;i<length(a);++i)
print size[i], "文件个数:", a[i], "百分比:", (a[i]/total)*100,"%"
}'
```
### D
```bash
ls -Rla | awk '{print $9, $4}' | awk '
BEGIN{
size[0] = " 0K-1K"
size[1] = " 1K-4K"
size[2] = " 4K-1M"
size[3] = " 1M-10M"
size[4] = "10M-1G"
size[5] = " 1G+ "
total = 0
}
($2 <= 1024) {a[0]++}
(1024 < $2 && $2 <= 4096) {a[1]++}
(4096 < $2 && $2 <= 1048576) {a[2]++}
(1048576 < $2 && $2 <= 10485760) {a[3]++}
(10485760 < $2 && $2 <= 1073741824) {a[4]++}
(1073741824 < $2 ) {a[5]++}
{total++}
END {
for(i=0;i<length(a);++i)
print size[i], "文件个数:", a[i], "百分比:", (a[i]/total)*100,"%"
}'
```
......@@ -1161,6 +1161,24 @@
],
"keywords_forbid": []
}
},
{
"管道": {
"node_id": "gml-e66e052f98274a60b0eb83273cd089e7",
"keywords": [],
"children": [],
"keywords_must": [
[
"linux",
"管道"
],
[
"linux",
"pipeline"
]
],
"keywords_forbid": []
}
}
],
"keywords_must": [
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册