1.cut
命令 |
含义 |
cut 动作 文件 |
从指定文件 截取内容 |
参数 |
英文 |
含义 |
-c |
characters |
按字符选取内容 |
head -2 1.txt | cut -c 5
参数 |
英文 |
含义 |
-d '分隔符' |
delimiter |
指定分隔符 |
-f n1,n2 |
fields |
分割以后显示第几段内容, 使用 , 分割 |
范围控制
范围 |
含义 |
n |
只显示第n项 |
n- |
显示 从第n项 一直到行尾 |
n-m |
显示 从第n项 到 第m项(包括m) |
head -2 1.txt | cut -d ':' -f 1,2
head -2 1.txt | cut -d ':' -f 1-2
2.sort
2.1 对字符串排序
[root@node01 tmp]# cat 2.txt
banana
apple
pear
orange
pear
[root@node01 tmp]# sort 2.txt
apple
banana
orange
pear
pear
2.2: 去重排序
它的作用很简单,就是在输出行中去除重复行。
[root@node01 tmp]# sort -u 2.txt
apple
banana
orange
pear
2.3: 对数值排序
参数 |
英文 |
含义 |
-n |
numeric-sort |
按照数值大小排序 |
-r |
reverse |
使次序颠倒 |
-
准备数据
[root@node01 tmp]# cat 3.txt
1
3
5
7
11
2
4
6
10
8
9
-
默认按照字符串
排序(字典序)
[root@node01 tmp]# sort 2.txt
1
10
11
2
3
4
5
6
7
8
9
-
升序
[root@node01 tmp]# sort -n 2.txt
1
2
3
4
5
6
7
8
9
10
11
-
倒序
[root@node01 tmp]# sort -n -r 2.txt
11
10
9
8
7
6
5
4
3
2
1
-
合并式
[root@node01 tmp]# sort -nr 2.txt
11
10
9
8
7
6
5
4
3
2
1
2.4: 对成绩排序
参数 |
英文 |
含义 |
-t |
field-separator |
指定字段分隔符 |
-k |
key |
根据那一列排序 |
# 根据第二段成绩 进行倒序显示 所有内容
[root@node01 tmp]# sort -t ',' -k2nr score.txt
lisi 98 66 96
zhouba 98 44 46
maq 88 22 66
zhaoliu 78 44 36
zhangsan 68 99 26
wangwu 38 33 86
3.wc
3.1 显示指定行数,单词数, 字节数, 文件信息.
命令 |
含义 |
wc 文件名 |
显示指定行数,单词数, 字节数, 文件 信息 |
[root@hadoop01 export]# cat 4.txt
111
222 bbb
333 aaa bbb
444 aaa bbb ccc
555 aaa bbb ccc ddd
666 aaa bbb ccc ddd eee
[root@hadoop01 export]# wc 4.txt
6 21 85 4.txt
3.2: 只显示 文件 的行数
参数 |
英文 |
含义 |
-c |
bytes |
字节数 |
-w |
words |
单词数 |
-l |
lines |
行数 |
[root@node01 tmp]# wc -c 4.txt
85 4.txt
[root@node01 tmp]# wc -w 4.txt
21 4.txt
[root@node01 tmp]# wc -l 4.txt
6 4.txt
3.3: 统计多个文件的 行数 单词数 字节数
[root@hadoop01 export]# wc 1.txt 2.txt 3.txt
4 4 52 1.txt
11 11 24 2.txt
6 21 85 3.txt
21 36 161 总用量
[root@hadoop01 export]# wc *.txt
4 4 52 1.txt
11 11 24 2.txt
6 21 85 3.txt
6 6 95 score.txt
27 42 256 总用量
3.4: 查看 /etc 目录下 有多少个 子内容
[root@hadoop01 export]# ls /etc | wc -w
240
4.uniq
4.1:实现去重效果
命令 |
英文 |
含义 |
uniq [参数] 文件 |
unique 唯一 |
去除重复行 |
# 准备内容
[root@hadoop01 export]# cat 5.txt
张三 98
李四 100
王五 90
赵六 95
麻七 70
李四 100
王五 90
赵六 95
麻七 70
# 排序
[root@hadoop01 export]# cat 5.txt | sort
李四 100
李四 100
麻七 70
麻七 70
王五 90
王五 90
张三 98
赵六 95
赵六 95
# 去重
[root@hadoop01 export]# cat 5.txt | sort | uniq
李四 100
麻七 70
王五 90
张三 98
赵六 95
4.2:不但去重,还要 统计出现的次数
参数 |
英文 |
含义 |
-c |
count |
统计每行内容出现的次数 |
[root@hadoop01 export]# cat 5.txt | sort | uniq -c
2 李四 100
2 麻七 70
2 王五 90
1 张三 98
2 赵六 95
5.tee
- 通过
tee
可以将命令结果 通过管道 输出到 多个文件中
命令 |
含义 |
命令结果 | tee 文件1 文件2 文件3 |
通过 tee 可以将命令结果 通过管道 输出到 多个文件中 |
6.tr
6.1: 实现 替换效果
命令 |
英文 |
含义 |
命令结果 | tr 被替换的字符 新字符 |
translate |
实现 替换效果 |
# 将 小写i 替换成 大写 I
# 把itheima的转换为大写
# 把 HELLO 转成 小写
# 将 小写i 替换成 大写 I
echo "itheima" | tr 'i' 'I'
# 把itheima的转换为大写
echo "itheima" |tr '[a-z]' '[A-Z]'
# 把 HELLO 转成 小写
echo "HELLO" |tr '[A-Z]' '[a-z]'
6.2: 实现删除效果
命令 |
英文 |
含义 |
命令结果 | tr -d 被删除的字符 |
delete |
删除指定的字符 |
echo 'abc1d4e5f' | tr -d '[0-9]'
6.3: 单词计数
准备工作
[root@hadoop01 export]# cat words.txt
hello,world,hadoop
hive,sqoop,flume,hello
kitty,tom,jerry,world
hadoop
步骤:
1.将, 换成换行
2.排序
3.去重
4.计数
# 统计每个单词出现的次数
[root@hadoop01 export]# cat words.txt | tr ',' '\n' | sort | uniq -c
1 flume
2 hadoop
2 hello
1 hive
1 jerry
1 kitty
1 sqoop
1 tom
2 world
7.split
- 通过
split
命令将大文件 切分成 若干小文件
7.1: 按 字节将 大文件 切分成 若干小文件
命令 |
英文 |
含义 |
split -b 10k 文件 |
byte |
将大文件切分成若干10KB的小文件 |
7.2: 按行数将 大文件 切分成 若干小文件
命令 |
英文 |
含义 |
split -l 1000 文件 |
lines |
将大文件切分成若干1000行 的小文件 |
- 通过
split 选项 文件名
命令将大文件 切分成 若干小文件
- 下图中以x开头的文件极为切出来的文件
注意
:如果在同一个文件夹下切割,下一次切割会将上一次切割后的文件覆盖掉
8.awk
vim score.txt
zhangsan 68 99 26
lisi 98 66 96
wangwu 38 33 86
zhaoliu 78 44 36
maq 88 22 66
zhouba 98 44 46
8.1: 模糊查询
搜索 zhangsan 和 lisi 的成绩
命令 |
含义 |
awk ‘/zhangsan|lisi/’ score.txt |
模糊查询 |
[root@node01 tmp]# awk '/zhangsan|lisi/' score.txt
zhangsan 68 99 26
lisi 98 66 96
8.2: 指定分割符, 根据下标显示内容
命令 |
含义 |
awk -F ’ ’ ‘{print $1, $2, $3}’ score.txt |
操作score.txt文件, 根据 逗号 分割, 打印 第一段 第二段 第三段 内容 |
[root@node01 tmp]# awk -F ' ' '{print $1, $2, $3}' score.txt
zhangsan 68 99
lisi 98 66
wangwu 38 33
zhaoliu 78 44
maq 88 22
zhouba 98 44
选项
选项 |
英文 |
含义 |
-F ',' |
field-separator |
使用 指定字符 分割 |
$ + 数字 |
|
获取第几段内容 |
$0 |
|
获取 当前行 内容 |
NF |
field |
表示当前行共有多少个字段 |
$NF |
|
代表 最后一个字段 |
$(NF-1) |
|
代表 倒数第二个字段 |
NR |
|
代表 处理的是第几行 |
8.3: 指定分割符, 根据下标显示内容
命令 |
含义 |
awk -F ’ ’ ‘{OFS="==="}{print $1, $2, $3}’ score.txt |
操作score.txt文件, 根据 逗号 分割, 打印 第一段 第二段 第三段 内容 |
[root@node01 tmp]# awk -F ' ' '{OFS="==="}{print $1, $2, $3}' score.txt
zhangsan===68===99
lisi===98===66
wangwu===38===33
zhaoliu===78===44
maq===88===22
zhouba===98===44
选项
选项 |
英文 |
含义 |
OFS="字符" |
output field separator |
向外输出时的段分割字符串 |
8.4: 调用 awk 提供的函数
命令 |
含义 |
awk -F ’ ’ ‘{print toupper($1)}’ score.txt |
操作score.txt文件, 根据 逗号 分割, 打印 第一段 第二段 第三段 内容 |
[root@node01 tmp]# awk -F ' ' '{print toupper($1)}' score.txt
ZHANGSAN
LISI
WANGWU
ZHAOLIU
MAQ
ZHOUBA
常用函数如下:
函数名 |
含义 |
作用 |
toupper() |
upper |
字符 转成 大写 |
tolower() |
lower |
字符 转成小写 |
length() |
length |
返回 字符长度 |
8.5: if语句 查询及格的学生信息
命令 |
含义 |
awk -F ’ ’ ‘{if($4>60) print $1, $4 }’ score.txt |
如果及格,就显示 $1, $4 |
awk -F ’ ’ ‘{if($4>60) print $1, $4, “及格”; else print $1, $4, “不及格”}’ score.txt |
显示 姓名, $4, 是否及格 |
[root@node01 tmp]# awk -F ' ' '{if($4>60) print $1, $4 }' score.txt
lisi 96
wangwu 86
maq 66
[root@node01 tmp]# awk -F ' ' '{if($4>60) print $1, $4, "及格"; else print $1, $4, "不及格"}' score.txt
zhangsan 26 不及格
lisi 96 及格
wangwu 86 及格
zhaoliu 36 不及格
maq 66 及格
zhouba 46 不及格
选项
参数 |
含义 |
if($0 ~ “aa”) print $0 |
如果这一行包含 “aa”, 就打印这一行内容 |
if($1 ~ “aa”) print $0 |
如果**第一段 **包含 “aa”, 就打印这一行内容 |
if($1 == “lisi”) print $0 |
如果第一段 等于 “lisi”, 就打印这一行内容 |
8.6: 段内容 求学科平均分
命令 |
含义 |
awk ‘BEGIN{初始化操作}{每行都执行} END{结束时操作}’ 文件名 |
BEGIN{ 这里面放的是执行前的语句 } {这里面放的是处理每一行时要执行的语句} END {这里面放的是处理完所有的行后要执行的语句 } |
awk -F ' ' 'BEGIN{}{total=total+$4}END{print total, NR, (total/NR)}' score.txt
[root@node01 tmp]# awk -F ' ' 'BEGIN{}{total=total+$4}END{print total, NR, (total/NR)}' score.txt
356 6 59.3333
9.sed
9.1:查询 功能
命令 |
含义 |
sed 可选项 目标文件 |
对目标文件 进行 过滤查询 或 替换 |
可选参数
可选项 |
英文 |
含义 |
p |
print |
打印 |
$ |
|
代表 最后一行 |
-n |
|
仅显示处理后的结果 |
-e |
expression |
根据表达式 进行处理 |
sed -n -e '1,5p' 1.txt
[root@node01 tmp]# sed -n -e '1,5p' 1.txt
aaa java root
bbb hello
ccc rt
ddd root nologin
eee rtt
sed -n -e '1,$p' 1.txt
[root@node01 tmp]# sed -n -e '1,$p' 1.txt
aaa java root
bbb hello
ccc rt
ddd root nologin
eee rtt
fff ROOT nologin
ggg rttt
-
9.1.3列出 1.txt的所有数据 且 显示行号
sed -n -e '1,$=' -e '1,$p' 1.txt
[root@node01 tmp]# sed -n -e '1,$=' -e '1,$p' 1.txt
1
aaa java root
2
bbb hello
3
ccc rt
4
ddd root nologin
5
eee rtt
6
fff ROOT nologin
7
ggg rttt
简化版
cat -n 1.txt
cat -b 1.txt
nl 1.txt
[root@node01 tmp]# cat -n 1.txt
1 aaa java root
2 bbb hello
3 ccc rt
4 ddd root nologin
5 eee rtt
6 fff ROOT nologin
7 ggg rttt
[root@node01 tmp]# cat -b 1.txt
1 aaa java root
2 bbb hello
3 ccc rt
4 ddd root nologin
5 eee rtt
6 fff ROOT nologin
7 ggg rttt
[root@node01 tmp]# nl 1.txt
1 aaa java root
2 bbb hello
3 ccc rt
4 ddd root nologin
5 eee rtt
6 fff ROOT nologin
7 ggg rttt
sed -n -e '/root/p' 1.txt
[root@node01 tmp]# sed -n -e '/root/p' 1.txt
aaa java root
ddd root nologin
-
9.1.5 列出 1.txt中包含root的内容,root不区分大小写,并显示行号
nl 1.txt | sed -n -e '/root/Ip'
nl 1.txt | grep -i root
cat -n 1.txt | grep -i root
[root@node01 tmp]# nl 1.txt | sed -n -e '/root/Ip'
1 aaa java root
4 ddd root nologin
6 fff ROOT nologin
[root@node01 tmp]# nl 1.txt | grep -i root
1 aaa java root
4 ddd root nologin
6 fff ROOT nologin
[root@node01 tmp]# cat -n 1.txt | grep -i root
1 aaa java root
4 ddd root nologin
6 fff ROOT nologin
-
9.1.6 查找出 1.txt中 字母r
后面是多个t的行,并显示行号
可选项 |
英文 |
含义 |
-r |
regexp-extended |
识别正则 |
nl 1.txt | sed -nr -e '/r+t/p'
[root@node01 tmp]# nl 1.txt | sed -nr -e '/r+t/p'
3 ccc rt
5 eee rtt
7 ggg rttt
或者
sed -nr -e '/r+t/p' -e '/r+t/=' 1.txt
[root@node01 tmp]# sed -nr -e '/r+t/p' -e '/r+t/=' 1.txt
ccc rt
3
eee rtt
5
ggg rttt
7
9.2 删除 功能
-
9.2.1 删除1.txt中前3行数据,并显示行号
可选项 |
英文 |
含义 |
d |
delete |
删除指定内容 |
nl 1.txt | sed -e '1,3d'
[root@node01 tmp]# nl 1.txt | sed -e '1,3d'
4 ddd root nologin
5 eee rtt
6 fff ROOT nologin
7 ggg rttt
-
9.2.2 保留1.txt中前4行数据,并显示行号
nl 1.txt | sed -e '5,$d'
nl 1.txt | sed -n -e '1,4p'
[root@node01 tmp]# nl 1.txt | sed -e '5,$d'
1 aaa java root
2 bbb hello
3 ccc rt
4 ddd root nologin
[root@node01 tmp]# nl 1.txt | sed -n -e '1,4p'
1 aaa java root
2 bbb hello
3 ccc rt
4 ddd root nologin
9.3修改 功能
-
9.3.1 在 1.txt的第二行后添加aaaaa,并显示行号
参数 |
英文 |
含义 |
i |
insert |
目标前面 插入内容 |
a |
append |
目标后面 追加内容 |
nl 1.txt | sed -e '2a aaaaa'
[root@node01 tmp]# nl 1.txt | sed -e '2a aaaaa'
1 aaa java root
2 bbb hello
aaaaa
3 ccc rt
4 ddd root nologin
5 eee rtt
6 fff ROOT nologin
7 ggg rttt
-
9.3.2 在1.txt的第1行前添加bbbbb,并显示行号
nl 1.txt | sed -e '1i bbbbb'
[root@node01 tmp]# nl 1.txt | sed -e '1i bbbbb'
bbbbb
1 aaa java root
2 bbb hello
3 ccc rt
4 ddd root nologin
5 eee rtt
6 fff ROOT nologin
7 ggg rttt
9.4 替换 功能
-
9.4.1 把 1.txt中的nologin替换成为huawei,并显示行号
|
英文 |
含义 |
s/oldString/newString/ |
replace |
替换 |
nl 1.txt | sed -e 's/nologin/huawei/'
[root@node01 tmp]# nl 1.txt | sed -e 's/nologin/huawei/'
1 aaa java root
2 bbb hello
3 ccc rt
4 ddd root huawei
5 eee rtt
6 fff ROOT huawei
7 ggg rttt
-
9.4.2 把1.txt中的1,2行替换为aaa,并显示行号
选项 |
英文 |
|
2c 新字符串 |
replace |
使用新字符串 替换 选中的行 |
nl 1.txt | sed -e '1,2c aaa'
[root@node01 tmp]# nl 1.txt | sed -e '1,2c aaa'
aaa
3 ccc rt
4 ddd root nologin
5 eee rtt
6 fff ROOT nologin
7 ggg rttt
9.5 对 原文件 进行操作
-
9.5.1 在 1.txt中把nologin替换为 huawei
参数 |
英文 |
含义 |
-i |
in-place |
替换原有文件内容 |
sed -i -e 's/nologin/huawei/' 1.txt
[root@node01 tmp]# sed -i -e 's/nologin/huawei/' 1.txt
[root@node01 tmp]# cat 1.txt
aaa java root
bbb hello
ccc rt
ddd root huawei
eee rtt
fff ROOT huawei
ggg rttt
-
9.5.2 在1.txt文件中第2、3行替换为aaaaaa
sed -i -e '2,3c aaa' 1.txt
[root@node01 tmp]# sed -i -e '2,3c aaa' 1.txt
[root@node01 tmp]# cat 1.txt
aaa java root
aaa
ddd root huawei
eee rtt
fff ROOT huawei
ggg rttt
注意
:在进行操作之前,最好是对数据进行备份,放置操作失误,数据无法恢复!
-
9.5.3 删除1.txt中前2行数据,并且删除原文件中的数据
sed -i -e '1,2d' 1.txt
[root@node01 tmp]# sed -i -e '1,2d' 1.txt
[root@node01 tmp]# cat 1.txt
ddd root huawei
eee rtt
fff ROOT huawei
ggg rttt
nl 1.txt 查看数据
[root@node01 tmp]# nl 1.txt
1 ddd root huawei
2 eee rtt
3 fff ROOT huawei
4 ggg rttt
9.6: 综合运用
ifconfig eth0 | grep "inet addr" | sed -e 's/^.*inet addr://' | sed -e 's/Bcast:.*$//'
[root@node01 tmp]# ifconfig eth0 | grep "inet addr" | sed -e 's/^.*inet addr://' | sed -e 's/Bcast:.*$//'
192.168.10.128
-
9.6.2 从1.txt中提出数据,匹配出包含root的内容,再把nologin替换为itheima
nl 1.txt | grep 'root' | sed -e 's/nologin/itheima/'
[root@node01 tmp]# nl 1.txt | grep 'root' | sed -e 's/nologin/itheima/'
1 aaa java root
4 ddd root itheima
或者
nl 1.txt | sed -n -e '/root/p' | sed -e 's/nologin/itheima/'
[root@node01 tmp]# nl 1.txt | sed -n -e '/root/p' | sed -e 's/nologin/itheima/'
1 aaa java root
4 ddd root itheima
或者
nl 1.txt | sed -n -e '/root/{s/nologin/itheima/p}' #只显示替换内容的行
[root@node01 tmp]# nl 1.txt | sed -n -e '/root/{s/nologin/itheima/p}'
4 ddd root itheima
-
9.6.3 从1.txt中提出数据,删除前2行,并把nologin替换为itheima,并显示行号
nl 1.txt | sed -e '1,2d' | sed -e 's/nologin/itheima/'
[root@node01 tmp]# nl 1.txt | sed -e '1,2d' | sed -e 's/nologin/itheima/'
3 ccc rt
4 ddd root itheima
5 eee rtt
6 fff ROOT itheima
7 ggg rttt