Shell学习笔记 三剑客-sed

21 篇文章 0 订阅
10 篇文章 0 订阅

sed是一种在线的、非交互式的编辑器,它一次处理一行内容。处理时候,把当前处理的行存储在临时缓冲区中,称为模式空间“pattern space”。

接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕,接着处理下一行。

sed主要用于自动编辑一个或者多个文件,简化对文件的反复操作,编写转换程序等

sed的命令格式

sed 【options】 command file(s)

sed和grep 不一样 是否找到指定的模式,退出的状态都是0

只有当输入命令错误的时候退出状态才是非0

sed与grep一样,sed在文件查找模式时也可以使用正则表达式(RE)和各种元字符。

正则表达式是括在斜杠间的模式,用于查找和替换,以下是sed支持的元字符。

使用基本元字符集 ^ , $ , . , *  ,  [] , [^] , \< \>  , \(\) , \{ \ }

使用拓展元字符集 ? , + , { } , | , ()

使用拓展元字符集的方式  \+ sed -r

sed命令

选项

sed -e 允许多项文件编辑

sed -n 取消默认输出

sed -i 直接修改对应文件

sed -r 支持拓展元字符

命令

a 在当前行后添加一行或者多行

c 用新文本修改(替换)当前行中文本

d 删除行

i 在当前行之前插入文本

l 列出非打印字符

p 打印行

n 读出下一输出行,并从下一条命令而不是第一条命令开始对其的处理

q 结束或退出sed

! 对所选行以外的所有行应用命令

sed的基本用法

sed -r '' /etc/passwd
sed -r 'p' /etc/passwd
sed -r -n 'p' /etc/passwd
sed -r -n '/root/p' /etc/passwd
sed -r 's/root/alice/' /etc/passwd
sed -r 's/root/alice/g' /etc/passwd
sed -r 's/root/alice/gi' /etc/passwd
sed -r 's/root/d' /etc/passwd
sed -r '\crootcd' /etc/passwd

打印命令 p

[root@servicex ~]# sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@servicex ~]# sed -n '3p' /etc/passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@servicex ~]# sed '' /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
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
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
sarah:x:1002:1002::/home/sarah:/sbin/nologin
natasha:x:1003:1003::/home/natasha:/bin/bash
harry:x:1004:1004::/home/harry:/bin/bash
test:x:1005:1005::/home/test:/bin/bash
alex:x:1234:1234::/home/alex:/bin/bash
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
lalin:x:1235:1235::/home/lalin:/bin/bash

追加命令 a

sed -i '行a 字符串' 文件位置
注意:\t需要转义 \n 不需要转义

 修改命令 c

//匹配模式替换
sed -i '/^SELINUX=/c SELINUX=Disable' /etc/selinux/config
//指定行号替换
sed -i '7c SELINUX=Disable' /etc/selinux/config

删除命令 d

//指定删除第三行但是不会改变文件内容,仅输出
sed '3d' /etc/passwd
//下面两种结果一样
sed '3{d;}' /etc/passwd
sed '3{d}' /etc/passwd
//从第三行到最后删除
sed '3,$d' /etc/passwd
//删除最后一行
sed '$d' /etc/passwd
//删除所有行
sed '1,$d' /etc/passwd
//模式匹配删除
sed /mail/d /etc/passwd

插入命令 i

在指定行前一行插入
sed '行号i\字符串' /etc/passwd

写文件命令 w

sed -n '/root/w newfile' /etc/passwd

替换命令 s

//替换每行出现的第一个root为test
sed -r 's/root/test/' file
//替换所有的root为test
sed -r 's/root/test/g' file
//替换两个0-9结尾的行将在匹配到的后面加上.5
sed -r 's/[0-9][0-9]$/$.5' file
//替换每行出现的第一个root为test,忽略大小写
sed -r 's/root/test/i' file
//替换所有的root为test,忽略大小写
sed -r 's/root/test/gi' file

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值