Liunx小工具(相关命令
1.cut 2.sort2.1 对字符串排序2.2: 去重排序2.3: 对数值排序2.4: 对成绩排序 3.wc3.1 显示指定行数,单词数, 字节数, 文件信息.3.2: 只显示 文件 的行数3.3: 统计多个文件的 行数 单词数 字节数3.4: 查看 /etc 目录下 有多少个 子内容 4.uniq4.1:实现去重效果4.2:不但去重,还要 统计出现的次数 5.tee将去重统计的结果 放到 a.txt、b.txt、c.txt 文件中 6.tr6.1: 实现 替换效果6.2: 实现删除效果6.3: 单词计数准备工作 7.split7.1: 按 字节将 大文件 切分成 若干小文件7.2: 按行数将 大文件 切分成 若干小文件 8.awk8.1: 模糊查询8.2: 指定分割符, 根据下标显示内容8.3: 指定分割符, 根据下标显示内容8.4: 调用 awk 提供的函数8.5: if语句 查询及格的学生信息8.6: 段内容 求学科平均分 9.sed9.1:查询 功能9.1.1 列出 1.txt的 1~5行 的数据9.1.2 列出 1.txt的所有数据9.1.3列出 1.txt的所有数据 且 显示行号9.1.4查找 1.txt中包含root行9.1.5 列出 1.txt中包含root的内容,root不区分大小写,并显示行号9.1.6 查找出 1.txt中 字母`r`后面是多个t的行,并显示行号 9.2 删除 功能9.2.1 删除1.txt中前3行数据,并显示行号9.2.2 保留1.txt中前4行数据,并显示行号 9.3修改 功能9.3.1 在 1.txt的第二行后添加aaaaa,并显示行号9.3.2 在1.txt的第1行前添加bbbbb,并显示行号 9.4 替换 功能9.4.1 把 1.txt中的nologin替换成为huawei,并显示行号9.4.2 把1.txt中的1,2行替换为aaa,并显示行号 9.5 对 原文件 进行操作9.5.1 在 1.txt中把nologin替换为 huawei9.5.2 在1.txt文件中第2、3行替换为aaaaaa9.5.3 删除1.txt中前2行数据,并且删除原文件中的数据 9.6: 综合运用9.6.1 获取ip地址9.6.2 从1.txt中提出数据,匹配出包含root的内容,再把nologin替换为itheima9.6.3 从1.txt中提出数据,删除前2行,并把nologin替换为itheima,并显示行号1.cut
参数head -2 1.txt | cut -c 5
范围控制
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.txtbananaapplepearorangepear[root@node01 tmp]# sort 2.txt applebananaorangepearpear
2.2: 去重排序
它的作用很简单,就是在输出行中去除重复行。
[root@node01 tmp]# sort -u 2.txt applebananaorangepear
2.3: 对数值排序
准备数据
[root@node01 tmp]# cat 3.txt 1357112461089
默认按照字符串
排序(字典序)
[root@node01 tmp]# sort 2.txt 1101123456789
升序
[root@node01 tmp]# sort -n 2.txt1234567891011
倒序
[root@node01 tmp]# sort -n -r 2.txt1110987654321
合并式
[root@node01 tmp]# sort -nr 2.txt 1110987654321
2.4: 对成绩排序
准备工作
vim score.txt
zhangsan 68 99 26lisi 98 66 96wangwu 38 33 86zhaoliu 78 44 36maq 88 22 66zhouba 98 44 46
# 根据第二段成绩 进行倒序显示 所有内容[root@node01 tmp]# sort -t ',' -k2nr score.txt lisi 98 66 96zhouba 98 44 46maq 88 22 66zhaoliu 78 44 36zhangsan 68 99 26wangwu 38 33 86
3.wc
3.1 显示指定行数,单词数, 字节数, 文件信息.
[root@hadoop01 export]# cat 4.txt111222 bbb333 aaa bbb 444 aaa bbb ccc555 aaa bbb ccc ddd666 aaa bbb ccc ddd eee[root@hadoop01 export]# wc 4.txt 6 21 85 4.txt
3.2: 只显示 文件 的行数
[root@node01 tmp]# wc -c 4.txt85 4.txt[root@node01 tmp]# wc -w 4.txt21 4.txt[root@node01 tmp]# wc -l 4.txt6 4.txt
3.3: 统计多个文件的 行数 单词数 字节数
[root@hadoop01 export]# wc 1.txt 2.txt 3.txt 4 4 52 1.txt11 11 24 2.txt6 21 85 3.txt21 36 161 总用量[root@hadoop01 export]# wc *.txt4 4 52 1.txt11 11 24 2.txt6 21 85 3.txt6 6 95 score.txt27 42 256 总用量
3.4: 查看 /etc 目录下 有多少个 子内容
[root@hadoop01 export]# ls /etc | wc -w240
4.uniq
4.1:实现去重效果
# 准备内容[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:不但去重,还要 统计出现的次数
[root@hadoop01 export]# cat 5.txt | sort | uniq -c2 李四 1002 麻七 702 王五 901 张三 982 赵六 95
5.tee
通过tee
可以将命令结果通过管道输出到多个文件中将去重统计的结果 放到 a.txt、b.txt、c.txt 文件中
cat 5.txt | sort | uniq -c | tee a.txt b.txt c.txt
6.tr
6.1: 实现 替换效果
# 将 小写i 替换成 大写 I# 把itheima的转换为大写# 把 HELLO 转成 小写
# 将 小写i 替换成 大写 Iecho "itheima" | tr 'i' 'I'# 把itheima的转换为大写echo "itheima" |tr '[a-z]' '[A-Z]'# 把 HELLO 转成 小写echo "HELLO" |tr '[A-Z]' '[a-z]'
6.2: 实现删除效果
需求: 删除abc1d4e5f中的数字echo 'abc1d4e5f' | tr -d '[0-9]'
6.3: 单词计数
准备工作
[root@hadoop01 export]# cat words.txt hello,world,hadoophive,sqoop,flume,hellokitty,tom,jerry,worldhadoop
步骤:
1.将, 换成换行
2.排序
3.去重
4.计数
# 统计每个单词出现的次数[root@hadoop01 export]# cat words.txt | tr ',' '\n' | sort | uniq -c1 flume2 hadoop2 hello1 hive1 jerry1 kitty1 sqoop1 tom2 world
7.split
通过split
命令将大文件切分成若干小文件7.1: 按 字节将 大文件 切分成 若干小文件
7.2: 按行数将 大文件 切分成 若干小文件
通过split 选项 文件名
命令将大文件切分成若干小文件下图中以x开头的文件极为切出来的文件注意
:如果在同一个文件夹下切割,下一次切割会将上一次切割后的文件覆盖掉
8.awk
vim score.txt
zhangsan 68 99 26lisi 98 66 96wangwu 38 33 86zhaoliu 78 44 36maq 88 22 66zhouba 98 44 46
8.1: 模糊查询
搜索 zhangsan 和 lisi 的成绩
[root@node01 tmp]# awk '/zhangsan|lisi/' score.txt zhangsan 68 99 26lisi 98 66 96
8.2: 指定分割符, 根据下标显示内容
[root@node01 tmp]# awk -F ' ' '{print $1, $2, $3}' score.txtzhangsan 68 99lisi 98 66wangwu 38 33zhaoliu 78 44maq 88 22zhouba 98 44
选项
8.3: 指定分割符, 根据下标显示内容
[root@node01 tmp]# awk -F ' ' '{OFS="==="}{print $1, $2, $3}' score.txt zhangsan===68===99lisi===98===66wangwu===38===33zhaoliu===78===44maq===88===22zhouba===98===44
选项
8.4: 调用 awk 提供的函数
[root@node01 tmp]# awk -F ' ' '{print toupper($1)}' score.txt ZHANGSANLISIWANGWUZHAOLIUMAQZHOUBA
常用函数如下:
8.5: if语句 查询及格的学生信息
[root@node01 tmp]# awk -F ' ' '{if($4>60) print $1, $4 }' score.txtlisi 96wangwu 86maq 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 不及格
选项
8.6: 段内容 求学科平均分
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
通过 sed 可以实现过滤和替换的功能.
准备工作
vim 1.txt
aaa java rootbbb helloccc rtddd root nologineee rttfff ROOT nologinggg rttt
9.1:查询 功能
可选参数
9.1.1 列出 1.txt的 1~5行 的数据
sed -n -e '1,5p' 1.txt [root@node01 tmp]# sed -n -e '1,5p' 1.txt aaa java rootbbb helloccc rtddd root nologineee rtt
9.1.2 列出 1.txt的所有数据
sed -n -e '1,$p' 1.txt [root@node01 tmp]# sed -n -e '1,$p' 1.txt aaa java rootbbb helloccc rtddd root nologineee rttfff ROOT nologinggg 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 1aaa java root2bbb hello3ccc rt4ddd root nologin5eee rtt6fff ROOT nologin7ggg rttt简化版cat -n 1.txtcat -b 1.txtnl 1.txt[root@node01 tmp]# cat -n 1.txt 1 aaa java root2 bbb hello3 ccc rt4 ddd root nologin5 eee rtt6 fff ROOT nologin7 ggg rttt[root@node01 tmp]# cat -b 1.txt 1 aaa java root2 bbb hello3 ccc rt4 ddd root nologin5 eee rtt6 fff ROOT nologin7 ggg rttt[root@node01 tmp]# nl 1.txt 1 aaa java root2 bbb hello3 ccc rt4 ddd root nologin5 eee rtt6 fff ROOT nologin7 ggg rttt
9.1.4查找 1.txt中包含root行
sed -n -e '/root/p' 1.txt[root@node01 tmp]# sed -n -e '/root/p' 1.txtaaa java rootddd root nologin
9.1.5 列出 1.txt中包含root的内容,root不区分大小写,并显示行号
nl 1.txt | sed -n -e '/root/Ip'nl 1.txt | grep -i rootcat -n 1.txt | grep -i root[root@node01 tmp]# nl 1.txt | sed -n -e '/root/Ip'1 aaa java root4 ddd root nologin6 fff ROOT nologin[root@node01 tmp]# nl 1.txt | grep -i root 1 aaa java root4 ddd root nologin6 fff ROOT nologin[root@node01 tmp]# cat -n 1.txt | grep -i root 1 aaa java root4 ddd root nologin6 fff ROOT nologin
9.1.6 查找出 1.txt中 字母r
后面是多个t的行,并显示行号
nl 1.txt | sed -nr -e '/r+t/p'[root@node01 tmp]# nl 1.txt | sed -nr -e '/r+t/p' 3 ccc rt5 eee rtt7 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 rt3eee rtt5ggg rttt7
9.2 删除 功能
9.2.1 删除1.txt中前3行数据,并显示行号
nl 1.txt | sed -e '1,3d'[root@node01 tmp]# nl 1.txt | sed -e '1,3d' 4 ddd root nologin5 eee rtt6 fff ROOT nologin7 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 root2 bbb hello3 ccc rt4 ddd root nologin[root@node01 tmp]# nl 1.txt | sed -n -e '1,4p'1 aaa java root2 bbb hello3 ccc rt4 ddd root nologin
9.3修改 功能
9.3.1 在 1.txt的第二行后添加aaaaa,并显示行号
nl 1.txt | sed -e '2a aaaaa'[root@node01 tmp]# nl 1.txt | sed -e '2a aaaaa' 1 aaa java root2 bbb helloaaaaa3 ccc rt4 ddd root nologin5 eee rtt6 fff ROOT nologin7 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'bbbbb1 aaa java root2 bbb hello3 ccc rt4 ddd root nologin5 eee rtt6 fff ROOT nologin7 ggg rttt
9.4 替换 功能
9.4.1 把 1.txt中的nologin替换成为huawei,并显示行号
nl 1.txt | sed -e 's/nologin/huawei/'[root@node01 tmp]# nl 1.txt | sed -e 's/nologin/huawei/'1 aaa java root2 bbb hello3 ccc rt4 ddd root huawei5 eee rtt6 fff ROOT huawei7 ggg rttt
9.4.2 把1.txt中的1,2行替换为aaa,并显示行号
nl 1.txt | sed -e '1,2c aaa'[root@node01 tmp]# nl 1.txt | sed -e '1,2c aaa' aaa3 ccc rt4 ddd root nologin5 eee rtt6 fff ROOT nologin7 ggg rttt
9.5 对 原文件 进行操作
9.5.1 在 1.txt中把nologin替换为 huawei
sed -i -e 's/nologin/huawei/' 1.txt[root@node01 tmp]# sed -i -e 's/nologin/huawei/' 1.txt[root@node01 tmp]# cat 1.txtaaa java rootbbb helloccc rtddd root huaweieee rttfff ROOT huaweiggg 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 rootaaaddd root huaweieee rttfff ROOT huaweiggg 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.txtddd root huaweieee rttfff ROOT huaweiggg rtttnl 1.txt 查看数据[root@node01 tmp]# nl 1.txt1 ddd root huawei2 eee rtt3 fff ROOT huawei4 ggg rttt
9.6: 综合运用
9.6.1 获取ip地址
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 root4 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 root4 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 rt4 ddd root itheima5 eee rtt6 fff ROOT itheima7 ggg rttt