cut命令用来显示行中的指定部分,删除文件中指定字段。cut经常用来显示文件的内容,类似于下的type命令。 说明:该命令有两项功能,其一是用来显示文件的内容,它依次读取由参数file所指明的文件,将它们的内容输出到标准输出上;其二是连接两个或多个文件,如cut fl f2 > f3将把文件fl和几的内容合并起来,然后通过输出重定向符“>”的作用,将它们放入文件f3中。
语法: cut -d ‘分隔符’ [-cf] n [filename] (这里n是正整数) -d:指定分隔符号 -f:指定第几段 -c:后面只有一个数字表示截取第几个字符;后面跟一个数字区域,表示截取从几到几(该选项不和d,f共同使用)
[root@adai003 tmp]# cut -c1 1.txt |head -n2
r
b
[root@adai003 tmp]# cut -c1,3 1.txt |head -n2
ro
bn
[root@adai003 tmp]# cut -f1,3 -d ':' 1.txt |head -n2
root:0
bin:1
[root@adai003 tmp]# cut -f1-3 -d ':' 1.txt |head -n2
root:x:0
bin:x:1
sort命令是在Linux里非常有用,它将文件进行排序,并将排序结果标准输出。sort命令既可以从特定的文件,也可以从stdin中获取输入。
语法: sort [-t 分隔符] [options] [filename] Options: -t:指定分隔符 -n:使用纯数字排序(系统默认所有字母为0) -r:反向排序 -u:=unique 去重复 -kn1,n2:由n1区间排序到n2区间,可以只写-kn1,即对n1字段排序(n1 < n2) sort不加任何选项,则从首字符向后,依次以ASCⅡ码值进行比较,最后将它们按升续输出。
[root@adai003 tmp]# head -n3 1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@adai003 tmp]# head -n3 1.txt |sort
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
wc命令用来计算数字。利用wc指令我们可以计算文件的Byte数、字数或是列数。
语法: wc [options] [filename] Options: -l:=line 统计行数 -m:=member 统计字符数 -w:=Word 统计词数
[root@adai003 tmp]# wc -l !$
wc -l 2.txt
2 2.txt
[root@adai003 tmp]# wc -m 2.txt
10 2.txt
[root@adai003 tmp]# cat !$
cat 2.txt
1234
qwer
[root@adai003 tmp]# cat -A 2.txt
1234$
qwer$
[root@adai003 tmp]# wc -w 2.txt
4 2.txt
[root@adai003 tmp]# cat 2.txt
1234 456 789,10
qwer
说明: wc -m会统计文件内所有字符,包括隐藏的换行符“&”;wc -w是以空格作为分隔符进行词组统计的。
uniq命令用于报告或忽略文件中的重复行,一般与sort命令结合使用(即:去重复)。
语法: uniq [options] [filename] Options: -c:=count 在每列旁边显示该行重复出现的次数
[root@adai003 tmp]# cat !$
cat 2.txt
1234
456 789,10
1234
qwer
456
[root@adai003 tmp]# uniq -c 2.txt
1 1234
1 456 789,10
1 1234
1 qwer
1 456
[root@adai003 tmp]# sort 2.txt |uniq -c
2 1234
1 456
1 456 789,10
1 qwer
说明: 直接使用uniq命令,2.txt内容显示并没有变化,使用sort排序后再用uniq命令,重复行被合并,即:在对文件进行去重之前需要先进行排序!
tee命令用于将数据重定向到文件,会删除文件内原有内容,与“>”不同的是,tee会把定向的文件内容显示出来。
语法: tee [options] [filename] Options: -a:向文件中重定向时使用追加模式(=“>>”)
[root@adai003 tmp]# cat 3.txt
00000000000
[root@adai003 tmp]# sort 2.txt |uniq -c |tee 3.txt
2 1234
1 456
1 456 789,10
1 qwer
[root@adai003 tmp]# cat 3.txt
2 1234
1 456
1 456 789,10
1 qwer
[root@adai003 tmp]# sort 2.txt |uniq -c |tee -a 3.txt
2 1234
1 456
1 456 789,10
1 qwer
[root@adai003 tmp]# cat 3.txt
2 1234
1 456
1 456 789,10
1 qwer
2 1234
1 456
1 456 789,10
1 qwer
tr命令可以对来自标准输入的字符进行替换、压缩和删除,它可以将一个字符变成另一个字符,也可以将一组字符变成另一组字符。
语法: tr [源字符] [目标字符]
[root@adai003 tmp]# echo "adailinux" |tr 'a' 'A'
AdAilinux 替换一个字符
[root@adai003 tmp]# echo "adailinux" |tr '[al]' '[AL]'
AdAiLinux 替换多个字符
[root@adai003 tmp]# echo "adailinux" |tr '[a-z]' '[A-Z]'
ADAILINUX
split命令可以将一个大文件分割成很多个小文件,有时需要将文件分割成更小的片段,比如为提高可读性,生成日志等。
语法: split [options] [filename] -b:指定每一输出档案的大小,默认单位为 byte,可自定义单位,如 split -b 100M filename -l:指定每一个输出档案的行数多少 eg1: 指定大小
[root@adai003 tmp]# split -b 100 1.txt
[root@adai003 tmp]# ls
xaa
xab
xac
xad
[root@adai003 tmp]# rm -rf x*
[root@adai003 tmp]# split -b 100 1.txt adai.
可以指定文件前缀!
[root@adai003 tmp]# ls
adai.aa
adai.ab
adai.ac
adai.ad
eg2: 指定行数
[root@adai003 tmp]# wc -l 1.txt
20 1.txt
[root@adai003 tmp]# split -l 5 1.txt
[root@adai003 tmp]# ls
xaa
xab
xac
xad
[root@adai003 tmp]# wc -l x*
5 xaa
5 xab
5 xac
5 xad
20 总用量
命令连接符: “||”、“&&”、“;”
(adsbygoogle = window.adsbygoogle || []).push({});