shell的uniq命令
uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。
uniq 可检查文本文件中重复出现的行列。
命令语法:
uniq [-c/d/D/u/i] [-f Fields] [-s N] [-w N] [InFile] [OutFile]
参数解释:
-c: 在每列旁边显示该行重复出现的次数。
-d: 仅显示重复出现的行列,显示一行。 -D: 显示所有重复出现的行列,有几行显示几行。 -u: 仅显示出一次的行列 -i: 忽略大小写字符的不同
-f Fields: 忽略比较指定的列数。
-s N: 忽略比较前面的N个字符。
-w N: 对每行第N个字符以后的内容不作比较。
[InFile]: 指定已排序好的文本文件。如果不指定此项,则从标准读取数据;
[OutFile]: 指定输出的文件。如果不指定此选项,则将内容显示到标准输出设备(显示终端)。
栗子
# uniq.txt
My name is Delav
My name is Delav
My name is Delav
I'm learning Java
I'm learning Java
I'm learning Java
who am i
Who am i
Python is so simple
My name is Delav
That's good
That's good
And studying Golang
1. 直接去重
uniq uniq.txt
结果为:
My name is Delav
I'm learning Java
who am i
Who am i
Python is so simple
My name is Delav
That's good
And studying Golang
2. 显示重复出现的次数
uniq -c uniq.txt
结果为:
My name is Delav
I'm learning Java
who am i
Who am i
Python is so simple
My name is Delav
That's good
And studying Golang
你会发现,上面有两行 ”My name is Delav ” 是相同的。也就是说,当重复的行不相邻时,uniq 命令是不起作用的。所以,经常需要跟 sort 命令一起使用。
sort uniq.txt | uniq -c
结果为:
And studying Golang
I'm learning Java
My name is Delav
Python is so simple
That's good
who am i
Who am i
3. 只显示重复的行,并显示重复次数
uniq -cd uniq.txt
结果为:
My name is Delav
I'm learning Java
That's good
显示所有重复的行,不能与 -c 一起使用
uniq -D uniq.txt
结果为:
My name is Delav
My name is Delav
My name is Delav
I'm learning Java
I'm learning Java
I'm learning Java
That's good
That's good
4. 忽略第几列字符
下面这里 -f 1 忽略了第一列字符,所以"who am i" 和 "Who am i" 判定为重复
uniq -c -f uniq.txt
结果为:
My name is Delav
I'm learning Java
who am i
Python is so simple
My name is Delav
That's good
And studying Golang
5. 忽略大小写
下面这里 -i 忽略了大小写,所以"who am i" 和 "Who am i" 判定为重复
uniq -c -i uniq.txt
结果为:
My name is Delav
I'm learning Java
who am i
Python is so simple
My name is Delav
That's good
And studying Golang
6. 忽略前面N个字符
下面这里 -s 4 表示忽略前面四个字符,所以"who am i" 和 "Who am i" 判定为重复
uniq -c -s uniq.txt
结果为:
My name is Delav
I'm learning Java
who am i
Python is so simple
My name is Delav
That's good
And studying Golang
7. 忽略第N个字符后的内容
uniq -c -w uniq.txt
shell的uniq命令的更多相关文章
- [shell基础]——uniq命令
uniq命令常见选项 去除重复行 -u 显示不重复的行 -d 显示有重复的行 -c 打印每一行重复的次数 测试文本内容如下: # cat 4.txt 11 ...
- Linux Shell脚本入门--Uniq命令
uniq uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用.也就是说,为了使uniq起作用,所有的重复行必须是相邻的. uniq语法 [root@www ~]# uniq [ ...
- linux shell 脚本攻略学习8---md5校验,sort排序,uniq命令详解
一.校验与核实 目前最为出名的校验技术是md5sum和sha1sum,它们对文件内容使用相应的算法来生成校验和. 举例: amosli@amosli-pc:~/learn$ md5sum text.t ...
- (转)Shell脚本编程--Uniq命令
uniq 原文:http://blog.csdn.net/xifeijian/article/details/9209627 uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用 ...
- sort与uniq命令详解
1.sort的作用 (排序) sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出. 如果 File 参数指定多个文件,那么 sort 命令将这些文件连接起来,并当作一个文件进行 ...
- Bash Shell内建命令和保留字
Bash Shell内建命令和保留字命令含义!保留字,逻辑非:不做任何事,只做参数展开.读取文件并在shell中执行它alias设置命令或命令行别名bg将作业置于后台运行bind将关键字序列与read ...
- Linux uniq命令
200 ? "200px" : this.width)!important;} --> 介绍 uniq命令是一个文本去重命令,它能对标准输入和文本文件进行去重操作,并且能将结 ...
- uniq命令注意事项,检查重复行的时候,只会检查相邻的行。
今天在使用uniq命令统计数量时,uniq -c总是得不到想要的效果,相同的行没有合并,例如 后来在http://ju.outofmemory.cn/entry/78365才看到,原来uniq检查重复 ...
- 单行bash、shell、perl命令
主题:单行经典bash.shell.perl命令 作者:luomg 摘要: 会陆陆续的写自己工作中的常用有意思的命令,争取你能看完后就能搞定常见操作, 且尽量自少提供基本shell.perl的实现方式 ...
随机推荐
- day 7 引用
1.b=a在c语言和python中的区别 c语言:a=100 a变量里面放的100 b = a b变量里面也放的100 python : a=100 内存中有个100 a放的100 ...
- day7 RHCE
6.配置本地邮件服务 在系统server0和desktop0上配置邮件服务,满足以下要求:这些系统不接收外部发送来的邮件这些系统上本地发送的任何邮件都会自动路由到 classroom.example. ...
- SRM 698 div1 RepeatString
250pts RepeatString 题意:问最少修改多少次将一个字符串修改为AA的形式.可以插入一个字符,删除一个字符,修改字符. 思路:枚举分界点,然后dp一下. /* * @Author: m ...
- C#之Lambda不得不说的用法
由于我才开始接触代码的时候遇到循环问题都是用foreach和for,慢慢就成了习惯,不愿意用其他简便的方式,偶然发现lambda能代替循环而且简便了很多.当然我用lambda也不是简便,更多是不用不行 ...
- Appium 安卓计算器demo
package testProject; import org.openqa.selenium.*; import org.openqa.selenium.remote.DesiredCapabili ...
- Egret入门(二)--windows下环境搭建
准备材料 安装Node.js TypeScript编辑器 HTTP服务器(可选) Chorme(可选) Egret 安装Node.js 打开www.nodejs.org 下载安装(全部next,全默认 ...
- Unity —— 通过鼠标点击控制物体移动
//ClickMove - - 通过鼠标点击控制物体移动 using System.Collections; using System.Collections.Generic; using Unity ...
- Paper Reading - Show and Tell: A Neural Image Caption Generator ( CVPR 2015 )
Link of the Paper: https://arxiv.org/abs/1411.4555 Main Points: A generative model ( NIC, GoogLeNet ...
- 动态语言的灵活性是把双刃剑 -- 以 Python 语言为例
本文有些零碎,总题来说,包括两个问题:(1)可变对象(最常见的是list dict)被意外修改的问题,(2)对参数(parameter)的检查问题.这两个问题,本质都是因为动态语言(动态类型语言)的特 ...
- selenium--判断元素是否存在
# coding:utf-8from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitf ...