Linux三剑客-Sed命令

时间:2022-06-24 01:16:50


Linux三剑客

目前许多公司都要求掌握一些必要的Linux命令。而目前大多数的公司服务器都是适用的Linux系统,这就要求我们必须要学习一些必要的Linux命令,前面已经学习了Linux的基本命令,这篇主要研究的是传说中的Linux三剑客(grep、sed、awk)

今天这篇介绍一下sed,sed类似于我们windows的编辑器。sed这个工具要想玩通,需要时间的和大量的练习,这里仅仅是根据我工作的需求列出一些常用的操作。方便日常运维和编写shell脚本方便操作


提示:有兴趣的可以深入了解下正则表达式

文章目录

  • Linux三剑客
  • 前言
  • 什么是 sed
  • sed处理数据流程
  • sed命令
  • 常用命令选项options:
  • 常用内部命令command
  • 常用模式空间和保持空间
  • sed添加操作
  • sed删除操作
  • sed替换操作
  • sed打印操作


前言

提示:这里可以添加本文要记录的大概内容:

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

什么是 sed

sed是linux中提供的一个外部命令,它是一个行(流)编辑器,非交互式的对文件内容进行增删改查的操作,使用者只能在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。它和文本编辑器有本质的区别。
也就是前者一次处理一个文本,而后者是一次处理一个文本中的一行。这个是我们应该弄清楚且必须牢记的,否者可能无法理解sed的运行原理和使用精髓。

sed处理数据流程

Linux三剑客-Sed命令

sed命令

语法:

sed [options] ‘{command}[flags]’ [filename]    
# 中括号内容必有 大括号内容可有可无
sed  # 执行命令
[options]  # 命令选项
{command}[flags]    # sed内部选项和参数
[filename]     # 文件

常用命令选项options:

-e script 将脚本中指定的命令添加到处理输入时执行的命令中  多条件,一行中要有多个操作
-f script 将文件中指定的命令添加到处理输入时执行的命令中
-n        抑制自动输出
-i        编辑文件内容,修改源文件
-i.bak    修改时同时创建.bak备份文件。
-r        使用扩展的正则表达式
!         取反 (跟在模式条件后与shell有所区别)

常用内部命令command

a   在匹配后面添加
i   在匹配前面添加
p   打印
d   删除
s   查找替换
c   更改
y   转换   N D P 
n   读入下一行输入,并从下一条而不是第一条命令对其处理

flags
数字             表示新文本替换的模式
g:             表示用新文本替换现有文本的全部实例
p:             表示打印原始的内容
w filename:     将替换的结果写入文件

常用模式空间和保持空间

  • h:把模式空间(pattern space)的内容复制到保持空间
  • H:把模式空间的内容添加到保持空间
  • g:取出保持空间的内容,将其复制到模式缓冲区
  • G:取出保持空间的内容,

Linux三剑客-Sed命令

sed添加操作

  • a:在文本后添加一条记录
  • i:在文本插入一条记录
[root@localhost sedTest]# cat demo 
1abcdef first
2adbche test
3adbcef demo
4abcdef
5abcdef
# 在第三行插入after3
[root@localhost sedTest]# sed '3a\after3' demo 
1abcdef first
2adbche test
3adbcef demo
after3
4abcdef
5abcdef
# 每一行后都插入after this line
[root@localhost sedTest]# sed 'a\after this line' demo 
1abcdef first
after this line
2adbche test
after this line
3adbcef demo
after this line
4abcdef
after this line
5abcdef
after this line
# 匹配有ad的行在其后面插入 after this line
[root@localhost sedTest]# sed '/ad/a\after this line' demo 
1abcdef first
2adbche test
after this line
3adbcef demo
after this line
4abcdef
5abcdef
# 匹配所有以2ad开头的行,在其后面加上after this line
[root@localhost sedTest]# sed '/^2ad/a\after this line' demo 
1abcdef first
2adbche test
after this line
3adbcef demo
4abcdef
5abcdef
# 在demo文件里第二行前加入before2
[root@localhost sedTest]# sed '2i\before2' demo 
1abcdef first
before2
2adbche test
3adbcef demo
4abcdef
5abcdef
# 不指定行号 则全局每一行前都会插入
[root@localhost sedTest]# sed 'i\before this line' demo 
before this line
1abcdef first
before this line
2adbche test
before this line
3adbcef demo
before this line
4abcdef
before this line
5abcdef

sed删除操作

# 删除第3行
[root@localhost sedTest]# sed '2d' demo 
1abcdef first
3adbcef demo
4abcdef
5abcdef
# 删除含ad的行
[root@localhost sedTest]# sed '/ad/d' demo 
1abcdef first
4abcdef
5abcdef
# 删除所有行
[root@localhost sedTest]# sed 'd' demo 
[root@localhost sedTest]# 
# 删除原文件中的内容
[root@localhost sedTest]# cat demo 
1abcdef first
2adbche test
3adbcef demo
4abcdef
5abcdef
[root@localhost sedTest]# sed -i 'd' demo
[root@localhost sedTest]# cat demo
[root@localhost sedTest]#

sed替换操作

  • c:change the line:即替换一整行的操作 。格式:sed ‘s\要替换的内容’ file
  • s:格式:sed ‘s\被替换的内容\要替换的内容’ file
# c 替换第二行的内容为 change the new line
[root@localhost sedTest]# sed '2c\change the new line' demo 
1abcdef first
change the new line
3adbcef demo
4abcdef
5abcdef
# s 匹配所有ad,替换为ab
[root@localhost sedTest]# sed 's\ad\ab\' demo 
1abcdef first
2abbche test
3abbcef demo
4abcdef
5abcdef

sed打印操作

[root@localhost sedTest]# sed 'p' demo 
1abcdef first
1abcdef first
2adbche test
2adbche test
3adbcef demo
3adbcef demo
4abcdef
4abcdef
5abcdef
5abcdef
# 为什么会打印出重复的一行呢?
P命令即是print pattern space(打印模式空间),它是一块活动缓冲区,在sed编辑器执行命令时,
它会保存sed编辑器要检验的文本。简言之sed 'p' 就是会一行一行的往一个暂存区添加,在一行一行的输出
# 打印2-4行的内容
[root@localhost sedTest]# sed -n '2,4p' demo 
2adbche test
3adbcef demo
4abcdef
# 打印指定行的下一行 n 多个命令同时使用需要使用到{},中间用分号;分割
[root@localhost sedTest]# sed -n '2{n;p}' demo 
3adbcef demo
# 打印指定行和下一行 N
[root@localhost sedTest]# sed -n '2{N;p}' demo 
2adbche test
3adbcef demo

# 打印奇数行、偶数行
# 打印奇数行
[root@localhost sedTest]# sed -n '{p;n}' demo  含义:打印第一行后跳过下一行在打印
1abcdef first
3adbcef demo
5abcdef
[root@localhost sedTest]# sed -n '{n;p}' demo  含义:跳过第一行在打印下一行
2adbche test
4abcdef
# 打印第二行以后在打印两行
[root@localhost sedTest]# sed -n '2,+2p' demo 
2adbche test
3adbcef demo
4abcdef