栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux文本常用工具,cut、tr、head、tail、wc、sort、uniq

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux文本常用工具,cut、tr、head、tail、wc、sort、uniq

一 、Linux cut 命令

cut命令用于显示每行从开头算起num1到num2的文字

语法:

cut [option]  [file]

常用参数:

-c  以字符为单位进行分割
-d  自定义分隔符,后面可以跟任何字符
-f   与 -d 一起使用,指定显示那个区域
	  -f3,指第三列
	  -f1,2,3指第一,二,三列
	  -f1-3,7指第1-3列和7列	  

例子:

-c参数
[root@ying test]# echo "hello world" | cut -c2
e
[root@ying test]# echo "hello world" | cut -c3
l

-df参数   -d后面可以跟任何字符,以空格为分隔符,截取第二列
[root@ying test]# echo "hello world" | cut -d" " -f2
world
二 、Linux tr 命令
tr 命令用于转换或删除文件中的字符

语法:

tr [Option]... set1 set2

常用参数:

-c		反选字符。
-d		删除指令字符
-s		缩减连续重复的字节成指定的单个字符
-t		削减set1指定范围,使之与set2设定长度相等

例子:

-s选项,将空格字符缩进成一个空格
[root@ying test]# echo "a  b     c    d e" | tr -s " "
a b c d e

-dc选项,-d删除,-c反选,删除除了数字以外的字符
[root@ying test]# echo "j1kh uuf2yab a3sd " | tr -dc [0-9]
123

1.-t选项 将小写字母转换成大写字母
[root@ying test]# echo "j1kh uuf2yab a3sd " | tr -t [a-z] [A-Z]
J1KH UUF2YAB A3SD
2.-t选项 将空格字符转换成-字符
[root@ying test]# echo "j1kh uuf2yab a3sd " | tr -t " " "-"
j1kh-uuf2yab-a3sd-
3.-t选项,将数字转换成字母
[root@ying test]# echo "j1kh uuf2yab a3sd " | tr -t [0-9] [a-j]
jbkh uufcyab adsd
三 、Linux head 命令

head命令用于查看文本开头部分的内容,默认前10行

语法:

head [Option] [File]

常用参数:

-q		隐藏文件名
-v		显示文件名
-c<数目>		显示的字节数
-n<行数>		显示文本头部n行的内容

例子:

-n选项,显示前两行
[root@ying test]# head -n2 number.txt 
1
2
-n选项,不显示后8行
[root@ying test]# head -n -8 number.txt 
1
2
-c选项,截取前三个字符
[root@ying test]# echo "abcdefg" |head -c3
abc
四 、Linux tail 命令

tail命令用于查看文本结尾部分的内容,默认后10行

语法:

tail [Option] [File]

常用选项:

-f		循环读取,一般用于查看日志实时变化
-q		不显示处理信息
-v		显示详细的处理信息
-c<数目>	显示的字节数
-n<行数>	显示文本的尾部n行的内容

例子:

-n选项,显示后两行
[root@ying test]# tail -n2 number.txt 
9
10
-n选项,从第八行开始显示,包括第八行
[root@ying test]# tail -n +8 number.txt
8
9
10
-c选项,截取倒数第四个字符以后的字符,不包括第四个
[root@ying test]# echo "abcdefg"|tail -c4
efg
五 、 Linux wc 命令

wc命令用于计算字数

语法:

wc [Option] [file]

常用参数:

-c		显示bytes数
-l		显示行数
-w		显示字数

例子:

-c选项,显示字节数
[root@ying test]# printf "abcd"|wc -c
4
-c选项,显示字节数,一个汉字等于三个字节
[root@ying test]# printf "你好"|wc -c
6

-l选项,显示行数
[root@ying test]# wc -l number.txt
11 number.txt
-w		显示字数
[root@ying test]# printf "你 好"|wc -w
2
六、Linux sort 命令

sort命令用于将文本内容加以排序

语法:

sort [Option] [file]

常用参数:

-r		以相反的顺序来排序
-n		按照数值的大小排序
-f		排序时,忽略文本中的大小写
-t		自定义分隔符
-k#		按照使用字符分割的#列来整理,能够使用多次

例子:

-n -r 选项,按照数值排序,以相反的顺序排序
[root@ying test]# cat number.txt 
1
2
3
[root@ying test]# sort -rn number.txt
3
2
1

-f选项,忽略大小写
[root@ying test]# cat number.txt 
c
B
A
a
[root@ying test]# sort -f number.txt 
A
a
B
c

-t -k 选项,-t定义分隔符为空格,-k使第二列排序
[root@ying test]# cat number.txt 
a 2
b 1
c 3
[root@ying test]# sort -t" " -k2 number.txt 
b 1
a 2
c 3
七 、 Linux uniq命令

uniq命令用于检查及删除文本中重复出现的行列,
一般与sort命令结合使用

语法:

uniq	[Option] [file]

常用参数:

-c		显示每行重复出现的次数
-d		仅显示重复过的行
-u		仅显示不曾重复的行

例子:

默认
[root@ying test]# cat number.txt 
1
1
2
3
3
[root@ying test]# uniq number.txt 
1
2
3
-c选项 显示每行重复的次数
[root@ying test]# uniq -c number.txt 
      2 1
      1 2
      2 3
 -d 只显示重复的行
[root@ying test]# uniq -d number.txt 
1
3
-u 只显示不曾重复的行
[root@ying test]# uniq -u number.txt 
2
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/845522.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号