`
tomhibolu
  • 浏览: 1385371 次
文章分类
社区版块
存档分类
最新评论

UNIX Shell 编程(4)

 
阅读更多

UNIX Shell 编程(4)

cut命令
可以从数据文件或者命令的输出中截取所需的数据域。
命令格式:cut -cchars file
chars表示要截取哪些文字,可以是数字。
file表示文件,如果不指定file,cut从标准输出读入输入,即可把cut命令作为管道的过滤器。
如:
[root@localhost misc]# who
root pts/1 2009-04-15 09:15 (10.3.34.117)
fedora tty7 2009-04-15 09:46 (:0)
[root@localhost misc]# who | cut -c1-8
root
fedora
说明:
cut -c1-8表示把输入的每一行的第1到8个字符截取出来,并作为标准输出。

cut命令的-d和-f选项
命令:cut -ddchar -ffields file
dchar是数据中分隔各字段的分隔符
fields表示要从文件file中截取的字段
如:
[root@localhost misc]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
fedora:x:500:500:Fedora:/home/fedora:/bin/bash
[root@localhost misc]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
fedora

cut的-f选项是对应制表符。
如:
[root@localhost programs]# cat phonebook
Alica Chebb 973-555-2015
Barbara Swingle 201-555-9257
Liz Stachiw 212-555-2298
Susan Goldberg 201-555-7776
Tony Iannino 973-555-1295
[root@localhost programs]# cut -f1 phonebook
Alica Chebb
Barbara Swingle
Liz Stachiw
Susan Goldberg
Tony Iannino
[root@localhost programs]# cut -f2 phonebook
973-555-2015
201-555-9257
212-555-2298
201-555-7776
973-555-1295


paste命令:与cut相反,它可以把多行合并在一起。
如:
[root@localhost programs]# cat names
Tony
Emanuel
Lucy
Ralph
Fred
[root@localhost programs]# cat addresses
55-23 Vine Street, Miami
39 University Place, New York
17E. 25th Street, New York
38 Chauncey St., Bensonhurst
17 E. 25th Street, New York
[root@localhost programs]# cat numbers
(307)555-5356
(212)555-3456
(212)555-9959
(212)555-7741
(212)555-0040
[root@localhost programs]# paste names numbers addresses
Tony (307)555-5356 55-23 Vine Street, Miami
Emanuel (212)555-3456 39 University Place, New York
Lucy (212)555-9959 17E. 25th Street, New York
Ralph (212)555-7741 38 Chauncey St., Bensonhurst
Fred (212)555-0040 17 E. 25th Street, New York

使用-d选项——则不使用制表符分隔各行的多个字符,如下:
[root@localhost programs]# paste -d'+' names numbers addresses
Tony+(307)555-5356+55-23 Vine Street, Miami
Emanuel+(212)555-3456+39 University Place, New York
Lucy+(212)555-9959+17E. 25th Street, New York
Ralph+(212)555-7741+38 Chauncey St., Bensonhurst
Fred+(212)555-0040+17 E. 25th Street, New York

-s选项,告诉paste把同一文件中的行粘贴在一起。
如:
[root@localhost programs]# cat names
Tony
Emanuel
Lucy
Ralph
Fred
[root@localhost programs]# paste -s names
Tony Emanuel Lucy Ralph Fred


sed是用来编辑数据的程序,指流编辑器,sed不能用于交互。
格式:sed command file
command是作用于指定文件file各行的跟ed同样风格的命令。
如未指定文件,则把标准输入作为处理对象。
例子:
[root@localhost programs]# cat intro
The Unix operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
in the late 1960s. One of the primary goals in
the design of the Unix system was to create an
environment that promoted efficient program
developments.
把Unix改为UNIX。
[root@localhost programs]# sed s/Unix/UNIX/ intro
The UNIX operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
in the late 1960s. One of the primary goals in
the design of the UNIX system was to create an
environment that promoted efficient program
developments.

sed的-n选项
提取文件的指定行。
例如:
[root@localhost programs]# sed -n '1,2p' intro
The Unix operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
只打印包含UNIX的行:
[root@localhost programs]# sed -n '/UNIX/p' temp
The UNIX operating system was pioneered by Ken
the design of the UNIX system was to create an

删除行,例如:
[root@localhost programs]# sed '1,2d' intro
in the late 1960s. One of the primary goals in
the design of the Unix system was to create an
environment that promoted efficient program
developments.
删除包含UNIX的行:
[root@localhost programs]# sed '/UNIX/d' temp
Thompson and Dennis Ritchie at Bell Laboratories
in the late 1960s. One of the primary goals in
environment that promoted efficient program
developments.

sed命令汇总示例:
——————————————————————————————————————
sed '5d' 删除第5行
sed '/[Tt]est/d' 删除包含test和Test的行
sed -n'20,25p' text 显示text文件的第20行到25行
sed '1,10s/unix/UNIX/g' intro 把intro文件前10行的unix改为UNIX
sed '/jan/s/-1/-5/' 把所有包含jan的行中的第一个-1改为-5
sed 's/...//' data 删除data文件每一行的前3个字符
sed 's/...$//' data 删除data文件每一行的最后3个字符
sed -n 'l' text 显示text文件的所有行,并把所有不可打印字符显示为/nn,制表符显示为/t
——————————————————————————————————————

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics