当前位置:网站首页>Shell 实现文件基本操作(切割、排序、去重)
Shell 实现文件基本操作(切割、排序、去重)
2022-07-02 23:31:00 【梦幻通灵】
使用Shell 自带的工具,实现对大文本文件的操作,满足日常数据处理的需求。
一、命令 cut -切割
cut 可实现对文本按列进行文本处理,特别适合于大文件的数据处理。
基本语法是 cut [option] filename
参数
cut -f 列号,获取第几列;
cut -c 以字符为单位分割;
cut -d 以字节为单位分割,自动忽略多字节字符边界,如加 -n ,则不分割多字节字符;
cut n- 指定第n列开始;
cut n-m 指定从n列到m列;
demo演示
1)字节切割
原始文件如下所示。
获取前两列。输入命令 cut info.text -d " " -f 1-2,自定义分割,按空格分割。
2)切割bash的PID
在虚拟机中找到bash的信息,如下图所示。
执行命令 ps -aux | grep bash | head -n 1 | cut -d " " -f 8,查找bash进程,取第一行,空格分割,按列截取,取第8列,结果如下图所示。
二、命令 sort -排序
sort 对文件进行排序,并将排序结果标准或重定向输出到指定文件。
基本语法是 **sort [option] **
参数
sort -n 按照数值大小排序;
sort -r 以相反的顺序排序;
sort -t 分隔符 默认空格分隔符,排序时的分隔符;
sort -k 指定需要排序的列;
sort -o 将排序后的结果存入指定的文件;
sort -u 结果唯一,即去重过的;
demo演示
1)排序
原始文件如下所示。
执行命令 sort -t " " -k2n,2 infodata.txt,第二列按照数字升序排序,注意排序要指定从哪列到哪列,效果如下图所示。
上图结果中存在重复的数据,如何去重呢?
在命令中添加 -uk1,2,完整命令 sort -t " " -k2n,2 -uk1,2 infodata.txt,效果如下所示。
如何将重复的数据打印出来呢?
使用命令 sort infodata.txt | uniq -dc,效果如下图所示。
三、命令 uniq - 去重
uniq 以行为单位,行与行之间比较去重,只能是对有序的文本进行有效的去重,因此与sort命令结合使用。
基本语法是 **uniq [option] **
参数
uniq -c 统计行出现的次数;
uniq -d 只显示重复行并去重;
uniq -u 只显示唯一行;
uniq -i 忽略大小写;
uniq -f 忽略前N个字段,字段用空白字符分割;
demo演示
1)排序去重
只展示出现一次的行,执行命令 sort infodata.txt | uniq -u,效果如下图所示。
对于有行号的文本文件处理,使用 -f 参数忽略第一个行号字段,对之后的字段去重处理。
测试发现 sort 去重 似乎对最后一行不生效(最后一行重复未包含),在实际应用中再验证。
参考博客
【1】https://blog.csdn.net/qq_43382735/article/details/121007185
边栏推荐
- Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation
- Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
- 详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
- Is the multitasking loss in pytoch added up or backward separately?
- 多进程编程(一):基本概念
- 国外的论文在那找?
- Mutual exclusion and synchronization of threads
- Markdown tutorial
- FRP reverse proxy +msf get shell
- Where can I find the English literature of the thesis (except HowNet)?
猜你喜欢
Mutual exclusion and synchronization of threads
Sysdig analysis container system call
pageoffice-之bug修改之旅
University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量
奥斯陆大学:Li Meng | 基于Swin-Transformer的深度强化学习
Multiprocess programming (I): basic concepts
Introduction and use of ftrace tool
Bigder:32/100 测试发现的bug开发认为不是bug怎么处理
为什么网站打开速度慢?
Redis21 classic interview questions, extreme pull interviewer
随机推荐
Where can I find foreign papers?
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
LeedCode1480. Dynamic sum of one-dimensional array
【雅思阅读】王希伟阅读P2(阅读填空)
Should you study kubernetes?
请问大家在什么网站上能查到英文文献?
百数不断创新,打造自由的低代码办公工具
Slf4j + logback logging framework
大学生课堂作业2000~3000字的小论文,标准格式是什么?
What is the standard format of a 2000-3000 word essay for college students' classroom homework?
Several methods of the minimum value in the maximum value of group query
How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?
Pageoffice - bug modification journey
Two common methods and steps of character device registration
Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)
Markdown tutorial
helm 基础学习
Multiprocess programming (II): Pipeline
Is there a specific format for English papers?
Feature Engineering: summary of common feature transformation methods