shell 常用命令——sed

一、简述

sed行编辑器

stream editor
用来操作纯 ASCII 码的文本
处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),可以指定仅仅处理哪些行
sed处理完成之后把缓冲区的内容送往屏幕,接着处理下一行,这样不断重复, 直到文件末尾
sed符合条件的处理,不符合条件的不予处理,文件内容并没有 改变,除非用重定向存储输出


二、调用 sed 命令有两种形式:

sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)


三,多种操作模式

p 显示   d 删除    a 添加   c 替换   w 写入    i 插入

1,p 模式操作

-n 只列出结果sed特殊处理的那一行,不显示原来那一行

-e 多重编辑

sed -n '/^#/p' fstab     表示显示以#开头的行

sed -n '/^#/!p' fstab     表示以#开头的行不显示

sed -n '/0$/!p' fstab      表示以0结尾的行不显示

sed -n '/0$/p' fstab       表示显示以0结尾结尾的行

[root@desktop mnt]# cat -n fstab
     1	
     2	#
     3	# /etc/fstab
     4	# Created by anaconda on Wed May  7 01:22:57 2014
     5	#
     6	# Accessible filesystems, by reference, are maintained under '/dev/disk'
     7	# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8	#
     9	UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
    10	/dev/vg0/vo	/home	ext4	defaults	0 0
[root@desktop mnt]# cat -n fstab | sed -n '4,7p'
     4	# Created by anaconda on Wed May  7 01:22:57 2014
     5	#
     6	# Accessible filesystems, by reference, are maintained under '/dev/disk'
     7	# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

显示第二行和第六行

[root@desktop mnt]# cat -n fstab | sed -n '2p;6p'  
     2	#
     6	# Accessible filesystems, by reference, are maintained under '/dev/disk'

sed -n -e '2!p'  fstab    表示第二行不显示

sed -n -e '2,5!p'  fstab  表示第2到5行不显示

[root@desktop mnt]# cat -n fstab | sed -n -e '2!p' 
     1	
     3	# /etc/fstab
     4	# Created by anaconda on Wed May  7 01:22:57 2014
     5	#
     6	# Accessible filesystems, by reference, are maintained under '/dev/disk'
     7	# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8	#
     9	UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
    10	/dev/vg0/vo	/home	ext4	defaults	0 0
[root@desktop mnt]# cat -n fstab | sed -n -e '2,5!p'
     1	
     6	# Accessible filesystems, by reference, are maintained under '/dev/disk'
     7	# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8	#
     9	UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
    10	/dev/vg0/vo	/home	ext4	defaults	0 0

练习

给定userfile和passfile,批量创建用户

[root@desktop mnt]# vim userfile
[root@desktop mnt]# cat userfile 
user1
user2
user3

[root@desktop mnt]# vim passwdfile
[root@desktop mnt]# cat passwdfile 
123
123
123
[root@desktop mnt]# vim user.sh
[root@desktop mnt]# cat user
cat: user: No such file or directory
[root@desktop mnt]# cat user.sh 
#!/bin/bas  
NUM=$( echo `wc -l userfile` | cut -d " " -f 1 )  
for i in `seq 1 $NUM `                             
do  
    USERNAME=`sed -n "${i}p" userfile`  
    PASSWORD=`sed -n "${i}p" passwdfile`  
    useradd $USERNAME  
    echo $PASSWORD | passwd --stdin  $USERNAME  
done  
[root@desktop mnt]# sh user
userfile  user.sh   
[root@desktop mnt]# sh user.sh 
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.

d模式

sed -e '2d;6d' fstab   表示第2行,第6行删除

sed -e '2,6d' fstab     表示第2到6行删除

[root@desktop mnt]# cat -n fstab | sed -e '2d;6d'
     1	
     3	# /etc/fstab
     4	# Created by anaconda on Wed May  7 01:22:57 2014
     5	#
     7	# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8	#
     9	UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
    10	/dev/vg0/vo	/home	ext4	defaults	0 0
[root@desktop mnt]# cat -n fstab | sed -e '2,6d'
     1	
     7	# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8	#
     9	UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
    10	/dev/vg0/vo	/home	ext4	defaults	0 0
ed -e '/^#/d' fstab   表示删除以#开头的行

sed -e '/^$/d;/^#/d' fstab   表示删除空行并且以#开头的行     ^$表示首尾相连,也就是空行

sed -e '/UUID/d' fstab   表示删除含有UUID的行

sed -e '/UUID/!d' fstab  表示不删除含有UUID的行,也就是只显示含有UUID的行


a模式

sed '/hello/aword' westos 表示把word加在含有hello后一行  默认是在后面一行追加

sed 's/hello/hello world/g' westos  表示把含有hello的行替换为hello world  ,这里g是全文替换,注意前面有s

sed '/hello/aworld westos' westos  表示在含有hello后面一行追加world westos,其中world westos在一行

sed '/hello/aworld\nwestos' westos  表示在含有hello 后面一行追加 world westos,但是有\n表示换行 

[root@desktop mnt]# vim westos
[root@desktop mnt]# cat westos 
hello
[root@desktop mnt]# sed '/hello/aword' westos 
hello
word
[root@desktop mnt]# sed 's/hello/hello world/g' westos 
hello world
[root@desktop mnt]# sed '/hello/aworld wesotos' westos 
hello
world wesotos

i模式

sed '/hello/iworld\nwestos' westos  表示在含有hello行的前面插入world westos,\n表示换行

[root@desktop mnt]# sed '/hello/iworld\nwestos' westos 
world
westos
hello

c模式

sed '/hello/chello world' westos     表示把含有hello的行替换为hello

sed '/hello/cwestos\nworld' westos   表示把含有hello的行替换为westos world,\n表示换行

[root@desktop mnt]# sed '/hello/chello world' westos 
hello world
[root@desktop mnt]# sed '/hello/cwestos\nworld' westos 
westos
world

w模式

sed -n '/bash$/p' passwd > file     重定向写入文件

sed -n '/bash$/wfile' passwd         在w模式下,可以直接写入文件,这是追加的过程,不会覆盖原文

[root@desktop mnt]# touch file
[root@desktop mnt]# sed -n '/bash$/p' passwd > file 
sed: can't read passwd: No such file or directory
[root@desktop mnt]# cp /etc/passwd /mnt/
[root@desktop mnt]# sed -n '/bash$/p' passwd > file 
[root@desktop mnt]# cat file 
root:x:0:0:root:/root:/bin/bash
student:x:1000:1000:Student User:/home/student:/bin/bash
westos:x:1001:1001::/home/westos:/bin/bash
test:x:1002:1002::/home/test:/bin/bash
user1:x:1003:1003::/home/user1:/bin/bash
user2:x:1004:1004::/home/user2:/bin/bash
user3:x:1005:1005::/home/user3:/bin/bash
[root@desktop mnt]# 










  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shell中的sed命令是一个强大的文本处理工具,可以用于编辑和转换文本文件。sed的工作原理是逐行读取输入文件,并根据指定的编辑命令对文本进行处理。 通过sed命令,我们可以删除、替换、插入和追加文本,以及执行其他各种文本操作。sed命令常用的选项包括-i和-e。-i选项可以直接在文件上进行编辑,而-e选项用于指定要执行的编辑命令。 sed命令的语法格式可以是多种形式,包括在命令行中直接输入sed编辑命令,使用管道传递shell命令和sed编辑命令,以及使用-f选项指定一个sed脚本文件。 总结来说,Shell中的sed命令是一种强大的文本处理工具,可以通过编辑命令对文本文件进行各种操作,包括删除、替换、插入和追加文本等。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [shell脚本中的sed命令用法](https://blog.csdn.net/qq_43028054/article/details/93136955)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Shellsed命令删除特定行的方法](https://download.csdn.net/download/weixin_38560768/14048093)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值