Linux sed 命令

Linux sed 命令

Linux sed 命令是利用脚本来处理文本文件

sed 可以找脚本的指令来处理,编辑文本文件

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

# 语法
sed [-hnV][-e<script>][-f<script文件>][文本文件]

# 参数说明
 -e<script> 或 --expression=<script> 以选项中指定的script来处理输入的文本文件
 -f<script> 或 --file=<script文件> 以选项中指定的script文件来处理输入的文本文件
 -h 或 --help 显示帮助
 -n 或 --quiet 或 --silent 仅显示script处理后的结果
 -V 或 --version 显示版本信息
    
# 动作说明
a : 新增, a的后面可以接字符串,这些字符串 将出现在新一行(当前行的下一行)
c : 取代, c的后面可以接字符串,这些字符串可以取代 n1,n2之间的行
d : 删除, 因为是删除,d后面通常不跟任何内容
i : 插入, i的后面可以接字符串 , 这些字符串 将出现在新一行(当前行的上一行)
p : 打印, 选择某个数据印出
s : 取代, 可以直接进行取代工作. 通常s懂可以搭配正则表达式,如 : 20s/old/new/g 
        
        
#### 务必以 '' 两个单引号括住

a

# 追加行

$ sed -e 4a
ewline testfile #使用sed 在第四行后添加新字符串  

HELLO LINUX! #testfile文件原有的内容  
Linux is a free unix-type opterating system.  
This is a linux testfile!  
Linux test  
newline 


以行为单位的新增/删除

$ nl testfile 
     1	HELLO LINUX!  
     2	Linux is a free unix-type opterating system.  
     3	This is a lin

# d 删除指定的行号
$ nl testfile  | sed '2,3d'
     1	HELLO LINUX!  
     4	Linux test 

# 删除 第二行
$ nl testfile | sed '2d' 

# 删除第三行到最后一行
$ nl testfile | sed '3,$d' 



#  a 追加内容第二行后 追加内容
$ nl testfile |sed '2a drink tea'
     1	HELLO LINUX!  
     2	Linux is a free unix-type opterating system.  
drink tea
     3	This is a linux testfile!  
     4	Linux test 
     
     
# i 插入内容第二行前
$ nl testfile |sed '2i drink tea'
     1	HELLO LINUX!  
drink tea
     2	Linux is a free unix-type opterating system.  
     3	This is a linux testfile!  
     4	Linux test 

# 增加两行以上的内容
$ nl testfile | sed '2a Drink tea or ......
> drink beer ?'

     1	HELLO LINUX!  
     2	Linux is a free unix-type opterating system.  
Drink tea or ......
> drink beer ?
     3	This is a linux testfile!  
     4	Linux test 

以行为单位的替换与显示

#  c 整行取代  2-3行替换成 : no 2-3 numbe
$ nl testfile |sed '2,3c no 2-3 number'
     1	HELLO LINUX!  
no 2-3 number
     4	Linux test 

# 仅列出 指定行号的内容
$ nl testfile |sed -n '2,3p'
     2	Linux is a free unix-type opterating system.  
     3	This is a linux testfile! 


数据的搜寻并显示

# 搜索 有 test关键字的行,重复出现
# p 打印
$ nl testfile | sed '/test/p'
     1	HELLO LINUX!  
     2	Linux is a free unix-type opterating system.  
     3	This is a linux testfile!  
     3	This is a linux testfile!  
     4	Linux test 
     4	Linux test 

# -n 只展示结果
$ nl testfile | sed -n '/test/p'
    3	This is a linux testfile!  
    4	Linux test 

数据的搜寻并删除

# d 删除 符合条件的行
$ nl testfile | sed  '/test/d'
     1	HELLO LINUX!  
     2	Linux is a free unix-type opterating system.  

数据的搜寻并执行命令

# 执行后面花括号中的一组命令,每个命令之间用分号分隔,这里把bash替换为blueshell,再输出这行

$ nl testfile |sed -n '/test/{s/linux/Centos/;p;q}'
     3	This is a Centos testfile!  

数据的搜寻并替换

# sed 's/要被取代的字串/新的字串/g'
     
#   利用 /sbin/ifconfig 查询 IP , 将 IP 前面的部分予以删除
 $ /sbin/ifconfig  ens33 |grep 'inet'|sed 's/^.*://g'
 inet 192.168.236.128  netmask 255.255.255.0  broadcast 192.168.236.255

# 删除后续的部分
 $ /sbin/ifconfig  ens33 |grep 'inet'|sed 's/^.*://g' | sed 's/netmask.*$//g'
        inet 192.168.236.128  

多点编辑

$ nl testfile  |sed -e '3,$d' 
     1	HELLO LINUX!  
     2	Linux is a free unix-type opterating system.  
     
$  nl testfile  |sed -e '3,$d' -e 's/free/fzzz/' 
     1	HELLO LINUX!  
     2	Linux is a fzzz unix-type opterating system.  
     
# -e表示多点编辑,第一个编辑命令删除testfile第三行到末尾的数据,第二条命令搜索free替换为fzzz。

直接修改文件内容(危险动作)

# 利用 sed 将 regular_express.txt 内每一行结尾若为 . 则换成 !
$ sed -i 's/.$/!/g' regular_express.txt

#  regular_express.txt 最后一行加入 # This is a test:
$ sed -i '$a # This is a test' regular_express.txt

https://www.runoob.com/linux/linux-comm-sed.html

原文地址:https://www.cnblogs.com/dengz/p/14892408.html