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 :直接修改读取的文件内容,而不是输出到终端。禁止与-n一起使用,会将原文件清空;
动作说明: [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 的动作可以搭配正规表示法!例如 ,20s/old/new/g ,将1-20行包含old的全部替换为new

正则中的元字符:

$ 表示行尾
^ 表示行首
[a-z0-]表示字符范围
[^]表示除了字符集中的字符以外的字符
& 正则表达式所匹配的内容 sed的正则中 \(\) 和 \{m,n\} 需要转义 . 表示任意字符
* 表示零个或者多个
\+ 一次或多次  
\? 零次或一次
\| 表示或语法
\b字符串\b表示正则匹配单词

不显示文件中的空行

# !在sed、awk find中表示取反
grep -v '^$' /tmp/passwd
# 去掉空行和以#开头的行
cat /tmp/passwd |grep -v '^$' | grep -v '^#'
sed '/^$/d' /tmp/passwd
# 匹配空格,不显示,!表示不显示有空格的行
awk '!/^$/' /tmp/passed
sed -n '/^$/!p' /tmp/passwd

三、sed实例

3.1.搜索p

准备一个txt文件

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

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

# 加-n和不加的区别(不加-n会在原文件下面打印需要打印的行。加-n只显示需要打印的行)
[root@VM_0_10_centos shellScript]# sed '3p' txt.txt
this is a test
Are you like awk
This's a test
This's a test
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
this is a test
Are you like awk
This's a test

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

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

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

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

搜索并执行命令

1)搜索passwd文件中带root的字符,将其shell替换为/bin/bashsed

# p直接接在后面是打印出替换的这一行;p前面加上;p会打印出修改的行及包含root的行
[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed -n '/root/{s/bash/bashsed/p}'
root:x:::root:/root:/bin/bashsed
[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed -n '/root/{s/bash/bashsed/;p}'
root:x:::root:/root:/bin/bashsed
operator:x:::operator:/root:/sbin/nologin

2)匹配包含bash字符,将其替换为bashsed,并退出

[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed -n '/bash/{s/bash/bashsed/;p}'
root:x:::root:/root:/bin/bashsed
apache:x::::/home/apache:/bin/bashsed
usertest1:x::::/home/usertest1:/bin/bashsed
[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed -n '/bash/{s/bash/bashsed/p}'
root:x:::root:/root:/bin/bashsed
apache:x::::/home/apache:/bin/bashsed
usertest1:x::::/home/usertest1:/bin/bashsed
[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed -n '/bash/{s/bash/bashsed/;p;q}'
root:x:::root:/root:/bin/bashsed

搜索并替换

格式:

sed 's/要被取代的字串/新的字串/g'

1)打印出ip信息。先使用ifconfig eth0查看网卡,使用grep -w过滤出只包含inet的行,使用sed命令用正则表达式匹配截取掉ip开头和结尾的数据

[root@VM_0_10_centos shellScript]# ifconfig eth0 | grep -w inet | sed 's/^.*inet//g' | sed 's/netmask.*$//g'
172.16.0.10

2)对所有以小写或大写a-zA-Z范围开头的行,执行替换操作

[root@VM_0_10_centos shellScript]# sed '/^[a-zA-Z]/s/r/u/' /tmp/passwd
uoot:x:::root:/root:/bin/bash
[root@VM_0_10_centos shellScript]# sed '/^[a-zA-Z]/s/r/u/g' /tmp/passwd
uoot:x:::uoot:/uoot:/bin/bash
[root@VM_0_10_centos shellScript]# sed '/^[a-zA-Z]/{s/r/u/}' /tmp/passwd
uoot:x:::root:/root:/bin/bash
[root@VM_0_10_centos shellScript]# sed '/^[a-zA-Z]/{s/r/u/g}' /tmp/passwd
uoot:x:::uoot:/uoot:/bin/bash

3)在passwd文件最好添加内容

# 将EOF输出的内容赋给cat命令输入到passwd文件中(将最后一个命令作为前一个命令的输入)
[root@VM_0_10_centos shellScript]# cat >> /tmp/passwd << EOF
>
>
>
> EOF

多点编辑 -e

[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed -e '3,$d' -e 's/bash/bashsed/g'
root:x:::root:/root:/bin/bashsed
bin:x:::bin:/bin:/sbin/nologin

3.2.增加  a或i

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

以行为单位新增或删除

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

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

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

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

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

[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2a hello sed'
root:x:::root:/root:/bin/bash
bin:x:::bin:/bin:/sbin/nologin
hello sed
daemon:x:::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'
root:x:::root:/root:/bin/bash
bin:x:::bin:/bin:/sbin/nologin
add1
add2
daemon:x:::daemon:/sbin:/sbin/nologin

3.3.替换  c

以行为单位的替换

1)将2~5行的内容替换为change

[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2,5c change'
root:x:::root:/root:/bin/bash
change
sync:x:::sync:/sbin:/bin/sync

3.4.直接修改原文件内容(危险动作)

sed 可以直接修改文件的内容,不必使用管道命令或数据流重导向! 不过,由於这个动作会直接修改到原始的文件,所以请你千万不要随便拿系统配置来测试!

1)先备份在修改文件内容

[root@VM_0_10_centos shellScript]# sed -i.bak 's/[1-9]/yy/g' /tmp/passwd
[root@VM_0_10_centos shellScript]# ls /tmp/
passwd
passwd.bak

[root@VM_0_10_centos shellScript]# sed -i.bak 's#y#1234#g' /tmp/passwd

2)利用 sed 将 regular_express.txt 内每一行结尾若为 . 则换成 !

[root@www ~]# sed -i 's/\.$/\!/g' regular_express.txt

3)利用 sed 直接在 regular_express.txt 最后一行加入『# This is a test』

[root@www ~]# sed -i '$a # This is a test' regular_express.txt

由於 $ 代表的是最后一行,而 a 的动作是新增,因此该文件最后新增『# This is a test』!

3)变量替换,只能使用双引号识别

[root@VM_0_10_centos shellScript]# x=
[root@VM_0_10_centos shellScript]# y=new
[root@VM_0_10_centos shellScript]# sed "s/$x/$y/g" /tmp/passwd

3.5 后项引用

1)将匹配的内容按规定格式进行输出

[root@VM_0_10_centos shellScript]# sed -r 's/(.*)/<\1>/' /tmp/passwd
<>
< n>

匹配签名的内容进行格式输出

[root@VM_0_10_centos shellScript]# echo '' | sed -r 's/(.*)/<\1>/g'
<>

2)使用" & "匹配内容,进行格式输出

[root@VM_0_10_centos shellScript]# sed -r 's/.*/<&>/g' /tmp/passwd
[root@VM_0_10_centos shellScript]# echo '123456' | sed -r 's/(.*)/<\1>/g'
<123456>

[root@VM_0_10_centos shellScript]# sed -r 's/./<&>/g' /tmp/passwd
 <1><2><3><4>

3)命令拼接

# 先新建jpg文件
[root@VM_0_10_centos shellScript]# touch {old01,old02,old03,old04}.jpg
[root@VM_0_10_centos shellScript]# ls *jpg
old01.jpg old02.jpg old03.jpg old04.jpg
[root@VM_0_10_centos shellScript]# ls *.jpg |sed -r 's/(.*)jpg/mv & \1avg/g'
mv old01.jpg old01.avg
mv old02.jpg old02.avg
mv old03.jpg old03.avg
mv old04.jpg old04.avg

ls *.jpg|sed -r 's#(.*)jpg#mv & \1avi#g'|bash

# rename [选项] 你要替换的内容 替换成什么 替换哪些文件
rename -v avi jpg *.jpg

PS:-r 与 -i 同时使用时,-r 参数必须放在前面,如 -ri。不能使用 -ir,这样会先将文件备份为文件.r,然后在替换文件

参考网址;https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html

【文本处理命令】之sed命令详解的更多相关文章

  1. linux sed命令参数及用法详解

    linux sed命令参数及用法详解 http://blog.csdn.net/namecyf/article/details/7336308 1. Sed简介 sed 是一种在线编辑器,它一次处理一 ...

  2. linux命令总结sed命令详解

    Sed 简介 sed 是一种新型的,非交互式的编辑器.它能执行与编辑器 vi 和 ex 相同的编辑任务.sed 编辑器没有提供交互式使用方式,使用者只能在命令行输入编辑命令.指定文件名,然后在屏幕上查 ...

  3. linux管道命令grep命令参数及用法详解---附使用案例|grep

    功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...

  4. 【OS_Linux】三大文本处理工具之sed命令

    1.sed命令的简介及用法 sed:即为流编辑器,“stream editor”的缩写.他先将源文件读取到临时缓存区(也叫模式空间)中,再对满足匹配条件的各行执行sed命令.sed命令只针对缓存区中的 ...

  5. 文本处理三剑客之sed命令

    第十八章.文本处理三剑客之sed命令 目录 sed介绍 sed命令常用选项 sed常用编辑命令 sed使用示例 sed高级语法 18.1.sed简介 sed全名stream editor,流编辑器,s ...

  6. linux mount命令参数及用法详解

    linux mount命令参数及用法详解 非原创,主要来自 http://www.360doc.com/content/13/0608/14/12600778_291501907.shtml. htt ...

  7. 【转】linux expr命令参数及用法详解

    在抓包过程中,查看某个设定时间内,数据上下行多少,用命令expr 计算! --------------------------------------------------------------- ...

  8. linux useradd(adduser)命令参数及用法详解(linux创建新用户命令)

    linux useradd(adduser)命令参数及用法详解(linux创建新用户命令) useradd可用来建立用户帐号.帐号建好之后,再用passwd设定帐号的密码.而可用userdel删除帐号 ...

  9. linux dmesg命令参数及用法详解(linux显示开机信息命令)

    linux dmesg命令参数及用法详解(linux显示开机信息命令) http://blog.csdn.net/zhongyhc/article/details/8909905 功能说明:显示开机信 ...

  10. Linux Bash命令关于程序调试详解

    转载:http://os.51cto.com/art/201006/207230.htm 参考:<Linux shell 脚本攻略>Page22-23 Linux bash程序在程序员的使 ...

随机推荐

  1. Python的filter方法实现筛选功能

    filter方法可以实现筛选,第一个参数是一个函数,返回值是True或者False,第二个参数可以是str.tuple.list,将后面的参数依次传递给函数,依次判断结果,留下结果为 True的.比如 ...

  2. post请求四种传送正文的方式

    一.简介 HTTP协议规定post提交的数据必须放在消息主体(entity-body)中,但协议没有规定数据必须使用什么编码方式.HTTP协议是以ASCII码传输,建立再TCP/IP协议之上的应用层规 ...

  3. doc 如何在指定的位置打印字符和颜色

    编程:在屏幕中间分别显示绿色,绿底红色,白底蓝色的字符串weclome to masm! B8000H~BFFFFH共32KB 的空间,为80*25彩色字符模式的显示缓冲区. 在80*25彩色字符模式 ...

  4. 一、I/O模型之BIO

    I/O模型之BIO 基本介绍 Java BIO 就是传统的 Java IO 编程,其相关的类和接口再 java.io 包下 BIO(blocking I/O):同步阻塞,服务器实现模式为一个连接一个线 ...

  5. Vue 从入门到进阶之路(十一)

    之前的文章我们说了一下 vue 中组件的原生事件绑定,本章我们来所以下 vue 中的插槽使用. <!DOCTYPE html> <html lang="en"&g ...

  6. IO流与装饰者模式

    java使用IO流来处理不同设备之间数据的交互;所有的IO操作实际上都是对 Stream 的操作 从功能上划分: 输入流: 当数据从源进入的编写的程序时,称它为输入流; 输出流: 从程序输出回另一个源 ...

  7. Sqlite-net 修改版 支持中文和CodeFirst技术

    最近, 做的一个windows 桌面WPF程序, 需要数据库支持.尝试了 sql server 的开发版,使用EF , 效率太低.后来采用sqlite数据库,中间踩坑无数.但最终完美的解决了这些问题. ...

  8. 数据显示按规格向datatable中增加空白记录

    /// <summary> /// 按前台分页样式为datatable增加空行 /// </summary> /// <param name="gridPage ...

  9. vue中使用props传递参数

    通常,父组件的模板中包含子组件,父组件要正向地向子组件传递数据或参数,子组件收到后根据参数的不同来渲染不同的内容,或者执行操作. 这个正向传递数据的过程是通过props来实现的. 在组件中,子组件使用 ...

  10. 持续集成与Devops关系

    什么是持续集成 持续集成(Continuous Integration,简称CI),是一种软件开发实践,在实践中指只要代码有变更,就自动运行构建和测试,反馈运行结果.通俗一点来讲,就是绑定项目的代码仓 ...