您的位置:首页 > 运维架构 > Shell

linux shell编程指南第十一章------------合并与分割2

2013-12-03 10:08 459 查看
c u t用来从标准输入或文本文件中剪切列或域。剪切文本可以将之粘贴到一个文本文件。
下一节将介绍粘贴用法。

c u t一般格式为:

cut [options] file1 file2

下面介绍其可用选项:

-c list 指定剪切字符数。

-f field 指定剪切域数。

-d 指定与空格和t a b键不同的域分隔符。

- c用来指定剪切范围,如下所示:

- c 1,5-7 剪切第1个字符,然后是第5到第7个字符。

-c1-50 剪切前5 0个字符。

-f 格式与- c相同。

-f 1,5 剪切第1域,第5域。

- f 1,10-12 剪切第1域,第1 0域到第1 2域。

参照上一节中的文件‘ p e r s’,现在从' p e r s '文件中剪切文本。使用冒号做其域分隔符。

[root@localhost huangcd]# cat pers
P.Jones:Office Runner:ID897
S.Round:UNIX admin:ID666
L.Clip:Personl Chief:ID982

文件中使用冒号“:”为域分隔符,故可用- d选项指定冒号,如- d:。如果有意观察第3域,
可以使用- f 3。要抽取I D域。可使用命令如下:

[root@localhost huangcd]# cut -d : -f 3 pers
ID897
ID666
ID982

c u t命令中剪切各域需用逗号分隔,如剪切域1和3,即名字和I D号,可以使用:

[root@localhost huangcd]# cut -d : -f 1,3 pers
P.Jones:ID897
S.Round:ID666
L.Clip:ID982

使用- c选项指定精确剪切数目。这种方法需确切知道开始及结束字符。通常我不用这种方
法,除非在固定长度的域或文件名上。
当信息文件传送到本机时,查看部分文件名就可以识别文件来源。要得到这条信息需抽
取文件名后三个字符。然后才决定将之存在哪个目录下。下面的例子显示文件名列表及相应
c u t命令:

[root@localhost huangcd]# cat xxx
2231DG
2232DP
3336DK
[root@localhost huangcd]# cat xxx|cut -c 4-6
1DG
2DP
6DK

[root@localhost huangcd]# who -u
root :0 2013-12-02 22:53 ? 4564
root pts/1 2013-12-02 22:56 . 4859 (:0.0)
[root@localhost huangcd]# who -u|cut -c 1-8
root
root

c u t用来从文本文件或标准输出中抽取数据列或者域,然后再用p a s t e可以将这些数据粘贴
起来形成相关文件。粘贴两个不同来源的数据时,首先需将其分类,并确保两个文件行数相
同。
p a s t e将按行将不同文件行信息放在一行。缺省情况下, p a s t e连接时,用空格或t a b键分隔
新行中不同文本,除非指定- d选项,它将成为域分隔符。
p a s t e格式为;
paste -d -s -file1 file2
选项含义如下:
-d 指定不同于空格或t a b键的域分隔符。例如用@分隔域,使用- d @。
-s 将每个文件合并成行而不是按行粘贴。
- 使用标准输入。例如ls -l |paste ,意即只在一列上显示输出。
从前面的剪切中取得下述两个文件:

基本p a s t e命令将之粘贴成两列:

[root@localhost huangcd]# cat pas1
ID897
ID666
ID892
[root@localhost huangcd]# cat pas2
P.Jones
S.Round
L.Clip
[root@localhost huangcd]# paste pas1 pas2
ID897 P.Jones
ID666 S.Round
ID892 L.Clip

要创建不同于空格或t a b键的域分隔符,使用- d选项。下面的例子用冒号做域分隔符。

[root@localhost huangcd]# paste -d : pas2 pas1
P.Jones:ID897
S.Round:ID666
L.Clip:ID892

要合并两行,而不是按行粘贴,可以使用- s选项。下面的例子中,第一行粘贴为名字,第
二行是I D号。

[root@localhost huangcd]# paste -s pas2 pas1
P.Jones S.Round L.Clip
ID897 ID666 ID892

p a s t e命令还有一个很有用的选项( -)。意即对每一个( -),从标准输入中读一次数据。
使用空格作域分隔符,以一个4列格式显示目录列表。方法如下:

[root@localhost huangcd]# ls
addaccount.sh files.out nltk-2.0.4 quote.txt
append.sed filetest nltk-2.0.4.tar.gz sedex.txt
c find.dt nohup.out sed.out
change.sed grade.txt pas1 sort.out
data.f grep.err pas2 today
delete_me_and_die huangcdcron pers var_text.sh
Desktop ipython-0.8.2 python vmware-tools-distrib
dht mix.sed PyYAML-3.10 who.out
file1 myfile1.txt PyYAML-3.10.tar.gz wow
filedt myfile.txt qrt.tmp xxx
[root@localhost huangcd]# ls | paste -d " " - - - -
addaccount.sh append.sed c change.sed
data.f delete_me_and_die Desktop dht
file1 filedt files.out filetest
find.dt grade.txt grep.err huangcdcron
ipython-0.8.2 mix.sed myfile1.txt myfile.txt
nltk-2.0.4 nltk-2.0.4.tar.gz nohup.out pas1
pas2 pers python PyYAML-3.10
PyYAML-3.10.tar.gz qrt.tmp quote.txt sedex.txt
sed.out sort.out today var_text.sh
vmware-tools-distrib who.out wow xxx

[root@localhost huangcd]# ls | paste -d : - - - -
addaccount.sh:append.sed:c:change.sed
data.f:delete_me_and_die:Desktop:dht
file1:filedt:files.out:filetest
find.dt:grade.txt:grep.err:huangcdcron
ipython-0.8.2:mix.sed:myfile1.txt:myfile.txt
nltk-2.0.4:nltk-2.0.4.tar.gz:nohup.out:pas1
pas2:pers:python:PyYAML-3.10
PyYAML-3.10.tar.gz:qrt.tmp:quote.txt:sedex.txt
sed.out:sort.out:today:var_text.sh
vmware-tools-distrib:who.out:wow:xxx

s p l i t用来将大文件分割成小文件。有时文件越来越大,传送这些文件时,首先将其分割可
能更容易。使用v i或其他工具诸如s o r t时,如果文件对于工作缓冲区太大,也会存在一些问题。
因此有时没有选择余地,必须将文件分割成小的碎片。
s p l i t命令一般格式:
split -output_file-size input-filename output-filename
这里o u t p u t - f i l e - s i z e指的是文本文件被分割的行数。s p l i t查看文件时,o u t p u t - f i l e - s i z e选项
指定将文件按每个最多1 0 0 0行分割。如果有个文件有2 8 0 0行,那么将分割成3个文件,分别有
1 0 0 0、1 0 0 0、8 0 0行。每个文件格式为x [ a a ]到x [ z z ],x为文件名首字母, [ a a ]、[ z z ]为文件名剩
余部分顺序字符组合,下面的例子解释这一点。

[root@localhost huangcd]# cat split1
this is line1
this is line2
this is line3
this is line4
this is line5
this is line6
[root@localhost huangcd]# split -2 split1
[root@localhost huangcd]# ls -lt |head
总计 1508
-rw-r--r-- 1 root root 28 12-02 23:18 xaa
-rw-r--r-- 1 root root 28 12-02 23:18 xab
-rw-r--r-- 1 root root 28 12-02 23:18 xac
-rw-r--r-- 1 root root 84 12-02 23:17 split1

文件有6行,s p l i t按每个文件两行进行了分割,并按字母顺序命名文件。为进一步确信操
作成功,观察一个新文件内容:

[root@localhost huangcd]# cat xac
this is line5
this is line6
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: