首页 > 代码库 > 文本处理命令

文本处理命令

第六单元

文本处理命令

 

diff

 

diff        参数

            -u              ####使用统一输出格式####

            -r              ####从指定目录开始文件执行递归比较###

            -c              ###显示上下文周围的行####

1 diff 命令用于比较两个文件的内容 , 以了解其区别。

diff    file     file1               ####可以显示两个文件的不同####

 

 

过程如下:

[root@localhost ~]# vim file

[root@localhost ~]# vim file1

[root@localhost ~]# cat file

hello westos

[root@localhost ~]# cat file1

hello westos

123

[root@localhost ~]# diff file file1   ###可以显示两个文件的不同####

1a2

> 123

 

 

2  diff  -u   file   file1              ####使用统一输出格式####

 

过程如下:

[root@localhost ~]# diff -u file file1      ####使用统一输出格式####

--- file2017-04-29 21:09:22.196294782 -0400

+++ file12017-04-29 21:09:55.873294782 -0400

@@ -1 +1,2 @@

 hello westos

+123

 

 

3 它还可用于创建补丁文件

diff   -u   file   file1  >   file.path     ###将统一输出格式输入到补丁文件file.path#######

 

过程如下:

 

[root@localhost ~]# cat file.path

--- file2017-04-29 21:09:22.196294782 -0400

+++ file12017-04-29 21:09:55.873294782 -0400

@@ -1 +1,2 @@

 hello westos

+123

 

 

  修补命令 patch

 

补丁版替换原始文件 , 但当指定 -b 选项时 , 可以制作备份。将用 .orig 文件名后缀

重命名原始文件

 

patch   file    file.path                  ####对file文件打补丁#####

cat     file              ###补丁版替换原始文件,打上补丁后原文件就不见了####

patch    -b    file     file.path     ####当指定-b可以制作备份将用 .orig 文件名后缀重命名原始文件

 

过程如下:

[root@localhost ~]# yum install patch -y         ###安装patch####

[root@localhost ~]# patch file file.path    ####对file文件打补丁#####

patching file file

[root@localhost ~]# cat file           ###补丁版替换原始文件,打上补丁后原文件就不见了####

hello westos

123

[root@localhost ~]# vim file

[root@localhost ~]# patch -b file file.path     ####当指定-b可以制作备份将用 .orig 文件名后缀重命名原始文件

patching file file

 

 

 

grep

 

grep 将显示文件中与模式匹配的行

grep             参数

                 -i             ####执行不区分大小写搜索

                 -n             ###前置返回行的行号####

                 -r             ###对文件执行递归式搜索,从命名目录开始###

                 -c             ####显示一共过滤出几行###

                 -v             ###显示不包含模式的行(反向过滤)

                 -E             ###过滤包含多个关键字的行###

过程如下:

[root@localhost ~]# cd /mnt/

[root@localhost mnt]# ls

[root@localhost mnt]# cp /etc/passwd .

[root@localhost mnt]# ls

passwd

[root@localhost mnt]# vim passwd

[root@localhost mnt]# cat 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

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

test:root:test

root:test:root

root:root:test

TEST:root:ROOT

[root@localhost mnt]# grep test passwd     ###精确过滤,只过滤含有test的行,区分大小写#####

test:root:test

root:test:root

root:root:test

[root@localhost mnt]# grep -i test passwd    ###粗略过滤,不区分大小写,都过滤###

test:root:test

root:test:root

root:root:test

TEST:root:ROOT

[root@localhost mnt]# grep -i test passwd -v   ###反向过滤,过滤除了含有test的行(不区分大小写)

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

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@localhost mnt]# grep -i -E "test|root" passwd    ###过滤多个关键字###

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

test:root:test

root:test:root

root:root:test

TEST:root:ROOT

[root@localhost mnt]# grep -i -E "^test" passwd     ###过滤以关键字开头的行###

test:root:test

TEST:root:ROOT

[root@localhost mnt]# grep -i -E "test$" passwd  ###过滤以关键字结尾的行###

test:root:test

root:root:test

[root@localhost mnt]# grep "test" passwd           ###精确过滤,只过滤含有test的行,区分大小写#####

test:root:test

root:test:root

root:root:test

[root@localhost mnt]# grep "test" passwd  | grep -E "^test|test$" ###过滤包含test的行中开头或者结尾是test的行####

test:root:test

root:root:test

[root@localhost mnt]# grep "test" passwd  | grep -E "^test|test$" -v ###过滤包含test的行中开头和结尾都不是test的行####

root:test:root

[root@localhost mnt]# grep test passwd -c    ####显示过滤出几行###

3

[root@localhost mnt]# touch file

[root@localhost mnt]# touch file1

[root@localhost mnt]# touch file2

[root@localhost mnt]# echo westos > file

[root@localhost mnt]# cat file

westos

[root@localhost mnt]# grep westos -r /mnt/              ###过滤出目录下含有关键字的文件####

/mnt/file:westos

[root@localhost mnt]# grep westos -r /mnt/ -n         ###过滤出目录下含有关键字的文件并且显示在第几行####

/mnt/file:1:westos

 

 

cut 命令

用于 “ 剪切 ” 文件中的文本字段或列并将其显示到标准输出

cut             参数

                -d                 ###指定用于提取字段的分隔符###

                -f                 ###指定要从每行中提取的字段####

                -c                 ###指定要从每行中提取的字符###

 

过程如下:

[root@localhost mnt]# cat 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

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

test:root:test

root:test:root

root:root:test

TEST:root:ROOT

[root@localhost mnt]# cut -d : -f 1 passwd   ###-d指定用于提取字段的分隔符,-f指定要从每行中提取的字段,该命令指提出passwd中每行的第一个字段#####

root

bin

daemon

adm

lp

sync

shutdown

halt

mail

operator

games

ftp

test

root

root

TEST

[root@localhost mnt]# cut -d : -f 1,7 passwd    ###提取passwd中的第1和第7字段###

root:/bin/bash

bin:/sbin/nologin

daemon:/sbin/nologin

adm:/sbin/nologin

lp:/sbin/nologin

sync:/bin/sync

shutdown:/sbin/shutdown

halt:/sbin/halt

mail:/sbin/nologin

operator:/sbin/nologin

games:/sbin/nologin

ftp:/sbin/nologin

test

root

root

TEST

[root@localhost mnt]# cut -d : -f 1-7 passwd    ###提取passwd中的第1到7字段####

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

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

test:root:test

root:test:root

root:root:test

TEST:root:ROOT

[root@localhost mnt]# cut -c 2-4 passwd    ###-c指定要从每行中提取的字符该命令指提取第2到第4个字符###

oot

in:

aem

dm:

p:x

ync

hut

alt

ail

per

ame

tp:

est

oot

oot

EST

 

 

sort 命令

sort 用于排序文本数据。该数据可以位于文件中或其他命令输出中。 Sort 通常与管道一起使用

sort             参数

                 -n                ###按数值从小到大排序###

                 -k                ###设置排序字段####

                 -t                ###指定其他分隔符(默认为空格)###

 

过程如下:

[root@localhost mnt]# sort file        ####开头从小到大排序#####

0

1

1

12

12

2

2

23

23

3

3

3

3

4

4

4

4

4

4

5

5

6

6

7

7

8

8

9

9

[root@localhost mnt]# sort -n file    ###按数字从小到大排序###

0

1

1

2

2

3

3

3

3

4

4

4

4

4

4

5

5

6

6

7

7

8

8

9

9

12

12

23

23

[root@localhost mnt]# sort -rn file       ###按数字从大到小排序####

23

23

12

12

9

9

8

8

7

7

6

6

5

5

4

4

4

4

4

4

3

3

3

3

2

2

1

1

0

[root@localhost mnt]# sort -rnu file             ###按数字从大到小排序,且每个数字只显示一次####

23

12

9

8

7

6

5

4

3

2

1

0

[root@localhost mnt]# sort -n file

3:a:0

3:a:1

3:a:1

3:a:12

3:a:12

3:a:2

3:a:23

3:a:23

3:a:3

3:a:3

3:a:3

3:a:3

3:a:4

3:a:4

3:a:4

3:a:4

3:a:4

3:a:5

3:a:5

3:a:6

3:a:7

3:a:7

3:a:8

3:a:8

3:a:9

3:a:9

11:a:2

12:a:4

25:a:6

 

 uniq 命令

uniq“ 删除 ” 文件中重复的相邻行

uniq            参数

                -u              ###仅显示唯一行###

                -d              ###显示重复行###

                -c              ###每行显示一次(包括出现计数)

过程如下:

[root@localhost mnt]# sort -t :  -k 3 -n file | uniq -c   ###-t指定分隔符,-k指定排序字段,即将file文件按第三字段的数字从小到大排序再通过管道将排序的结果每行只显示一次,并计数出现次数#####   

      1 3:a:0

      2 3:a:1

      1 11:a:2

      1 3:a:2

      4 3:a:3

      1 12:a:4

      5 3:a:4

      2 3:a:5

      1 25:a:6

      1 3:a:6

      2 3:a:7

      2 3:a:8

      2 3:a:9

      2 3:a:12

      2 3:a:23

 

 

 

 

 

[root@localhost mnt]# sort -rn file | uniq -c    ###每行只显示一次并统计重复的个数####

      2 23

      2 12

      2 9

      2 8

      2 7

      2 6

      2 5

      6 4

      4 3

      2 2

      2 1

      1 0

[root@localhost mnt]# sort -rn file | uniq -d     ###只显示重复行###

23

12

9

8

7

6

5

4

3

2

1

[root@localhost mnt]# sort -rn file | uniq -u        ###只显示唯一行###

0

 

 

tr命令

 

tr 用于转字符 : 即 , 如果给定了两个字符范围 , 则只要发现某个字符位于第一个范围中, 就会将其转换为第二个范围中对等的字符。该命令通常在 shell 脚本中使用 , 以按预期情况转换数据

tr ‘A-Z‘ ‘a-z‘ <file                     ###将大写字母转换成小写字母###

 

过程如下:

 

[root@localhost mnt]# vim file

[root@localhost mnt]# cat file

westos

WESTOS

[root@localhost mnt]# tr ‘a-z‘ ‘A-Z‘ < file ###将小写字母转换成大写字母###

WESTOS

WESTOS

[root@localhost mnt]# tr ‘A-Z‘ ‘a-z‘ < file ###将大写字母转换成小写字母###

westos

westos

[root@localhost mnt]# cat file            ###不改变原文件###

westos

WESTOS

 

 

 

 sed 命令

 

sed 命令是流编辑器 , 用于对文本数据流执行编辑。假定要处理一个文件名 , sed 将对文件中的所有行执行搜索和替换 , 以将修改后的数据发送到标准输出 ; 即 , 其实际上并不修改现有文件。与 grep 一样 , sed通常在管道中使用

 

 

sed    ‘s/old/new/g’   filename             ###执行字符串转换,将filename全文的old替换成new,该替换并不会改变原文件#####

sed    ‘s/old/new/g’       -i      filename   ###-i指该替换改变原文件###

sed -e ‘s/sbin/westos/g‘ -e ‘s/nologin/hello/g‘ -i  passwd    ###-e指连接,文件的两处都要替换####

sed ‘3,5s/westos/sbin/g‘ westos     ####将westos文件的第3行到第5行的westos替换成sbin####

sed -e  ‘3s/sbin/westos/g‘ -e ‘5s/sbin/westos/g‘ westos  ####将westos文件里的第三行和第五行的sbin替换成westos####

sed       5x    westos                     ###将westos文件的第五行屏蔽###

sed       5d    westos                     ###将westos文件的第五行删除###

sed       5p    westos                     ###复制westos的第5行###

sed       -n    5p    westos                ###只显示westos的第5行###

sed       -n    3,5p   westos               ###只显示westos的第3到第5行###

sed   -ne    3p    -ne    5p    westos      ###显示westos的第3和第5行###

 

 

 

过程如下:

[root@localhost mnt]# vim passwd

[root@localhost mnt]# cat 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

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@localhost mnt]# sed ‘s/sbin/westos/g‘ passwd      ###-g指全文替换,将passwd文件里的全部sbin替换成westos#####

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/westos/nologin

daemon:x:2:2:daemon:/westos:/westos/nologin

adm:x:3:4:adm:/var/adm:/westos/nologin

lp:x:4:7:lp:/var/spool/lpd:/westos/nologin

sync:x:5:0:sync:/westos:/bin/sync

shutdown:x:6:0:shutdown:/westos:/westos/shutdown

ftp:x:14:50:FTP User:/var/ftp:/westos/nologin

[root@localhost mnt]# cat 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

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@localhost mnt]# sed ‘s/sbin/westos/g‘ -i  passwd   ###-i使替换改变原文件###

[root@localhost mnt]# cat passwd                  ###查看,发现原文件发生了改变###

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/westos/nologin

daemon:x:2:2:daemon:/westos:/westos/nologin

adm:x:3:4:adm:/var/adm:/westos/nologin

lp:x:4:7:lp:/var/spool/lpd:/westos/nologin

sync:x:5:0:sync:/westos:/bin/sync

shutdown:x:6:0:shutdown:/westos:/westos/shutdown

ftp:x:14:50:FTP User:/var/ftp:/westos/nologin

[root@localhost mnt]# sed -e ‘s/westos/sbin/g‘ -e ‘s/nologin/hello/g‘ -i  passwd                       ###将passwd文件的westos替换成sbin,nologin替换成hello#####

[root@localhost mnt]# cat passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/hello

daemon:x:2:2:daemon:/sbin:/sbin/hello

adm:x:3:4:adm:/var/adm:/sbin/hello

lp:x:4:7:lp:/var/spool/lpd:/sbin/hello

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

ftp:x:14:50:FTP User:/var/ftp:/sbin/hello     

 

也可以将替换规则写入文件里然后用sed   -f执行文件里的规则

vim   rule       ###在rule下编辑规则

sed    -f   rule    passwd     ###执行rule文件里的替换规则###

文件里的内容只需写单引号内的内容即可

s/sbin/wetos/g

  s/hello/nologin/g

 

过程如下:

[root@localhost mnt]# vim rule

[root@localhost mnt]# sed -f rule passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/westos/nologin

daemon:x:2:2:daemon:/westos:/westos/nologin

adm:x:3:4:adm:/var/adm:/westos/nologin

lp:x:4:7:lp:/var/spool/lpd:/westos/nologin

sync:x:5:0:sync:/westos:/bin/sync

shutdown:x:6:0:shutdown:/westos:/westos/shutdown

ftp:x:14:50:FTP User:/var/ftp:/westos/nologin

[root@localhost mnt]# cat passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/hello

daemon:x:2:2:daemon:/sbin:/sbin/hello

adm:x:3:4:adm:/var/adm:/sbin/hello

lp:x:4:7:lp:/var/spool/lpd:/sbin/hello

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

ftp:x:14:50:FTP User:/var/ftp:/sbin/hello

[root@localhost mnt]# sed -f rule -i  passwd

[root@localhost mnt]# cat passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/westos/nologin

daemon:x:2:2:daemon:/westos:/westos/nologin

adm:x:3:4:adm:/var/adm:/westos/nologin

lp:x:4:7:lp:/var/spool/lpd:/westos/nologin

sync:x:5:0:sync:/westos:/bin/sync

shutdown:x:6:0:shutdown:/westos:/westos/shutdown

ftp:x:14:50:FTP User:/var/ftp:/westos/nologin

 

 

cat passwd -b > westos      ####将passwd的内容加上行号输出到westos文件里####

 

过程如下:   

root@localhost mnt]# cat passwd -b             ###-b指加上行号###

     1root:x:0:0:root:/root:/bin/bash

     2bin:x:1:1:bin:/bin:/westos/nologin

     3daemon:x:2:2:daemon:/westos:/westos/nologin

     4adm:x:3:4:adm:/var/adm:/westos/nologin

     5lp:x:4:7:lp:/var/spool/lpd:/westos/nologin

     6sync:x:5:0:sync:/westos:/bin/sync

     7shutdown:x:6:0:shutdown:/westos:/westos/shutdown

     8ftp:x:14:50:FTP User:/var/ftp:/westos/nologin

[root@localhost mnt]# cat passwd -b > westos      ####将passwd的内容加上行号输出到westos文件里#####

[root@localhost mnt]# cat westos

     1root:x:0:0:root:/root:/bin/bash

     2bin:x:1:1:bin:/bin:/westos/nologin

     3daemon:x:2:2:daemon:/westos:/westos/nologin

     4adm:x:3:4:adm:/var/adm:/westos/nologin

     5lp:x:4:7:lp:/var/spool/lpd:/westos/nologin

     6sync:x:5:0:sync:/westos:/bin/sync

     7shutdown:x:6:0:shutdown:/westos:/westos/shutdown

     8ftp:x:14:50:FTP User:/var/ftp:/westos/nologin

[root@localhost mnt]# sed ‘3,5s/westos/sbin/g‘ westos     ####将westos文件的第3行到第5行的westos替换成sbin####

     1root:x:0:0:root:/root:/bin/bash

     2bin:x:1:1:bin:/bin:/westos/nologin

     3daemon:x:2:2:daemon:/sbin:/sbin/nologin

     4adm:x:3:4:adm:/var/adm:/sbin/nologin

     5lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

     6sync:x:5:0:sync:/westos:/bin/sync

     7shutdown:x:6:0:shutdown:/westos:/westos/shutdown

     8ftp:x:14:50:FTP User:/var/ftp:/westos/nologin

[root@localhost mnt]# sed -e  ‘3s/sbin/westos/g‘ -e ‘5s/sbin/westos/g‘ westos              ####将westos文件里的第三行和第五行的sbin替换成westos####

     1root:x:0:0:root:/root:/bin/bash

     2bin:x:1:1:bin:/bin:/westos/nologin

     3daemon:x:2:2:daemon:/westos:/westos/nologin

     4adm:x:3:4:adm:/var/adm:/westos/nologin

     5lp:x:4:7:lp:/var/spool/lpd:/westos/nologin

     6sync:x:5:0:sync:/westos:/bin/sync

     7shutdown:x:6:0:shutdown:/westos:/westos/shutdown

     8ftp:x:14:50:FTP User:/var/ftp:/westos/nologin

[root@localhost mnt]# sed 5x westos        ###将westos文件的第五行屏蔽###

     1root:x:0:0:root:/root:/bin/bash

     2bin:x:1:1:bin:/bin:/westos/nologin

     3daemon:x:2:2:daemon:/westos:/westos/nologin

     4adm:x:3:4:adm:/var/adm:/westos/nologin

 

     6sync:x:5:0:sync:/westos:/bin/sync

     7shutdown:x:6:0:shutdown:/westos:/westos/shutdown

     8ftp:x:14:50:FTP User:/var/ftp:/westos/nologin

[root@localhost mnt]# sed 5d westos       ###将westos文件的第五行删除###

     1root:x:0:0:root:/root:/bin/bash

     2bin:x:1:1:bin:/bin:/westos/nologin

     3daemon:x:2:2:daemon:/westos:/westos/nologin

     4adm:x:3:4:adm:/var/adm:/westos/nologin

     6sync:x:5:0:sync:/westos:/bin/sync

     7shutdown:x:6:0:shutdown:/westos:/westos/shutdown

     8ftp:x:14:50:FTP User:/var/ftp:/westos/nologin

[root@localhost mnt]# sed 5p westos          ###复制westos的第5行###

     1root:x:0:0:root:/root:/bin/bash

     2bin:x:1:1:bin:/bin:/westos/nologin

     3daemon:x:2:2:daemon:/westos:/westos/nologin

     4adm:x:3:4:adm:/var/adm:/westos/nologin

     5lp:x:4:7:lp:/var/spool/lpd:/westos/nologin

     5lp:x:4:7:lp:/var/spool/lpd:/westos/nologin

     6sync:x:5:0:sync:/westos:/bin/sync

     7shutdown:x:6:0:shutdown:/westos:/westos/shutdown

     8ftp:x:14:50:FTP User:/var/ftp:/westos/nologin

[root@localhost mnt]# sed -n  5p westos          ###只显示westos的第5行###

     5lp:x:4:7:lp:/var/spool/lpd:/westos/nologin

[root@localhost mnt]# sed -n  3,5p westos       ###只显示westos的第3到第5行###

     3daemon:x:2:2:daemon:/westos:/westos/nologin

     4adm:x:3:4:adm:/var/adm:/westos/nologin

     5lp:x:4:7:lp:/var/spool/lpd:/westos/nologin

[root@localhost mnt]# sed -ne 3p -ne 5p westos        ###显示westos的第3和第5行###

     3daemon:x:2:2:daemon:/westos:/westos/nologin

     5lp:x:4:7:lp:/var/spool/lpd:/westos/nologin


文本处理命令