Linux之sed

一、sed简介

二、sed语法

附:正则表达式元字符

三、sed常用编辑命令

四、sed常用选项

一、sed简介

Linux三大文本处理工具,grep、sed和awk。

Stream Editor,sed是文本流编辑器,它能对文本进行行编辑,使用它能对数据进行匹配查找后,进行添加、删除、替换等操作。

二、sed语法

sed [options] /pattern/ /path/to/file1 /path/to/file2 ······    #pattern为匹配模式;处理多个文件时用空格隔开

地址定界/pattern/:支持正则表达式,默认支持基本正则表达式,使用选项-r支持扩展正则表达式

1、可以仅仅是模式匹配的行

[[email protected] scripts]# sed ‘/^#/p‘ /scripts/test
# The first line.
# The first line.
 The second one.

2、指定行,如1,7第一行到第7行,3,/^#/第3行到#开头的行

[[email protected] scripts]# sed -n ‘1,3p;5,/^#/p‘ /etc/fstab    #打印文件中第1行到第3行、第5行到以井号开头的行其内容
                        #第1行
#                    #第2行    
# /etc/fstab         #第3行
#                    #第4行
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘    #这里第5行和以井号开头的行是同一行

3、查找替换

[email protected]要查找的内容@替换为的内容@g:查找的内容可以使用模式匹配,替换为的内容不能使用模式匹配,但可以引用;标志位:g表示全局,i表示忽略大小写

[[email protected] scripts]# which ls
alias ls=‘ls --color=auto‘
	/bin/ls
[[email protected] scripts]# which ls | sed ‘/^alias/!s#^[[:space:]]##‘    
alias ls=‘ls --color=auto‘
/bin/ls
[[email protected] scripts]# which ls | sed ‘/^alias/d;s#^[[:space:]]##‘    #先去掉alias开头的行,然后搜索替换去掉空白字符
/bin/ls

附:正则表达式元字符

1、基本正则表达式

.    任意单个字符

[]    范围内

[^]     范围外,例如非空白字符[^[:space:]]

[0-9],[[:digi:]]    数字

[a-z],[[:lower:]]    小写字母

[A-Z],[[:upper:]]    大写字母

[a-zA-Z],[[:alpha:]]    字母

[a-zA-Z0-9],[[:alnum:]]    数字+字母

[[:space:]]   空白字符

[[:punct:]]    标点符号

\{m,n\}    至少m次,最多n次

\{m\}    精确m次

\{m,\}    至少m次

\{0,n\}    最多n次

*    匹配前面字符任意字符任意次

\?  匹配前面字符0次或1次

^    锚定行首

$    锚定行尾

\<,\b    锚定词首

\>,\b    锚定词尾

\(\),\1 ,\2,······    分组

2、扩展正则表达式

.    任意单个字符

[]    范围内

[^]     范围外,例如非空白字符[^[:space:]]

[0-9],[[:digi:]]    数字

[a-z],[[:lower:]]    小写字母

[A-Z],[[:upper:]]    大写字母

[a-zA-Z],[[:alpha:]]    字母

[a-zA-Z0-9],[[:alnum:]]    数字+字母

[[:space:]]   空白字符

[[:punct:]]    标点符号

{m,n}    至少m次,最多n次

*    匹配前面字符任意字符任意次

?    匹配前面字符0次或1次

+    匹配前面字符1次或多次

|    或者

( ),\1, \2    分组

^    锚定行首

$    锚定行尾

\<,\b    锚定词首

\>,\b    锚定词尾

三、sed常用编辑命令

p:在屏幕上打印目标文本内容和模式匹配的内容

d:删除匹配的内容并将内容打印到屏幕

!d:删除非匹配的内容并将内容打印到屏幕

a \Text:添加内容到匹配的内容下一行,使用\n能添加下一行

i \Text:添加内容到匹配的内容前面一行

c \Text: 将符合条件的内容替换为指定的文本

r /path/to/file:在匹配内容处加入其他文件的内容

w /path/to/file:将匹配内容保存至指定文件中

=:打印匹配到内容的行号

------------------------------------------

p:在屏幕上打印目标文本内容和模式匹配的内容

[[email protected] scripts]# cat test 
# The first line.
 The second one.
[[email protected] scripts]# sed /^#/p /scripts/test    #未使用-n时,不仅打印匹配内容,文件所有内容也会打印到屏幕
# The first line.
# The first line.
 The second one.

d:删除匹配的内容并将内容打印到屏幕

[[email protected] scripts]# cat test
# The first line.
 The second one.
[[email protected] scripts]# sed /^#/d /scripts/test    #删除以井号开头的行
 The second one.

!d:删除非匹配的内容并将内容打印到屏幕

[[email protected] scripts]# cat test
# The first line.
 The second one.
[[email protected] scripts]# sed ‘/^#/!d‘ /scripts/test    #删除非井号开头的行
# The first line.

a \Text:添加内容到匹配的内容下一行,使用\n能添加下一行

[[email protected] scripts]# cat test
# The first line.
 The second one.
[[email protected] scripts]# sed ‘/^#/a \next line‘ /scripts/test    #内容插入到井号开头的行下一行
# The first line.
next line
 The second one.

i \Text:添加内容到匹配的内容前面一行

[[email protected] scripts]# cat test
# The first line.
 The second one.
[[email protected] scripts]# sed ‘/^#/i \previous line‘ /scripts/test    #内容插入到井号开头行的上一行
previous line
# The first line.
 The second one.
You have new mail in /var/spool/mail/root

r /path/to/somefile:在匹配内容处加入其他文件的内容

[[email protected] scripts]# cat test
# The first line.
 The second one.
[[email protected] scripts]# sed ‘/^#/r /etc/issue‘ /scripts/test    #把issue文件的内容加入匹配内容后
# The first line. 
CentOS release 6.5 (Final)    #issue文件中内容
Kernel \r on an \m    #issue文件中内容
                      #issue文件中内容
 The second one.

c \Text:将匹配内容替换为指定的文本

[[email protected] scripts]# !c
cat test
# The first line.
 The second one.
[[email protected] scripts]# sed ‘/^#/c \Replace line.‘ /scripts/test    #替换内容
Replace line.
 The second one.

w /path/to/file:将匹配内容保存至指定文件中

[[email protected] scripts]# ls
test
[[email protected] scripts]# sed ‘/^#/w /scripts/new‘ /scripts/test
# The first line.
 The second one.
[[email protected] scripts]# cat new    #匹配的内容已经添加到new文件中,经测试,如果文件中存在内容,此命令相当于>输出重定向,而不是>>追加
# The first line.

=:打印匹配到内容的行号

[[email protected] scripts]# cat test
# The first line.
# The second one.
# Last line.
111111111111111
222222222222222
333333333333333
# New line.
[[email protected] scripts]# sed /^#/= /scripts/test    #匹配到内容所在行的行号
1
# The first line.
2
# The second one.
3
# Last line.
111111111111111
222222222222222
333333333333333
7
# New line.

四、sed常用选项

-n:静默模式,仅在屏幕打印出模式匹配的内容

-e:在一个sed命令中使用多个/pattern/,如sed –e /pattern1/ –e /pattern2/ –e /pattern3/ ······ /path/to/file1 /path/to/file2 ······,例如

-i:直接修改文件内容

-r:使用扩展正则表达式元字符

-f:从文件中读取处理脚本,并执行;sed -f sed_scripts8 /path/to/file1 /path/to/file2 ······

------------------------------------------

-n:静默模式,仅在屏幕打印出模式匹配的内容

[[email protected] scripts]# cat test
# The first line.
# The second line.
# Last line.
[[email protected] scripts]# sed /first/p /scripts/test    #默认除了打印匹配的内容,文件内容也将打印到屏幕
# The first line.
# The first line.
# The second line.
# Last line.
[[email protected] scripts]# sed -n /first/p /scripts/test    #仅打印匹配的内容
# The first line.
[[email protected] scripts]#

-e:在一个sed命令中使用多个/pattern/

[[email protected] scripts]# which ls
alias ls=‘ls --color=auto‘
	/bin/ls
[[email protected] scripts]# which ls | sed ‘/^alias/d‘
	/bin/ls
[[email protected] scripts]# which ls | sed -e‘/^alias/d‘ -e s#^[[:space:]]##    #使用多个/pattern/
/bin/ls        
[[email protected] scripts]#

-i:直接修改文件内容

[[email protected] scripts]# cat test
# The first line.
 The second line.
 Last line.
[[email protected] scripts]# sed -i ‘s/The first line./The new line./‘ /scripts/test    #将文件中“The first line.”修改为“The new line.”
[[email protected] scripts]# cat test    #已被修改
# The new line.
 The second line.
 Last line.

-f:从文件中读取处理脚本,并执行;sed -f sed_scripts8 /path/to/file1 /path/to/file2 ······

[[email protected] scripts]# ls
new  sed.script1  test
[[email protected] scripts]# cat sed.script1    #创建一个脚本,里面每行是一个匹配模式或命令 
/^#/p
/second/w /scripts/new2
[[email protected] scripts]# sed -f /scripts/sed.script1 /scripts/test    #使用脚本文件执行命令 
# The new line.
# The new line.
 The second line.
 Last line.
[[email protected] scripts]# ls
new  new2  sed.script1  test
[[email protected] scripts]# cat new2    #第二条命令执行结果--->成功
 The second line.
时间: 2024-12-15 00:46:14

Linux之sed的相关文章

linux之sed用法

sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed [-nefri] 'command' 输入文本 常用选项:        -n∶使用安静(silent)模式.在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到萤幕上.但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来.        -e∶直接在

关于linux中sed命令的简单用法

linux中sed的学习和使用 今天突然用到了sed这个命令,就即兴学习了一波突然感觉这个功能还是很强的.sed在#man sed下文档查看解释的是:用于过滤和转换文本的流编辑器.即如下图: 我自己用过这个命令之后感觉对于文档来说可以显示某个区间行,显示结果还可以在段前后添加内容删除,替换内容.但是最后才发现,怎么还能直接修改到文件里不仅仅是显示出来,文件内容直接被修改了. 下面就一一介绍这个有趣的命令,莫急莫急一个一个来. 首先找个文件(没用的文件且有内容,搞坏了系统文件可不好玩了)来当测试文

linux正则表达式--sed

Linux之sed sed是stream editor(流式编辑器)的缩写,它可以对文本流.指定文件集或标准输入进行文本编辑.功能非常强大. sed命令的基本模式是: sed  [-参数]  '命令'  文本 1. sed两大原则 sed命令总是以单个字母开头.比如 [[email protected]]$echo "hello123" | sed 's/hello/HELLO/'  #把hello用HELLO替换HELLO123 上例中s是替换命令,s后面是分割符号,啥都行(一般用'

【转载】linux之sed用法

linux之sed用法 原文地址:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed [-nefri] 'command' 输入文本 常用选项:        -n∶使用安静(silent)模式.在一般 sed 的用法中

Linux三剑客——sed命令简述

Linux三剑客--sed13.1 sed介绍 sed--流编辑器(stream editor) sed功能与版本:处理文本文件,日志,配置文件增加.删除.修改.查询sed --version 语法格式:sed [选项] [sed指令] [输入文件]sed -i.bak 's#boy#girl#g' oldboy.txt-i sed命令的参数s sed命令g 小尾巴/修饰 sed命令执行过程13.2 sed命令功能示例 等号'='显示行号 sed = person.txt 单行-输入行号即可 s

更新Alpine Linux源 sed -i &#39;s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g&#39; /etc/apk/repositories apk add xxx

更新Alpine Linux源 国内镜像源 清华TUNA镜像源:https://mirror.tuna.tsinghua.edu.cn/alpine/中科大镜像源:http://mirrors.ustc.edu.cn/alpine/阿里云镜像源:http://mirrors.aliyun.com/alpine/ 如何配置软件源 可以使用如下命令: sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositori

[ Linux命令 ] sed

一.sed是什么? Linux sed命令是利用script来处理文本文件. sed可依照script的指令,来处理.编辑文本文件. Sed主要用来自动编辑一个或多个文件:简化对文件的反复操作:编写转换程序等. 二.sed基本语法 sed基本用法: sed: 流编辑器,行编辑器 ( 全屏编辑器:vim ) 模式空间:默认不编辑源文件,仅对模式空间中的数据做处理 sed [option]'AddressCommand' file ... -n: 静默模式    不再默认显示模式空间中的内容    

linux下sed批量替换文件内容

在linux超级终端下编辑文档是件比较麻烦的事情,下面简单介绍一下如何在linux下批量替换文件内容 linuxsed 批量替换多个文件中的字符串 格式: sed -i "s/查找字段/替换字段/g" `grep 查找字段 -rl 路径` 例如:替换/home下所有文件中的xxx为ooo sed -i "s/xxx/ooo/g" `grep xxx -rl /home` 单个文件中的字符串替换 将文件1.txt内的文字“111”替换成“222” sed -i &qu

linux下sed命令对文件执行文本替换

让我们看一下 sed 最有用的命令之一,替换命令.使用该命令,可以将特定字符串或匹配的规则表达式用另一个字符串替换.下面是该命令最基本用法的示例: $ sed -e ‘s/foo/bar/’ myfile.txt 上 面的命令将 myfile.txt 中每行第一次出现的 ‘foo’(如果有的话)用字符串 ‘bar’ 替换,然后将该文件内容输出到标准输出.请注意,我说的是每行第一次出现,尽管这通常不是您想要的.在进行字符串替换时,通常想执行全局替换.也就是说, 要替换每行中的所有出现,如下所示: