文本三剑客sed高级用法和企业级实战| 学习笔记

简介: 快速学习文本三剑客sed高级用法和企业级实战

开发者学堂课程【Linux企业运维实战 - 入门及常用命令:文本三剑客sed高级用法和企业级实战】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/550/detail/7620


文本三剑客sed高级用法和企业级实战

 

内容介绍:

一、sed 工具

二、sed 示例

三、高级编辑命令

 

一、sed工具

1、用法:        

sed[option]...'script' inputfile…

2、常用选项:

-n:不输出模式空间内容到屏幕,即不自动打印

-e:多点编辑      

-f:/PATH/SCRIPT_FILE:从指定文件中读取编辑脚本

-r:支持使用扩展正则表达式

-i.bak:备份文件并原处编辑

3、script:

‘地址命令’

4、地址定界:

(1)不给地址:对全文进行处理

(2)单地址:      

#:指定的行,$:最后一行      

/pattern/:被此处模式所能够匹配到的每一行

(3)地址范围:      

#,#      

#,+#          

/pat1/,/pat2/          

#./pat1/

4)~:步进      

1~2奇数行      

2~2偶数行

5、编辑命令:

d:删除模式空间匹配的行,并立即启用下一轮循环

p:打印当前模式空间内容,追加到默认输出之后

a  [\]text:在指定行后面追加文本              

支持使用\n实现多行追加

i  [\]text:在行前面插入文本

c  [\]text:替换行为单行或多行文本

w  /path/somefile:保存模式匹配的行至指定文件

r  /path/somefile:读取指定文件的文本至模式空间中                    

匹配到的行后

=: 为模式空间中的行打印行号

! :  模式空间中匹配行取反处理

示例:

[root@centos7 ~]#sed ‘2,$a=====’ f1

作用:追加别名命令、模式匹配…

在包含alias后面追加别名定义:

[root@centos7 ~]#alias p=poweroff’ .bashrc

[root@centos7 ~]#cat .bahsrc

# .bashrc

[root@centos7 ~]#Cat .bashrc

并没有真的改文件,只是把选项改变了,为避免文件改错了,一般都会加.bashrc后缀,表示备份

备份文件:

[root@centos7 ~]#sed -I.bak ‘/aliases/alias p=poweroff’ .bashrc

[root@centos7 ~]#Cat .bashrc

加别名:

[root@centos7 ~]#set -i bak2 ‘/aliases/alias cdnet=“cd /etc/sysconfig/network-network-scripts/”’ .bashrc

-a特点:加空格加不进去

[root@centos7 ~]#sed ‘2,5a”  ====‘ f1

加斜线,实现多行追加

[root@centos7 ~]#sed ‘5a\   =====‘ f1

i在前面加,a在后面加

[root@centos7 ~]#sed ‘2,5i\   ====‘ f1相当于用新的代替

c为代替,二行到五行代替

[root@centos7 ~]#sed ‘2,5c\   =====’ f1

用w把二行到五行符合条件的内容存到新文件里

[root@centos7 ~]#sed ‘2,5w  f2’ f1

用r 读入内容放到二行到五行中:

[root@centos7 ~]#sed ‘2,5r /etc/issue’ f1

[root@centos7 ~]#Cat .bashrc

[root@centos7 ~]#Cat > sed.txt

[root@centos7 ~]#Sed ‘/Source/r sed.txt’  .bashrc

[root@centos7 ~]#cat sed.txt

[root@centos7 ~]#Reset.sh

加别名,一条一条加很麻烦,可以将别名放到一个文件里,r读取即可

在source行前插入,r表示在后面追加

[root@centos7 ~]#sed ‘/source/r sed,txt’ .bashrc

6、s ///:查找替换,支持使用其他分隔符,s@@@,s###

7、替换标记:

g:行内全局替换

p:显示替换成功的行

w /PATH/TO/SOMEFILE :将替换成功的行保存至文件中

示例:

搜索root,替换成administer,如果没有写地址,就代表全文件搜索替代,用g实现全文替代

[root@centos7 ~]#sed ‘s/root/administrator/‘ /etc/passwd

写正则表达式、正向引用:

[root@centos7 ~]#sed -r ‘s/(root)/\1er/g’  /etc/passwd^C

[root@centos7 ~]#sed -r ‘s/(root)/admin\1/g’  /etc/passwd

整行加字符串:root替换成rooter,扩展的不用加斜线/

[root@centos7 ~]#ssed -r ‘s/()/admin/\1erg’  /etc/passwd

每个整行的结尾都加字符串:

[root@centos7 ~]#sed -r ‘s/(.*)/\imagedu/‘  /etc/paswd

每个整行的结尾都加字符串:

[root@centos7 ~]#sed -r ‘s/(.*)magedu\1/‘  /etc/passwd

bash 前加变为s 并 bash:

[root@centos7 ~]#sed -r s@/bin/bash$@/sbin/bash@‘  /etc/passwd

Or 分组写

[root@centos7 ~]#sed  -r  ‘$@/(bin/bash)$@/s\1@’  /etc/passwd

在quiet后面加一个字符串表示xyz:

[root@centos7 ~]#sed -r ‘/GRUB_CMDLINE_LINUX/s/(.*)//’ /etc/default/grub

不用正则表达式也可以

[root@centos7 ~]#sed -r ‘/GRUB_CMDLINE_LINUX/s/“$/xyz”/’ /etc/default/grub

在整行结尾插入:

[root@centos7 ~]#sed -r ‘/GRUB_CONLINE_LINUX/$/\1 xyz”/‘  /etc/default/grub

简便方法:

[root@centos7 ~]#sed -r ‘/GRUB_CMDLINE_LINUX/s/“$/ xyz”  /etc/default/grub

用sed命令取ip地址

[root@centos7 ~]#ifconfigs  ens33|   sed -n ‘2p’

[root@centos7 ~]#ifconfig ens33| sed -n ‘2p’ | sad -r ‘$@(.*inept )(.*)( net.*)@\2@‘

浓缩命令:

[root@centos7 ~]#Ifconfig ens33| sed -r ‘2!d;s@(.*inet)(.*)( net.*)’

简单版:

[root@centos7 ~]#ifconfig ens33|sed -n ‘2p’ |sed ‘s/.*inet //’ -e ‘s/ net ask.*//’

整合:

ifconfig ens33| sed -n ‘2p’  |  sed -r  s@.inet(.*)net.*@\1@’

ifconfig ens33| sed -n ‘2p’  |  sed -r  s@.inet (.*)net.*@\1@’

ifconfig ens33| sed -n ‘2p’  |  sed -r  s@.inet (.*)  net.*@\1@’

ifconfig ens33| sed -n ‘2p’  |  sed -r  s@.inet (.*inet )(.*)(  net.*)@\1@’

ifconfig ens33| sed -r ‘2!d:s@(.*inet )(.*)(  net.*)@\2@’

ifconfig ens33| sed -n ‘2p’  |  sed   s/:*inet  //‘  |sed ’ s/ netmask.*//’

ifconfig ens33| sed -n ‘2p’  |  sed  -e  s/:*inet  //‘  -e ’  s/ netmask.*//

示例:

批量删除注释中的符号或者注释:

装载 http 安装包

[root@centos6 ~]#rpm -ivh  /misc/cd/Packages/https-2.2.15-59.e16.centos.x86_64.rpm  

(1)删除注释中的 #

[root@centos6 profile.d]#sed ‘/^#NamevirtualHost/s/#//’  

/etc/https/conf/https.conf

(2)删除全部内容

[root@centos6 profile.d]#sed ‘/^#

/etc/https/conf/https.conf

Or

[root@centos6 profile.d]#sed  -e  ‘/^#

查看文件夹或文件的基名:

[root@centos6 profile.d]#echo “/etc/sysconfig/network/”  |sed -r  

‘(.*/)(.*?)\1/’

/etc/sysconfig/network/

[root@centos6 profile.d]#echo “/etc/sysconfig/network/”  |sed -r  

‘(.*/)(.*?$)\1/’

/etc/sysconfig/network/

[root@centos6 profile.d]#echo “/etc/sysconfig/network/”  |sed -r  

‘(.*/)([^/]+\/?$)\1/’

/etc/sysconfig/

# virtualHost: If you want to maintain multiple domains/ hostnames on your

# machine you can setup virtualhost containers for them. Most contigurations

# use only name-based virtual hosts so the server doesnt need to worry about

# IP addresses. This is indicated by the asterisks in the directives below.

# Please see the documentation at

# < URL: http: / / httpd. apache. org/docs/2.2/vhosts/>

# for further details before you try to setup virtual hosts

# You may use the command line option ' -sto verify your virtual host

# contiguration

# Use name-based virtual hosting

# NamevirtualHost : 80

# NOTE: NamevirtualHost cannot be used without a port specifier

# (e. g. : 80) if mod_ ssl is being used, due to the nature of the

# ssL protocol

# Almost any Apache directive may go into a virtualHost container

# The first virtualHost section is used for requests without a known

# server name

VirtualHOST *:80>

# SERVERADMIN WEBMASTER@DUMMY-HOST.EXAMPLE.COM

# DOCUMENTROOT /www/Docs/Dummy-host.EXAMPLE.COM

# SERVERNAME dummy-hostexamLE.com

# ErrorLog 1ogs/dummy-host.exampLE.COM-ERROR-LOG  


二、sed示例

sed   '2p'  /etc/passwd

sed   -n   ‘2p’  /etc/passwd

sed   -n   ‘1,4p’  /etc/passwd

sed   -n    /root/p’  /etc/passwd

sed   -n    ‘2,/root/p’  /etc/passwd 从2行开始

sed   -n    ‘/^$/=”  file显示空行行号                    

sed   -n  -e  '/^$/p'   -e  ‘/^$/=’   file

sed   '/root/a\superman"/etc/passwd行后

sed   '/root/i\superman" /etc/passwd行前

sed   '/root/c\superman'  /etc/passwd代替行

sed  "/^$/d’  file

sed   ‘1,10d’  file

nl  /etc/passwd l sed  ‘2,5d’

nl  /etc/passwd | sed  '2a tea'

sed 's/test/mytest/g' example

sed  -n 's/root/&superman/p  /etc/passwd单词后

sed  -n 's/root/superman&/p  /etc/passwd单词前

sed  -e 's/dog/cat/"  -e  's/hi/lo/' pets

sed  -i.bak   's/dog/cat/g'  pets


三、高级编辑命令

P:打印模式空间开端至\n内容,并追加到默认输出之前

h:把模式空间中的内容覆盖至保持空间中

H:把模式空间中的内容追加至保持空间中

9:从保持空间取出数据覆盖至模式空间

G:从保持空间取出内容追加至模式空间

x:把模式空间中的内容与保持空间中的内容进行互换

n:读取匹配到的行的下一行覆盖至模式空间

N:读取匹配到的行的下一行追加至模式空间

d:删除模式空间中的行

D:如果模式空间包含换行符,则删除直到第一个换行符的模式空间中的文本,并不会读取新的输入行,而使用合成的模式空间重新启动循环。

如果模式空间不包含换行符,则会像发出d命令那样启动正常的新循环

相关文章
|
安全 数据安全/隐私保护 Windows
解锁安全之门,Windows Server 2019密码修改攻略大揭秘
解锁安全之门,Windows Server 2019密码修改攻略大揭秘
1767 0
|
SQL 消息中间件 关系型数据库
ClickHouse(04)如何搭建ClickHouse集群
ClickHouse集群的搭建和部署和单机的部署是类似的,主要在于配置的不一致,如果需要了解ClickHouse单机的安装设部署,可以看看这篇文章,[ClickHouse(03)ClickHouse怎么安装和部署](https://zhuanlan.zhihu.com/p/532431053)。
2140 1
|
安全 开发者 Windows
Win10/11系统永久暂停更新教程,避免频繁更新 Win 系统出问题,一个小工具就能搞定。
Win10/11系统永久暂停更新教程,避免频繁更新 Win 系统出问题,一个小工具就能搞定。
4906 7
|
12月前
|
存储 人工智能 机器人
【2026必看 AI智能体】零基础Coze平台使用教程
本文介绍了Coze智能体的实战入门与进阶应用,涵盖智能体创建、配置大语言模型(LLM)、使用插件扩展功能、构建知识库(RAG)实现高考志愿填报助手、利用记忆功能开发记账本,以及通过API调用和工作流实现中草药识别与菜谱生成等复杂任务,全面展示其在多场景下的智能化能力。
9401 11
|
存储 人工智能 项目管理
2025年GitHub平台上的十大开源MCP服务器汇总分析
本文深入解析了GitHub上十个代表性MCP(Model Context Protocol)服务器项目,探讨其在连接AI与现实世界中的关键作用。这些服务器实现了AI模型与应用程序、数据库、云存储、项目管理等工具的无缝交互,扩展了AI的应用边界。文中涵盖Airbnb、Supabase、AWS-S3、Kubernetes等领域的MCP实现方案,展示了AI在旅行规划、数据处理、云存储、容器编排等场景中的深度应用。未来,MCP技术将向标准化、安全性及行业定制化方向发展,为AI系统集成提供更强大的支持。
3518 2
2025年GitHub平台上的十大开源MCP服务器汇总分析
|
Linux 网络性能优化 网络安全
Linux(openwrt)下iptables+tc工具实现网络流量限速控制(QoS)
通过以上步骤,您可以在Linux(OpenWrt)系统中使用iptables和tc工具实现网络流量限速控制(QoS)。这种方法灵活且功能强大,可以帮助管理员有效管理网络带宽,确保关键业务的网络性能。希望本文能够为您提供有价值的参考。
4017 28
|
Java Python Spring
spring boot读取resources目录下的python脚本执行
spring boot读取resources目录下的python脚本执行
2537 0
spring boot读取resources目录下的python脚本执行
|
消息中间件 分布式计算 大数据
大数据-128 - Flink 并行度设置 细节详解 全局、作业、算子、Slot
大数据-128 - Flink 并行度设置 细节详解 全局、作业、算子、Slot
1747 0
|
网络协议 Linux iOS开发
《熬夜整理》保姆级系列教程-玩转Wireshark抓包神器教程(2)-Wireshark在Windows系统上安装部署
【2月更文挑战第2天】《熬夜整理》保姆级系列教程-玩转Wireshark抓包神器教程(2)-Wireshark在Windows系统上安装部署
1111 4