Linux管道相关命令
文章目录
- (1)cut (提取)
- (2)sort (排序)
- (3)wc命令
- (4)uniq (唯一)
- (5)tee (管道输出)
- (6)tr (替换)
- (7)split (切割)
- (8)awk
- (9)sed
(1)cut (提取)
作用:cut
根据条件 从命令结果中 提取 对应内容
vim 1.txt 内容:
111:aaa:bbb:ccc
222:ddd:eee:fff
333:ggg:hhh
444:iii
小结:通过 cut 动作 目标文件
可以根据条件 提取对应内容
(2)sort (排序)
作用:sort可针对文本文件的内容,以行为单位来排序。
第一步: 对字符串排序
cat 表示查看当前内容
[root@node01 tmp]# cat 2.txt
banana
apple
pear
orange
pear通过sort排序后
[root@node01 tmp]# sort 2.txt
apple
banana
orange
pear
pear
第二步: 去重排序
作用:在输出行中去除重复行。
[root@node01 tmp]# sort -u 2.txt
apple
banana
orange
pear
第三步: 对数值排序
默认按照字符串
排序
[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
第四步: 对成绩排序
vim score.txt 准备内容
注意看第二列
lisi01,68,99,26
lisi02,98,66,96
lisi03,38,33,86
lisi04,78,44,36
lisi05,88,22,66
lisi06,98,44,46倒序显示 所有内容
[root@node01 tmp]# sort -t ',' -k2nr score.txt结果:
lisi02,98,66,96
lisi06,98,44,46
lisi05,88,22,66
lisi04,78,44,36
lisi01,68,99,26
lisi03,38,33,86
(3)wc命令
第一步: 显示指定文件 字节数, 单词数, 行数 信息.
准备内容:
cat 查看文本
[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
第二步: 只显示 文件 的行数
只显示 文件 的行数
[root@node01 opt]# wc -l 4.txt
6 4.txt
第三步: 统计多个文件的 行数 单词数 字节数
[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 总用量
第四步: 查看 /etc
目录下 有多少个 子内容
[root@hadoop01 export]# ls /etc | wc -w
240
小结
通过 wc 文件
就可以 统计 文件的 字节数、单词数、行数
(4)uniq (唯一)
作用:uniq 命令用于检查及删除文本文件中重复出现的行,一般与 sort 命令结合使用。
第一步:实现去重效果
# 排序
[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
第二步:不但去重,还要 统计出现的次数
要求:去重和统计出现的次数
[root@hadoop01 export]# cat 5.txt | sort | uniq -c2 李四 1002 麻七 702 王五 901 张三 982 赵六 95
小结
通过 uniq [选项] 文件
就可以完成 去重行 和 统计次数
(5)tee (管道输出)
将去重统计的结果 放到 a.txt、b.txt、c.txt 文件中
[root@hadoop01 export]# cat 5.txt | sort | uniq -c | tee a.txt b.txt c.txt
小结
通过 tee
可以将命令结果 通过管道 输出到 多个文件中
(6)tr (替换)
第一步: 实现 替换效果
# 将 小写i 替换成 大写 I
[root@node01 opt]# echo "itheima" | tr 'i' 'I'
ItheIma# 把itheima的转换为大写
[root@node01 opt]# echo "itheima" | tr '[a-z]' '[A-Z]'
ITHEIMA# 把 HELLO 转成 小写
[root@node01 opt]# echo "HELLO" |tr '[A-Z]' '[a-z]'
hello
第二步: 实现删除效果
需求: 删除abc1d4e5f中的数字
[root@node01 opt]# echo 'abc1d4e5f' | tr -d '[0-9]'
abcdef
第三步: 单词计数
准备内容
[root@hadoop01 export]# cat words.txt
hello,world,hadoop
hive,sqoop,flume,hello
kitty,tom,jerry,world
hadoop1. 将,换成 换行 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 (切割)
第一步: 按 字节 将 大文件 切分成 若干小文件
[root@node01 opt]# split -b 10k 1.txt
第二步: 按 行数 将 大文件 切分成 若干小文件
[root@node01 opt]# split -l 5 2.txt
小结
通过 split 选项 文件名
命令将大文件 切分成 若干小文件
(8)awk
作用:通过 awk
实现 模糊查询, 按需提取字段, 还可以进行 判断 和 简单的运算等
第一步: 搜索 zhangsan 和 lisi 的成绩
第二步: 指定分割符, 根据下标显示内容
第三步: 指定分割符, 根据下标显示内容
第四步: 调用 awk 提供的函数
第五步: if语句 查询及格的学生信息
第六步: 段内容 求学科平均分
(9)sed
作用:通过 sed 可以实现 过滤 和 替换 的功能
第一步: 实现 查询 功能
准备数据:vi 1.txt
aaa java root
bbb hello
ccc rt
ddd root nologin
eee rtt
fff ROOT nologin
ggg rttt练习1 列出 1.txt的 1~5行 的数据
[root@node01 opt]# sed -n -e '1,5p' 1.txt
aaa java root
bbb hello
ccc rt
ddd root nologin
eee rtt练习2 列出1.txt的所有数据
[root@node01 opt]# sed -n -e '1,$p' 1.txt
aaa java root
bbb hello
ccc rt
ddd root nologin
eee rtt
fff ROOT nologin
ggg rttt练习3 列出1.txt的所有数据 且 显示行号
[root@node01 opt]# 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其他方法
简化版
1:cat -n 1.txt
2:cat -b 1.txt
3:nl 1.txt练习4: 查找1.txt中包含root行
[root@node01 opt]# sed -n -e '/root/p' 1.txt
aaa java root
ddd root nologin
练习5 列出1.txt中包含root的内容,root不区分大小写,并显示行号
[root@node01 opt]# nl 1.txt | sed -n -e '/root/Ip'1 aaa java root4 ddd root nologin6 fff ROOT nologin其他方法:1. nl 1.txt | grep -i root2. cat -n 1.txt | grep -i root
练习6 查找出1.txt中 字母`r`后面是多个t的行,并显示行号
[root@node01 opt]# 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
第二步: 实现 删除 功能
练习1 删除01.txt中前3行数据,并显示行号
[root@node01 opt]# nl 1.txt | sed -e '1,3d'4 ddd root nologin5 eee rtt6 fff ROOT nologin7 ggg rttt练习2 保留1.txt中前4行数据,并显示行号
[root@node01 opt]# nl 1.txt | sed -e '5,$d'1 aaa java root2 bbb hello3 ccc rt4 ddd root nologin或者 nl 1.txt | sed -n -e '1,4p'
第三步: 实现 修改 功能
练习1: 在1.txt的第二行后添加aaaaa,并显示行号
[root@node01 opt]# nl 1.txt | sed -e '2a aaaaa'1 aaa java root2 bbb hello
aaaaa3 ccc rt4 ddd root nologin5 eee rtt6 fff ROOT nologin7 ggg rttt练习2 在1.txt的第1行前添加bbbbb,并显示行号
[root@node01 opt]# 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
第四步: 实现 替换 功能
练习1 把1.txt中的nologin替换成为huawei,并显示行号
[root@node01 opt]# 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
练习2 把1.txt中的1,2行替换为aaa,并显示行号
[root@node01 opt]# nl 1.txt passwd | sed -e '1,2c aaa'
nl: passwdaaa3 ccc rt4 ddd root nologin5 eee rtt6 fff ROOT nologin7 ggg rttt
第五步: 对 原文件 进行操作
练习1 在1.txt中把nologin替换为 huawei
[root@node01 opt]# sed -i -e 's/nologin/huawei/' 1.txt练习2 在1.txt文件中第2、3行替换为aaaaaa
[root@node01 opt]# sed -i -e '2,3c aaa' 1.txt练习3 删除1.txt中前2行数据,并且删除原文件中的数据
[root@node01 opt]# sed -i -e '1,2d' 1.txt
第六步: 综合 练习
练习1 获取ip地址
[root@node01 opt]# ifconfig eth0 | grep "inet addr" | sed -e 's/^.*inet addr://' | sed -e 's/Bcast:.*$//'
192.168.100.100 练习2 从1.txt中提出数据,匹配出包含root的内容,再把nologin替换为itheima
[root@node01 opt]# nl 1.txt | grep 'root' | sed -e 's/nologin/itheima/'1 ddd root huawei或者1. nl 01.txt | sed -n -e '/root/p' | sed -e 's/nologin/itheima/'2. nl 01.txt | sed -n -e '/root/{s/nologin/itheima/p}' #只显示替换内容的行练习3 从1.txt中提出数据,删除前2行,并把nologin替换为itheima,并显示行号
[root@node01 opt]# nl 1.txt | sed -e '1,2d' | sed -e 's/nologin/itheima/'3 fff ROOT huawei4 ggg rttt
Linux管道相关命令相关推荐
- linux 管道相关命令(待学)
1.1 cut cut:以某种方式按照文件的行进行分割 参数列表: -b 按字节选取 忽略多字节字符边界,除非也指定了 -n 标志 -c 按字符选取 -d 自定义分隔符,默认为制表符. -f 与-d一 ...
- Linux操作系统相关命令
Linux操作系统相关命令 Linux中的命令格式为:command [options] [arguments] 中括号表示可选的,即有些命令不需要选项也不需要参数,但有的命令在运行时需要多个选项或参 ...
- Linux网络相关命令
Linux网络相关命令 1 ping 2 netstat 3 tranceroute 4 ARP 5 tcpdump 1 ping 作用:检测当前主机与目标主机之间的连通性(不是100%准确,有的服务 ...
- Linux进程相关命令
一.ps命令 Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程, ps命令时列出当前进程的快照,想要动态的显示进程的相关信息,需要使用top命令 ① ...
- linux内核模块相关命令:lsmod,depmod,modprobe,modinfo,insmod,rmmod 使用说明
原文链接:http://www.cnblogs.com/jacklikedogs/p/4659249.html inux内核模块相关命令:lsmod,depmod,modprobe,modinfo,i ...
- Linux用户相关命令
目录 简介 用户ID: UID-- UserIDentification 用户组ID:GID -- Group IDentification 用户和用户组关系 User相关命令 useradd -- ...
- linux shell 相关命令
查看空闲cpu ]#top -b -n 1 | grep Cpu | awk -F "," '{print $4}' | awk '{print $1}' 输出数字: echo & ...
- Linux管道命令及管道相关命令(详细)
文章目录 1. 什么是管道命令 2.第一个管道命令 3. cut 3.1 cut能干什么? 3.2 实现 3.2.1 截取出1.txt文件中前2行的第5个字符 3.2.2 截取出1.txt文件中前2行 ...
- Linux学习(三):管道相关命令
学习目标 熟练掌握以下命令 1.cut 2.sort 3.wc 4.uniq 5.tee 6.tr 7.split 8.awk 9.sed / grep 熟练掌握以下命令 1.cut 准备工作 成绩表 ...
- linux内存相关命令汇总
内存整体信息 查看内存剩余free: e0005055@ibudev20:~$ freetotal used free shared buff/cache available Mem: 3279172 ...
最新文章
- C# 多线程并发锁模式-总结
- python运行命令_对python中执行DOS命令的3种方法总结
- python--批量下载豆瓣图片
- UNIX-LINUX编程实践教程-第八章-实例代码注解-写一个简单的shell
- Docker上部署MySQLCluster(基于mysql/mysql-cluster镜像)
- python中函数包括参数函数吗_Python中的函数---函数的定义和参数
- javascript window alert
- python 高级_Python高级特性(一)
- java 中生成文档_在Java中生成专业的公文文档
- 前端如何更精准的评估开发时间
- digital ocean 内存不足时增加swap文件的方法
- 【原创】大数据基础之Spark(1)Spark Submit即Spark任务提交过程
- 安全模式:天猫 App 启动保护实践
- visio图形包解压
- 迷途视频解析HTML源码,抖音短视频无水印解析接口源码
- 微信小程序的页面布局(1)
- 【金猿投融展】Techsun天正聚合——联接品牌与人
- 【EXLIBRIS】关于我现有的大部分PDF书目以及共享
- 《国际经济学(商学院)》
- 浏览器功能大比拼:谁更出彩?