sed基本用法

sed基本用法

概述

sed是一个非交互性文本流编辑器。它编辑文件或标准输入导出的文本拷贝。标准输入可能是来自键盘、文件重定向、字符串或变量,或者是一个管道的文本。

sed从文件的一个文本行或从标准输入的几种格式中读取数据,将之拷贝到一个编辑缓冲区,然后读命令行或脚本的第一条命令,并使用这些命令查找模式或定位行号编辑它。重复此过程直到命令结束。

调用sed

sed命令行格式:

sed [选项] sed命令 输入文件

sed脚本使用格式:

sed [选项] -f sed脚本 输入文件

sed的选项:

  1. n 不打印;缺省为打印所有行

  2. c 下一命令是编辑命令,使用多项编辑时加入此选项。如果只用到一条 sed命令,此选项无用,但指定它也没有关系。

  3. f 调用sed脚本时使用此选项

sed查询文本的方式

x                   x为一行号,如1
x,y                 表示行号范围从x到y,如2,5表示从第2行到第5行
/pattern/           查询包含模式的行
/pattern/pattern/   查询包含两个模式的行
pattern/,x          在给定行号上查询包含模式的行
x,/pattern/         通过行号和模式查询匹配行
x,y!                查询不包含指定行号x和y的行

sed编辑命令

p       打印匹配行
=       显示文件行号
a\      在定位行号后附加新文本信息
i\      在定位行号后插入新文本信息
d       删除定位行
c\      用新文本替换定位文本
s       使用替换模式替换相应模式
r       从另一个文件中读文本
w       写文本到一个文件
q       第一个模式匹配完成后推出或立即推出
l       显示与八进制ASCII代码等价的控制字符
{}      在定位行执行的命令组
n       从另一个文件中读文本下一行,并附加在下一行
g       将模式2粘贴到/pattern n/
y       传送字符
n       延续到下一输入行;允许跨行的模式匹配语句

举例

创建一个文本文件

$pg test.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
  1. 使用p显示指定行

    # 打印单行
    $:sed -n '2p' test.txt
    It was an evening of splendid music and company.
    ​
    # 打印多行
    $:sed -n '1,3p' test.txt
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    Too bad the disco floor fell through at 23:10.
    ​
    # 打印整个文件,$代表最后一行
    $:sed -n '1,$p' test.txt
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    Too bad the disco floor fell through at 23:10.
    The local nurse Miss P.Neave was in attendance.
  2. 打印匹配模式

    # 打印匹配字符串的行
    $ sed -n '/Neave'p test.txt
    The local nurse Miss P.Neave was in attendance.
    ​
    # 模式和行号查询
    $ sed -n '4,/The/'p test.txt
    The local nurse Miss P.Neave was in attendance.
  3. sed脚本

    $pg test.sed
    ​
    #  sed命令解释行
    #!/bin/sed -f
    # 查询定位字符串所在行并在其后加入一行
    /company/ a\
    Then suddenly it happened.
    ​
    # 给可执行权限
    $chmod u+x test.sed
    ​
    $test.sed test.txt
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    Then suddenly it happened.
    Too bad the disco floor fell through at 23:10.
    The local nurse Miss P.Neave was in attendance.

命令之间可以自由组合,如在指定行后插入文本信息 n \i;修改指定行文本n \c/pattern/ c\;删除文本nd/pattern/d;替换文本s/pattern/newpattern/g (g表示全局替换)

快速一行命令

sed是一个强大的文本过滤工具,sed还提供了一些便捷的快速指令

‘s/\.$//g’          删除以句点结尾行
‘-e /abcd/d’        删除包含abcd的行
‘s/[][][]*/[]/g’    删除一个以上空格,用一个空格代替
‘s/^[][]*//g’       删除行首空格
‘s/\.[][]*/[]/g’    删除句点后跟两个或更多空格,代之以一个空格
‘/^$/d’             删除空行
‘s/^.//g’           删除第一个字符
‘s/COL\(...\)//g’   删除紧跟COL的后三个字母
‘s/^\///g’          从路径中删除第一个\
‘s/[]/[]//g’        删除所有空格并用tab键替代
‘S/^[]//g’          删除行首所有tab键
‘s/[]*//g’          删除所有tab键
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值