linux-文本处理三剑客之sed

sed是Stream editor的简称,和vi不同,sed是行编辑器。sed特点:非交互式增删改查文本。

sed工作原理

sed是从文件或管道中读取一行,处理一行,输出一行;然后再读取一行,处理一行,输出一行,如此反复直到最后一行。每当处理一行时,把当前处理的行存储在临时缓冲区中,这称之为模式空间(Pattern Space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。
在这里插入图片描述
sed命令有默认打印行为。

一次处理一行的设计模式使得sed性能很高,sed在读取大文件时不会出现卡顿现象。如果使用vi命令打开几十M上百M的文件,明显会出现有卡顿的现象,这是因为vi命令打开文件时,一次性将文件所有内容加载到内存中,然后再打开。sed就避免了这种情况,一行一行的处理,打开速度非常快,执行速度也很快。

帮助参考网站:https://www.gnu.org/software/sed/manual/sed.html

在这里插入图片描述

[root@rocky ~]# rpm -qi sed
Name        : sed
Version     : 4.5
Release     : 2.el8
Architecture: x86_64
Install Date: Tue 02 Apr 2024 05:41:49 PM CST
Group       : Applications/Text
Size        : 765126
License     : GPLv3+
Signature   : RSA/SHA256, Wed 14 Apr 2021 04:59:43 PM CST, Key ID 15af5dac6d745a60
Source RPM  : sed-4.5-2.el8.src.rpm
Build Date  : Wed 14 Apr 2021 04:54:38 PM CST
Build Host  : ord1-prod-x86build001.svc.aws.rockylinux.org
Relocations : (not relocatable)
Packager    : infrastructure@rockylinux.org
Vendor      : Rocky
URL         : http://sed.sourceforge.net/
Summary     : A GNU stream text editor
Description :
The sed (Stream EDitor) editor is a stream or batch (non-interactive)
editor.  Sed takes text as input, performs an operation or set of
operations on the text and outputs the modified text.  The operations
that sed performs (substitutions, deletions, insertions, etc.) can be
specified in a script file or from the command line.

sed基本用法

[root@rocky ~]# sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode
  -b, --binary
                 does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
                 open files in binary mode (CR+LFs are not treated specially))
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -E, -r, --regexp-extended
                 use extended regular expressions in the script
                 (for portability use POSIX -E).
  -s, --separate
                 consider files as separate rather than as a single,
                 continuous long stream.
      --sandbox
                 operate in sandbox mode (disable e/r/w commands).
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
  --help
                 display this help and exit
  --version
                 output version information and exit

If no -e, --expression, -f, or --file option is given, then the first non-option argument is taken 
as the sed script to interpret.  Allremaining arguments are names of input files; 
if no input files are specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.

常用选项

格式

sed [option]... 'script;script;...' [inputfile]
-n         不输出模式空间内容到屏幕,即不自动打印
-e         多点编辑
-f FILE    从指定文件中读取编辑脚本
-r,-E      使用扩展正则表达式
-i.bak     备份文件并原处编辑
-s         将多个文件视为独立文件,而不是单个连续的长文件流

#说明:
 -ir       支持
 -i -r     支持
 -ri       支持
 -ni       危险选项,会清空文件

script格式:

'地址命令'

sed -n

在这里插入图片描述
打印指定行
在这里插入图片描述

sed 和 grep

打印非#注释的行
在这里插入图片描述
找出以root开头的行,grep能干的,sed也可以。但是如果仅仅只是过滤行的话,grep语法似乎更简洁,且如果sed只用来过滤行的话,那sed就失去了它的价值。

grep只能看,sed不仅能看还能改,诶!你说牛不牛!
在这里插入图片描述

常用命令

p           打印当前模式空间内容,追加到默认输出之后
Ip          忽略大小写输出
d           删除模式空间匹配的行,并立即启用下一轮循环
a []text    在指定行后面追加文本,支持使用\n实现多行追加
i [\]text   在行前面插入文本
c [\]text   替换行为单行或多行文本
w file      保存模式匹配的行至指定文件
r file      读取指定文件的文本至模式空间中匹配到的行后
=           为模式空间中的行打印行号
!           模式空间中匹配行取反处理
q           结束或退出sed

sed ‘p’

sed的默认有打印功能,如果再加上p参数就是两次打印了。-n参数是关闭自动打印
在这里插入图片描述

sed ‘/root/d’ xxx

在这里插入图片描述
在这里插入图片描述

sed ‘5a abc’

在这里插入图片描述
在这里插入图片描述

sed ‘5i\ lei’

在这里插入图片描述

sed ‘5c\ lei very handsome’

在这里插入图片描述

sed -e ‘5i\lei’ -e ‘5a\very handsome’

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

sed ‘3,6r /data/sed.log’

在这里插入图片描述
在这里插入图片描述

sed ‘/^d/, /^s/r /data/issue’ /etc/passwd

将issue文件的内容添加上passwd文件以d开头的行和s开头的行中间
在这里插入图片描述

地址格式

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

2.单地址:
  #:指定的行,$:最后一行
  /pattern/: 被此处模式所能够匹配到的每一行
  
3.地址范围:
  num,num           从num行到第num行, 3,6 从第3行到第6行
  num,+num          从num行到+num行,  3,+4表示从3行到第7行
  /pat1/,/pat2/
  num, /pat/
  /pat/ ,num

4.步进:~
  1~2 奇数行
  2~2 偶数行

在这里插入图片描述

sed -n ‘num,num’

在这里插入图片描述
在这里插入图片描述

sed -n ‘num,+num’

在这里插入图片描述

sed -n ‘/d/,/s/p’ /data/passwd

查找以d开头的行到以s结尾的行
在这里插入图片描述

sed ‘/root/=’ /etc/passwd

在这里插入图片描述

sed -i.bak

备份文件并原处编辑

上文的所有命令都是在模式空间中修改的的文件,并没有影响到真正的磁盘文件,使用sed -i.bak才是修改真的文件并将原文件备份为.bak后缀文件。
在这里插入图片描述
在这里插入图片描述

修改httpd.conf文件

sed -i.bak -e '/^User apache/cUser daemon' -e '/^Group apache/cGroup daemon' /data/apache/httpd.conf 

在这里插入图片描述

修改SELinux文件

sed -i.bak '/^SELINUX=enforcing/c SELINUX=disabled' /etc/selinux/config

在这里插入图片描述

查找替代

s/pattern/string/修饰符  查找替换,支持使用其它分隔符,可以是其它形式: s@@@,s###

替换修饰符:
g     行内全局替换
p     显示替换成功的行
w     /PATH/FILE  将替换成功的行保存至文件中
I,i   忽略大小写
  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值