当前位置:网站首页>sed命令
sed命令
2022-07-24 05:23:00 【笑 瘾】
sed命令
一、sed的定义
1、sed:是一个文本字符串流进行过滤和转换(替换和删除)的编辑器
2、sed的使用
[[email protected] lianxi]# cat fan.txt
wo ai chi fan ming you
fan ming you fan ming you
[[email protected] lianxi]# sed -i '/^fan/ s/fan/liu/g' fan.txt
[[email protected] lianxi]# cat fan.txt
wo ai chi fan ming you
liu ming you liu ming you
[[email protected] lianxi]# sed -i 's/you//g' fan.txt
[[email protected] lianxi]# cat fan.txt
wo ai chi fan ming
liu ming liu ming
二、sed里的两种空间
1、pattern space:加工车间
2、hold space:临时仓库

三、sed的语法命令格式

1、三种命令格式:
1、sed [选项] sed编辑命令 输入文件
2、shell 命令 | sed [选项] sed编辑命令
3、sed [选项] -f sed脚本文件 输入文件
四、sed的常用选项

1、-n 只显示匹配处理的行(否则会输出所有)
#输出第8行
[[email protected] 76]# sed -n '8p' /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
#输出第一行和最后一行
[[email protected] 76]# sed -n '1p;$p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
mengmeng2:x:1014:1014::/home/mengmeng2:/bin/bash
2、-i 直接在文件中进行修改,而不是输出到屏幕
[[email protected] 76]# cat fan.txt
wo ai chi fan ming you
fan ming you fan ming you
[[email protected] 76]# sed -i '/^fan/ s/fan/liu/g' fan.txt
[[email protected] 76]# cat fan.txt
wo ai chi fan ming you
liu ming you liu ming you
[[email protected] 76]#
[[email protected] 76]# sed -i 's/you//g' fan.txt
[[email protected] 76]# cat fan.txt
wo ai chi fan ming
liu ming liu ming
3、-r 支持扩展正则----》(+ | {} )
五、sed里常用的编辑命令

1、p命令的练习:
#不连续的输出1,3,5行的内容,;表示命令连接符
[[email protected] lianxi]# sed -n '1p;3p;5p;' passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
#从第1行到第5行的内容
[[email protected] lianxi]# sed -n '1,5p' 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
#步长是2,每次增加2
[[email protected] lianxi]# cat -n /etc/passwd|sed -n '1~2p'
1 root:x:0:0:root:/root:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
11 games:x:12:100:games:/usr/games:/sbin/nologin
13 nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
15 systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
…………
[[email protected] lianxi]# cat -n /etc/passwd|sed -n '2~2p'
2 bin:x:1:1:bin:/bin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
8 halt:x:7:0:halt:/sbin:/sbin/halt
10 operator:x:11:0:operator:/root:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
14 dbus:x:81:81:System message bus:/:/sbin/nologin
16 systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
18 polkitd:x:998:996:User for polkitd:/:/sbin/nologin
20 sssd:x:996:993:User for sssd:/:/sbin/nologin
22 liuhongjie:x:1001:1001::/home/liuhongjie:/bin/bash
24 caohx:x:1004:1004::/home/caohx:/bin/bash
2、shell的变量在sed里面的使用,用双引号引起来
[[email protected] lianxi]# num1=5
[[email protected] lianxi]# num2=15
[[email protected] lianxi]# sed -n "${num1}p;${num2}p" /etc/passwd
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
3、a和i操作
在行首和行尾添加内容,使用替换操作,使用^和$元字符
4、s操作
[[email protected] 76]# cat fan.txt
wo ai chi fan ming you
fan ming you fan ming you
[[email protected] 76]# sed -i '/^fan/ s/fan/liu/g' fan.txt
[[email protected] 76]# cat fan.txt
wo ai chi fan ming you
liu ming you liu ming you
[[email protected] 76]#
[[email protected] 76]# sed -i 's/you//g' fan.txt
[[email protected] 76]# cat fan.txt
wo ai chi fan ming
liu ming liu ming
5、模式匹配
[[email protected] lianxi]# cat cat.txt
i have a "fat" big "cat" 4376
i have a "fat" big "cat" 7887
i have a "fat" big "cat" 456
i have a "fat" big "cat" 123
i have a "fat" big "cat" 789
[[email protected] lianxi]# sed -i -r 's/\<[0-9]{3}\>/&0/g' cat.txt
[[email protected] lianxi]# cat cat.txt
i have a "fat" big "cat" 4376
i have a "fat" big "cat" 7887
i have a "fat" big "cat" 4560
i have a "fat" big "cat" 1230
i have a "fat" big "cat" 7890
6、sed里面的分组标签
[[email protected] 76]# echo aaa bbb 1234 |sed -r 's/([a-z]+) ([a-z]+) ([0-9]+)/\3 \2 \1/'
1234 bbb aaa
[[email protected] 76]#
[[email protected] 76]# echo aaa bbb 1234 |awk '{print $3,$2,$1}'
1234 bbb aaa
六、练习
练习1、
复制/etc/hosts文件到当前目录下,然后进行操作
1.在每行前面加一个字符串sanchuang
2.自己编辑一个文件test.txt,内容如下:
0.0.0.0
1.1.1.1
2.2.2.2
输出以下形式:
0.0.0.0:80,1.1.1.1:80,2.2.2.2:80
1、[[email protected] lianxi]# cat test.txt |sed -n 'N;N;s/\n/:80,/g;s/$/:80/p'
0.0.0.0:80,1.1.1.1:80,2.2.2.2:80
练习2、
1.sed取出/etc/passwd文件的第一列
sed -n '1p' /etc/passwd
2.sed将PATH环境变量中的冒号换成换行 ->可以将PATH变量的内容重定向到一个文件里,例如path.txt
sed -i 's/:/\n/g' path.txt
3.sed将PATH环境变量斜杠/换成斜杠\
sed -i 's/\//\\/g' path.txt
4.sed修改SELINUX配置文件从开启(enforcing)变成禁用(disabled)
/etc/sysconfig/selinux
sed -i '/^SELINUX=/ s/enforcing/disabled/'
5.去掉/etc/passwd文件中第二个字段的x
sed -i 's/x//g' passwd
6.将/etc/sysconfig/network-scripts/ifcfg-ens33里的ONBOOT=no修改为yes
sed -i '/^ONBOOT/ s/yes/no/' ifcfg-ens33
7.只显示ip add的ip地址
ip add|sed -nr '/ens33$/ s#([a-z ]+) (([0-9]+\.){3}[0-9]+/[0-9]{2}) (.*)#\2#p'
8.复制/etc/ssh/sshd_config到当前目录下,修改里面的端口号修改为8899,将#Port 22 配置修改为Port 8899 要求去掉前面的#号,将22修改为8899
sed -i '/^#Port/ c Port 8899' sshd_config
9.给下列含有大写字母的行,在大写字母后追加数字2022 --》sed
abcdSdddde
islHishbxld
goBkefji
daanshXxge
不要直接对上面的源文件进行操作,建议复制到当前文件夹里进行
sed -ir 's/[A-Z]/&2022/g' test.txt
答案:
1、[[email protected] lianxi]# sed -n '1p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
2、[[email protected] lianxi]# cat path.txt
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root
[[email protected] lianxi]# sed -i 's/:/\n/g' path.txt
[[email protected] lianxi]# cat path.txt
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root
3、[[email protected] lianxi]# cat path.txt
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root
[[email protected] lianxi]# sed -i 's/\//\\/g' path.txt
[[email protected] lianxi]# cat path.txt
\usr\local\sbin:\usr\local\bin:\usr\sbin:\usr\bin:\root
4、[[email protected] lianxi]# sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config
[[email protected] lianxi]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
5、sed -i 's/x//g' passwd
6、[[email protected] lianxi]# sed -i '/^ONBOOT/ s/yes/no/' ifcfg-ens33
[[email protected] lianxi]# cat ifcfg-ens33
BOOTPROTO=none #设置网卡静态配置ip地址,none静态配置(手工指向ip)DHCP表示虚拟机动态获得ip地址
NAME=ens33 #网卡的设备名为ens33
DEVICE=ens33 #网卡的设备名为ens33
ONBOOT=no #开机自启动网卡--》自动激活网卡
7、[[email protected] lianxi]# ip add|sed -nr '/ens33$/ s#([a-z ]+) (([0-9]+\.){
3}[0-9]+/[0-9]{
2}) (.*)#\2#p'
192.168.243.128/24
8、sed -i '/^#Port/ c Port 8899' sshd_config
9、[[email protected] lianxi]# cat test.txt
abcdSdddde
islHishbxld
goBkefji
daanshXxge
[[email protected] lianxi]# sed -ir 's/[A-Z]/&2022/g' test.txt
[[email protected] lianxi]# cat test.txt
abcdS2022dddde
islH2022ishbxld
goB2022kefji
daanshX2022xge
边栏推荐
- UE4 reload system 1. basic principle of reload system
- ip作业(1)
- IP笔记(12)
- Remote connection to Qunhui NAS at home [no public IP, free intranet penetration]
- IP notes (9)
- [no need for public IP] configure a fixed public TCP port address for remote desktop raspberry pie
- How to build a website full of ritual sense and publish it on the public website 2-2
- Solutions to the failure of wechat TBS online kernel installation
- MeterSphere一站式开源持续测试平台
- Top 10 vulnerability assessment and penetration testing tools
猜你喜欢

进程和计划任务管理

Unity (II) more APIs and physical engines

leetcode剑指offer JZ73 翻转单词序列
![Quickly and simply set up FTP server, and achieve public network access through intranet [no need for public IP]](/img/2a/43ba2839b842e0901a550d2883b883.png)
Quickly and simply set up FTP server, and achieve public network access through intranet [no need for public IP]

Leetcode sword finger offer jz23: the entry node of the link in the linked list

Process and planned task management

Sorting of common AR and MR head mounted display devices

Using keras and LSTM to realize time series prediction of long-term trend memory -lstnet
![[222] memory overflow and location](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[222] memory overflow and location

ip作业(1)
随机推荐
Mysql database - SQL summary (remember to pay attention to me! Come on in China!)
leetcode剑指offer JZ42 连续子数组的最大和
leetcode剑指offer jz5 替换空格字符串
jz47 礼物的最大价值(动态规划思路)
Ia note 1
IP课笔记(4)
Hololens 2 development 101: create the first hololens 2 Application
IP notes (7)
The public network uses Microsoft Remote Desktop remote desktop to work remotely at any time
mysql 忘记退出直接关闭窗口现在要删除整个文件夹如何删除
常用工作方法总结(7S、SWOT分析、PDCA循环、SMART原则、6W2H、时间管理、WBS、二八原则)
TensorFlow-GPU 安装 -- 056
IP课(OSPF)综合实验
LuckyFrameWeb测试平台(一款支持接口自动化、WEB UI自动化、APP自动化,并且支持分布式测试的全纬度免费开源测试平台)
Summary of common working methods (7S, SWOT analysis, PDCA cycle, smart principle, 6w2h, time management, WBS, 28 principles)
Simple but easy to use: using keras 2 to realize multi-dimensional time series prediction based on LSTM
IP笔记(12)
进程和计划任务管理
Leetcode refers to offer jz5 to replace the space string
Flink time stream processing