Shell笔记——命令:Sort,uniq,join,cut,paste,split

来源:互联网 发布:淘宝子账号怎么开通 编辑:程序博客网 时间:2024/04/30 21:59

转自:http://blog.csdn.net/wklken/article/details/6562098

Sort,uniq,join,cut,paste,split

命令—— Sort

Sort 可将许多不同的域按不同的列顺序分类

命令格式:

sort –cmu –o out-putfile [other options] *pos1 *pos2 input-files

选项:

   -c 测试文件是否已经分类

   -m 合并两个分类文件

   -u 删除所有复制行

   -o 存储 sort 结果的输出文件名

   -b 使用域进行分类是,忽略首个空格

   -n 指定分类是域上数字分类

   -t 域分隔符;用非空个或者 tab 键分割域

   -r 对分类次序或比较求逆

   +n n 为域号,使用此域开始分类

   n n 为域号,在分类比较时忽略此域,一般与 +n 一起使用

   post m,n 传递到 m,n , m 为域号, n 为开始分类字符数

 

示例 :

1.       保存输出

$sort –o result sortfile

$sort sortfile > result

 

Sort 的启动方式, sort 认为一空格 / 多空格为分隔符,要加入其他的,必须使用 -t ,执行时,先查看 -t, 若是有,使用其进行分割,若是无,使用空格

 

2.       查看文件是否已排序

$sort –c sortfile

3.       使用其他分隔符

$sort –t   : sortfile

4.       排完序后求逆

$sort –t : -r sortfile

5.       唯一分类,原文件中重复行去除

$sort –u sortfile

6.       指定分类域, 1 开始

$sort –t : -k 4 sortfile

$sort –t : -k 4 –k 1 sortfile

7.       指定 sort 序列

$sort +0 -2 +3 sortfile

8.       将两个分类文件合并

$sort –m sorted-file1 sorted_file2

 

 

命令—— uniq

Uniq 从文本文件中去除或禁止重复行,一般 uniq 假定文件已分类排序,并且结果正确 [sort –u 唯一性选项去除所有的重复行 ]

 

Uniq 中重复行指持续不断重复出现的行

 

格式 :uniq –udc –f input-file output-file

选项:

     -u 只显示不重复行

     -d 只显示有重复的行,每种重复显示一行

     -c 打印每一重复行出现的次数

     -f n 为数字,前 n 个域被忽略

1. 只显示非重复行

$uniq –u sortfile

2. 提取非重复行到某个文件

$uniq –u sortfile result

3. 只显示重复行

$uniq –d sortfile

4. 打印重复行及其出现的次数

$uniq –c sortfile

5. 忽略比较指定列

$uniq –f2 parts.txt

 

 

命令—— join

将来自两个分类文本文件的行连在一起

前提 : file1,file2 已分类

每个文件都有一些元素与另一文件相关——连

有点像求并集

注: joio 时文本域要少于 20=

格式: join [options] input-file   input-file2

选项:

   an       n 为数字,用于连接时从文件 n 中显示不匹配行 ,-a1 表示第一个文件的不匹配行

   o n,m    n 为文件号, m 为域号,   1,3 表示只显示文件 1 的第三域

   j n m   n 为文件号, m 为域   使用其他域做连接域

   t 域分隔符   用来设置非空格 / 非 tab 分隔符

1.       连接两个文件 [ 默认连接域为域 0]

$Join name.txt. turn.txt  

2.       显示第一个文件的不匹配行

$join –a1 name.txt town.txt

3.       设置显示连接结果

$join –o 1.1,2.2 name.txt town.txt

第一个文件第一个域和第二个文件第二个域作为显示结果

4.       设置连接域

$join –j 1 3 –j 2 2 file1 file2

 

 

命令—— cut

用来从标准输入或文本文件中剪切列或域

可将剪切到的文本贴到另一个文件中

 

格式 :cut [options] file1 file2

选项:

     -c list  指定剪切字符数

     -f field 指定剪切域数

     -d 指定与空格 /tab 不同的分隔符

     -c 指定剪切的字符范围,字符,如 -c 1,5,7  第 1,5,6,7 字符    -c 1-50 前 50 个字符

     -f  指定剪切的域范围   -f 1,5 剪切 1,5 两个域    -f 1,10-12 剪切 1,10,11,12 四个域

   

1.       使用域分隔符

$cut –d : -f3 data

[root@localhost temp]# cut -d: -f1 /etc/passwd |head -5
root
bin
daemon
adm
lp

–d : 指示cut改用:为分隔符,-f1表示第一个字段意思。

2.       剪切指定域

$cut –d : -f1,3 data   ##表示取出各行第一及第三个字段意思

3.       剪切字符

$who –u | cut –c 1-8

 

[root@localhost temp]# who -u
root     tty1         2011-10-19 22:09  old         2463 (:0)
root     pts/0        2011-11-04 08:48   .          7804 (192.168.0.86)
root     pts/2        2011-10-31 09:25  old        18934 (:0.0)
root     pts/3        2011-10-31 09:47  old        18934 (:0.0)
[root@localhost temp]# who -u |cut -c 1-8
root
root
root
root

命令: paste

Paste 将数据贴到相关文件中

存在两个不同来源 数据应先分类,确保文件行数同

格式 :paste –d –s file1 file2

选项:

-d 指定不同分隔符

-s 将每个文件合并成行而不是按行黏贴

 

File1

1

2

File2

A

B

 

1.       合并之

$paste file1 file2

1         A

2         B

2.       指定分隔符

$paste –d: file2 file1

         A:1

         B:2

3.       合并两行,而非按行黏贴

$paste –s file1 file2

 

命令: split

用来将文件切分为小文件

格式: split –output_file_size input_filename output_filename

其中 out_file_size 为被分割行数,默认 1000

原创粉丝点击