Linux系统学习之字符处理
管道
管道是一种使用非常频繁的通信机制,我们可以使用管道符"|"来连接进程,
由管道连接起来订单进程可以自动运行,如同有一个数据流一样,所以管道表现为输入输出重定向的一种方法,
它可以把一个命令的输出内容当作下一个命令的输入内容,两个命令之间只需要管道符连接即可
使用grep搜索文本
grep [-ivnc] '需要匹配的字符' 文件名
#-i 不区分大小写
#-c 统计包含匹配的行数
#-n 输出行号
#-v 反向匹配
例子:
[root@Cfhost-170820-UCNK ~]# cat test.txt
The cat's name is Tom,What's the mouse's name?
The mouse's NAME is Jerry
They are good friends
[root@Cfhost-170820-UCNK ~]# grep 'name' test.txt //搜索含有'name'的句子
The cat's name is Tom,What's the mouse's name?
[root@Cfhost-170820-UCNK ~]# grep -i 'name' test.txt //搜索含有'name'的句子,忽略大小写
The cat's name is Tom,What's the mouse's name?
The mouse's NAME is Jerry
[root@Cfhost-170820-UCNK ~]# grep -c 'name' test.txt//含'name'的句子有多少条
1
[root@Cfhost-170820-UCNK ~]# grep -ci 'name' test.txt//含'name‘的句子有多少条,大小写可忽略
2
[root@Cfhost-170820-UCNK ~]# grep -v 'name' test.txt //搜索不含'name'的句子
The mouse's NAME is Jerry
They are good friends
[root@Cfhost-170820-UCNK ~]# grep -vi 'name' test.txt//搜索不含'name'的句子,大写的NAME也过滤掉
They are good friends
[root@Cfhost-170820-UCNK ~]# cat test.txt
The cat's name is Tom,What's the mouse's name?
The mouse's NAME is Jerry
They are good friends
[root@Cfhost-170820-UCNK ~]# cat test.txt | grep -vi 'name' ? //以上命令都可以使用管道符改写,比如上一个命令可以这样写,意思都是一样的
They are good friends
使用sort排序
sort [-ntkr] 文件名
#-n 采取数字排序
#-t 指定分隔符
#-k 指定第几列
#-r 反向排序
[root@Cfhost-170820-UCNK ~]# cat sort.txt
b:3
c:2
a:4
e:5
d:1
f:11
[root@Cfhost-170820-UCNK ~]# cat sort.txt | sort //按字母正向排序
a:4
b:3
c:2
d:1
e:5
f:11
[root@Cfhost-170820-UCNK ~]# cat sort.txt | sort -r //按字母反向排序
f:11
e:5
d:1
c:2
b:3
a:4
[root@Cfhost-170820-UCNK ~]# cat sort.txt | sort -t ":" -k 2 -n
d:1
c:2
b:3
a:4
e:5
f:11
使用uniq删除重复内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | uniq [-ic] #-i 忽略大小写 #-c 计算重复内容 [root@Cfhost-170820-UCNK ~]# cat uniq.txt | sort | uniq //需要说明的是uniq命令一般需要和sort一起使用,也就是先将文件使用进行sort排序,然后再使用uniq删除重复的内容。单独加上uniq不加sort是没有效果的。 123 abc 补充说明: [root@Cfhost-170820-UCNK ~]# cat uniq.txt | uniq abc 123 abc 123 [root@Cfhost-170820-UCNK ~]# cat uniq.txt | sort | uniq -c //使用-c参数就会在每行前面打印出改行重复的次数 2 123 |
使用cutt截取文本、使用tr做文本转换、使用paste做文本合并(还有一个文件分割用split做,这里不再说了,我目前想不到它到底有什么用)
cut -f 指定列 -d '分隔符'
[root@Cfhost-170820-UCNK ~]# cat /etc/passwd | cut -f1 -d ':'
root
bin
daemon
admin
tr命令比较简单,其主要作用在于文本转换或删除
[root@Cfhost-170820-UCNK ~]# cat /etc/passwd | tr '[a-z]' '[A-Z]'
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
paste的作用在于将文件按照行进行合并,中间使用tab隔开
[root@Cfhost-170820-UCNK ~]# cat a.txt
a b c
[root@Cfhost-170820-UCNK ~]# cat b.txt
a b c
[root@Cfhost-170820-UCNK ~]# paste a.txt b.txt
a b c a b c
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述