文本处理命令之sed命令详解

sed行处理命令详解

一、简介

  sed命令是一种在线编辑器。一个面向字符流的非交互式编辑器,也就是说sed不允许用户与它进行交互操作。sed是按行来处理文本内容的,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。在shell中,使用sed来批量修改文本内容是非常方便的。

参考网址:https://blog.51cto.com/13517084/2069074

二、sed用法

sed命令能进行增删改查操作

格式:

sed [OPTION]... {script-only-if-no-other-script} [input-file]...
即
sed [选项] [动作] [输出文件]

选项:

-n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到终端上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
-e :直接在命令列模式上进行 sed 的动作编辑;
-f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作;
-r :sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
-i :直接修改读取的文件内容,而不是输出到终端。
动作说明: [n1[,n2]]function
n1, n2 :不见得会存在,一般代表[选择进行动作的行数],举例来说,如果我的动作是需要在 10 到 20 行之间进行的,则[ 10,20[动作行为] ]

function:

a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(在下一行添加)
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除, d 后面通常不接任何东西;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(在上一行插入);
p :列印,即将某个选择的数据打印出来。通常 p 与参数 sed -n 一起使用
s :取代,可以直接进行取代的工作!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g ,将1-20行包含old的全部替换为new

正则中的元字符:

$ 表示行尾 
^ 表示行首
[a-z0-9]表示字符范围
[^]表示除了字符集中的字符以外的字符

sed的正则中  \(\)  和 \{m,n\} 需要转义

. 表示任意字符  
* 表示零个或者多个  
\+ 一次或多次  
\? 零次或一次    
\| 表示或语法

三、sed实例

3.1.查询

准备一个txt文件

[root@VM_0_10_centos shellScript]# cat txt.txt 
2 this is a test
3 Are you like awk
This's a test
10 There are orange,apple,mongo

1)显示txt文件第3行的内容

# 加-n和不加的区别(不加-n会在原文件下面打印需要打印的行。加-n只显示需要打印的行)
[root@VM_0_10_centos shellScript]# sed '3p' txt.txt 
2 this is a test
3 Are you like awk
This's a test
This's a test
10 There are orange,apple,mongo
[root@VM_0_10_centos shellScript]# sed -n '3p' txt.txt 
This's a test

2)连续显示多行信息输出到屏幕

[root@VM_0_10_centos shellScript]# sed -n '1,3p' txt.txt 
2 this is a test
3 Are you like awk
This's a test

3)显示包含"s"的行。'//':表示过滤内容,可以匹配正则表达式

[root@VM_0_10_centos shellScript]# sed -n '/s/p' txt.txt 
2 this is a test
This's a test

PS:需匹配多个条件,使用 " , " 分隔;同一行多个命令使用";"分隔

[root@VM_0_10_centos shellScript]# sed -n '1p;2p' txt.txt 
2 this is a test
3 Are you like awk

3.2.增加

使用/etc/passed文件操作,先备份好。使用备份的操作

以行为单位新增或添加

1)将 /etc/passwd 的内容列出并且列出行号,同时,将第 2~5 行删除!

[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2,5d'
     1    root:x:0:0:root:/root:/bin/bash
     6    sync:x:5:0:sync:/sbin:/bin/sync
     7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8    halt:x:7:0:halt:/sbin:/sbin/halt
     9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10    operator:x:11:0:operator:/root:/sbin/nologin
# 如果只删除第二行,只需要'2d'即可

2)删除第3行到最后一行

[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '3,$d'
     1    root:x:0:0:root:/root:/bin/bash
     2    bin:x:1:1:bin:/bin:/sbin/nologin

3)在第2行后面添加一行,在第2行前面添加一行

[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2a hello sed'
1    root:x:0:0:root:/root:/bin/bash
2    bin:x:1:1:bin:/bin:/sbin/nologin
hello sed
3    daemon:x:2:2:daemon:/sbin:/sbin/nologin

 [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2i before sed'
 1 root:x:0:0:root:/root:/bin/bash
 before sed
 2 bin:x:1:1:bin:/bin:/sbin/nologin

如果是要添加两行或以上。需在添加的信息后面接 " \ " ,然后回车,输入要添加的信息

[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2a add1\
> add2'
     1    root:x:0:0:root:/root:/bin/bash
     2    bin:x:1:1:bin:/bin:/sbin/nologin
add1
add2
     3    daemon:x:2:2:daemon:/sbin:/sbin/nologin

3.3.更新

以行为单位的替换

 

猜你喜欢

转载自www.cnblogs.com/HeiDi-BoKe/p/11685621.html
今日推荐