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. linux 安装redis服务

    下载地址:http://redis.io/download,下载最新稳定版本. 本教程使用的最新文档版本为 2.8.17,下载并安装: $ wget http://download.redis.io/ ...

  2. V4 Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 2471245.1)

    V4 Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 2471245. ...

  3. Redhat Linx使用Centos yum源

    一.故障现象: 在安装了Read linux后,使用yum安装软件,出现如下提示:[root@localhost~]# yum install xxxLoaded plugins: product-i ...

  4. Cocos2d-x项目编译为Android应用——命令行方式

    配置: 相关工具:Cocos2d-x 3.2 + Ant 1.9.4 + Android NDK r9d + Android SDK 运行平台:OS X 10.9.4+ Xcode 6 前言:笔者使用 ...

  5. Bandizip解压压缩软件

    上次重装系统以后,特别注意安装软件,在这里提一些比较好的 解压软件:Bandizip 这个是一个免费轻量级的软件,压缩解压速度快,没什么广告,推荐大家在官方下载 Bandizip下载网址 点击下载安装 ...

  6. 安装swoole出现make报错的原因与解决方法

    安装swoole报错,错误信息如下: 报错原因 报错很明显,找不到 openssl/ssl.h ,首先要确认电脑上有没有安装 openssl Mac安装openssl 既然它找不到,那么就需要手动的指 ...

  7. python之字符串的拼接总结

    加号连接 1.通过+号连接起来 逗号连接 2.通过都好连接起来 但是,这里值得注意的是,只能用于print打印,赋值组操作会生成元组 直接连接 3.直接连接中间有无空格均可 %连接 在python2. ...

  8. 关于 Comparable 的使用

    作为一名刚上路的超初级程序员,今天终于可以迈开自己的第一步,写一篇博客.把我自己都感动哭了. 今天看面试题时看到了一个Comparable 的使用,才发现自己好像并没有使用过这个接口,具体这个接口是怎 ...

  9. (六十三)c#Winform自定义控件-箭头(工业)-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  10. C#中类的修饰符

    Q&A  项目=程序集=assembly 1,Q:类的修饰符有哪些? A:   有 new.public.protect.internal.private.abstract.sealed.st ...