UNIX Shell 编程(4)

简介: UNIX Shell 编程(4)   cut命令 可以从数据文件或者命令的输出中截取所需的数据域。 命令格式:cut -cchars file chars表示要截取哪些文字,可以是数字。

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
——————————————————————————————————————

目录
相关文章
|
1月前
|
存储 Shell Linux
【Shell 命令集合 网络通讯 】Linux 显示Unix-to-Unix Copy (UUCP) 系统的状态信息 uustat命令 使用指南
【Shell 命令集合 网络通讯 】Linux 显示Unix-to-Unix Copy (UUCP) 系统的状态信息 uustat命令 使用指南
26 0
|
2月前
|
Ubuntu Linux Shell
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
59 0
|
7天前
|
监控 Shell 开发工具
Shell编程
Shell编程
|
26天前
|
存储 Java Shell
bigdata-04-shell编程基础
bigdata-04-shell编程基础
12 0
|
28天前
|
Shell Linux C++
【Shell 编程设计】 编写自己的清理后台的Shell脚本
【Shell 编程设计】 编写自己的清理后台的Shell脚本
30 1
|
28天前
|
存储 Shell 数据安全/隐私保护
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
23 0
|
28天前
|
Shell C语言 C++
【Shell 编程指南】shell中的(),{}几种语法用法
【Shell 编程指南】shell中的(),{}几种语法用法
17 0
|
28天前
|
Shell 程序员 Linux
【Shell 编程指南】shell运算操作符之(())
【Shell 编程指南】shell运算操作符之(())
19 0
|
2月前
|
Unix Shell Linux
在Unix/Linux Shell中,管道(`|`)和重定向
在Unix/Linux Shell中,管道(`|`)和重定向
23 1
|
3月前
|
Shell
Shell 编程快速入门 之 函数基础知识
Shell 编程快速入门 之 函数基础知识
67 0
Shell 编程快速入门 之 函数基础知识