uniq cut wc 命令详解

uniq

 uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用。也就是说,为了使uniq起作用,所有的重复行必须是相邻的。

uniq语法

[root@www ~]# uniq [-icu]
选项与参数:
-i   :忽略大小写字符的不同;
-c  :进行计数
-u  :只显示唯一的行

 

testfile的内容如下

复制代码
cat testfile
hello
world
friend
hello
world
hello
复制代码

 

直接删除未经排序的文件,将会发现没有任何行被删除

复制代码
#uniq testfile  
hello
world
friend
hello
world
hello
复制代码

 

排序文件,默认是去重

#cat words | sort |uniq
friend
hello
world

 

排序之后删除了重复行,同时在行首位置输出该行重复的次数

#sort testfile | uniq -c
1 friend
3 hello
2 world

 

仅显示存在重复的行,并在行首显示该行重复的次数

#sort testfile | uniq -dc
3 hello
2 world

 

仅显示不重复的行

sort testfile | uniq -u
friend  

 

cut

cut命令可以从一个文本文件或者文本流中提取文本列。

cut语法

[root@www ~]# cut -d'分隔字符' -f fields <==用于有特定分隔字符
[root@www ~]# cut -c 字符区间            <==用于排列整齐的信息
选项与参数:
-d  :后面接分隔字符。与 -f 一起使用;
-f  :依据 -d 的分隔字符将一段信息分割成为数段,用 -f 取出第几段的意思;
-c  :以字符 (characters) 的单位取出固定字符区间;

 

PATH 变量如下

[root@www ~]# echo $PATH
/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games
# 1 | 2       | 3   | 4       | 5            | 6            | 7

 

将 PATH 变量取出,我要找出第五个路径。

#echo $PATH | cut -d ':' -f 5
/usr/local/bin

 

将 PATH 变量取出,我要找出第三和第五个路径。

#echo $PATH | cut -d ':' -f 3,5
/sbin:/usr/local/bin

 

将 PATH 变量取出,我要找出第三到最后一个路径。

echo $PATH | cut -d ':' -f 3-
/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games

 

将 PATH 变量取出,我要找出第一到第三个路径。

#echo $PATH | cut -d ':' -f 1-3
/bin:/usr/bin:/sbin:
 

 

将 PATH 变量取出,我要找出第一到第三,还有第五个路径。

echo $PATH | cut -d ':' -f 1-3,5
/bin:/usr/bin:/sbin:/usr/local/bin

 

实用例子:只显示/etc/passwd的用户和shell

#cat /etc/passwd | cut -d ':' -f 1,7 
root:/bin/bash
daemon:/bin/sh
bin:/bin/sh

 

 wc

统计文件里面有多少单词,多少行,多少字符。

wc语法

[root@www ~]# wc [-lwm]
选项与参数:
-l  :仅列出行;
-w  :仅列出多少字(英文单字);
-m  :多少字符;

 

默认使用wc统计/etc/passwd

#wc /etc/passwd
40   45 1719 /etc/passwd

40是行数,45是单词数,1719是字节数

 

wc的命令比较简单使用,每个参数使用如下:

复制代码
#wc -l /etc/passwd   #统计行数,在对记录数时,很常用
40 /etc/passwd       #表示系统有40个账户

#wc -w /etc/passwd  #统计单词出现次数
45 /etc/passwd

#wc -m /etc/passwd  #统计文件的字节数
1719
复制代码

 

转载:https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858385.html

posted @ 2017-12-03 20:24  小胖帅哥  阅读(237)  评论(0编辑  收藏  举报