• Linux sed命令详解


    Linux sed命令

    Linux sed命令是利用script来处理文本文件。sed可依照script的指令,来处理、编辑文本文件。主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。sed 是一种新型的,非交互式的编辑器。它能执行与编辑器 vi 和 ex 相同的编辑任务。sed 编辑器没有提供交互式使用方式,使用者只能在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。 sed 编辑器没有破坏性,它不会修改文件,除非使用 shell 重定向来保存输出结果。默认情况下,所有的输出行都被打印到屏幕上。

    sed工作流程

    sed 编辑器逐行处理文件(或输入),并将输出结果发送到屏幕。 sed 的命令就是在 vi和 ed/ex 编辑器中见到的那些。 sed 把当前正在处理的行保存在一个临时缓存区中,这个缓存区称为模式空间或临时缓冲。sed 处理完模式空间中的行后(即在该行上执行 sed 命令后),就把改行发送到屏幕上(除非之前有命令删除这一行或取消打印操作)。 sed 每处理完输入文件的最后一行后, sed 便结束运行。 sed 把每一行都存在临时缓存区中,对这个副本进行编辑,所以不会修改或破坏源文件

    用法:

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

    sed定位:

    sed 命令在没有给定的位置时,默认会处理所有行;
    sed 支持一下几种地址类型:
    1、 first~step
    这两个单词的意思: first 指起始匹配行, step 指步长,例如: sed -n 2~5p 含义:从第二行开始匹配,隔 5 行匹配一次,即 2,7,12.......
    2、 $
    这个$符表示匹配最后一行。
    3、 /REGEXP/
    这个是表示匹配正则那一行,通过//之间的正则来匹配。
    4、 cREGEXPc
    这个是表示匹配正则那一行,通过c 和 c 之间的正则来匹配,c 可以是任一字符
    5、 addr1, add2 
    定址 addr1, add2 决定用于对哪些行进行编辑。地址的形式可以是数字、正则表达式或二者的结合。如果没有指定地址, sed 将处理输入文件中的所有行。如果定址是一个数字,则这个数字代表行号,如果是逗号分隔的两个行号,那么需要处理的定址就是两行之间的范围(包括两行在内)。范围可以是数字,正则或二者组合。

    6、 addr1, +N

    从 addr1 这行到往下 N 行匹配,总共匹配 N+1 行
    7、 addr1, ~N
    Will match addr1 and the lines following addr1 until the next line whose input line number is a multiple of N.【没有看懂是什么意思】

    参数:

    -e        多重编辑,且命令顺序会影响结果
    -f        以选项中指定的script文件来处理输入的文本文件。
    -n        仅显示script处理后的结果。
    -l N      指定"l"命令的换行期望长度
    -r        在脚本中使用扩展正则表达式
    -s        将输入文件视为各个独立的文件而不是一个长的连续输入
    -u        从输入文件读取最少的数据,更频繁的刷新输出
    -i        直接修改文档读取内容,不在屏幕输出

    动作:

    a  在当前行后添加一行或多行。多行时除最后一行外,每行末尾需用“”续行
    c  用此符号后的新文本替换当前行中的文本。多行时除最后一行外,每行末尾需用""续行
    i  在当前行之前插入文本。多行时除最后一行外,每行末尾需用""续行
    d   删除行
    h   把模式空间里的内容复制到暂存缓冲区
    H   把模式空间里的内容追加到暂存缓冲区
    g   把暂存缓冲区里的内容复制到模式空间,覆盖原有的内容 
    G   把暂存缓冲区的内容追加到模式空间里,追加在原有内容的后面 
    l   列出非打印字符
    p   打印行
    n   读入下一输入行,并从下一条命令而不是第一条命令开始对其的处理
    q   结束或退出sed
    r   从文件中读取输入行
    !   对所选行以外的所有行应用命令 
    s   用一个字符串替换另一个
    g   在行内进行全局替换
    w   将所选的行写入文件
    x   交换暂存缓冲区与模式空间的内容
    y   将字符替换为另一字符(不能对正则表达式使用y命令)

    示例:

    我们使用以下一个测试文件来作为输入文件,练习sed命令的使用:

    [root@localhost shell]# cat sed.txt 
    northwest       NW      Charles Main    3.0     .98     3       34
    western         WE      Sharon Gray     5.3     .97     5       23
    southwest       SW      Lewis Dalsass   2.7     .8      2       18
    southern        SO      Suan Chin       5.1     .95     4       15
    southeast       SE      Patricia Hemenway       4.0     .7      4       17
    eastern         EA      TB Savage       4.4     .84     5       20
    northeast       NE      AM Main Jr.     5.1     .94     3       13
    north           NO      Margot Weber    4.5     .89     5       9

    命令 是打印命令,用于显示模式缓存区的内容。默认情况下, sed 把输入行打印在屏幕上,选项-n 用于取消默认打印操纵。当选项-n 和命令 同时出现时, sed 可打印选定的内容

    案例1:

    [root@localhost shell]# sed '/north/p' sed.txt 
    northwest       NW      Charles Main    3.0     .98     3       34
    northwest       NW      Charles Main    3.0     .98     3       34
    western         WE      Sharon Gray     5.3     .97     5       23
    southwest       SW      Lewis Dalsass   2.7     .8      2       18
    southern        SO      Suan Chin       5.1     .95     4       15
    southeast       SE      Patricia Hemenway       4.0     .7      4       17
    eastern         EA      TB Savage       4.4     .84     5       20
    northeast       NE      AM Main Jr.     5.1     .94     3       13
    northeast       NE      AM Main Jr.     5.1     .94     3       13
    north           NO      Margot Weber    4.5     .89     5       9
    north           NO      Margot Weber    4.5     .89     5       9
    central         CT      Ann Stephens    5.7     .94     5       13

    说明:默认情况下, sed 把所有输入行都打印在标准输出上。如果在某一行匹配到 north, sed就把该行另外打印一遍。 

    案例2:

    [root@localhost shell]# sed -n '/north/p' sed.txt 
    northwest       NW      Charles Main    3.0     .98     3       34
    northeast       NE      AM Main Jr.     5.1     .94     3       13
    north           NO      Margot Weber    4.5     .89     5       9

    说明:默认情况下, sed 打印当前缓存区中的输入行。命令 p 指示 sed 将再次打印该行。选项-n 取消 sed 取消默认打印操作。选项-n 和命令配合使用,模式缓冲区内的输入行,只被打印一次。如果不指定-n 选项, sed 就会像上例中那样,打印出重复的行。如果指定了-n,则sed 只打印包含模式 north 的行。 

    删除: d 命令

    命令 d 用于删除输入行。sed 先将输入行从文件复制到模式缓存区,然后对该行执行 sed命令,最后将模式缓存区的内容显示在屏幕上。如果发出的是命令 d,当前模式缓存区的输入行会被删除,不被显示。
    案例 3:

    [root@localhost shell]# sed '3d' sed.txt 
    northwest       NW      Charles Main    3.0     .98     3       34
    western         WE      Sharon Gray     5.3     .97     5       23
    southern        SO      Suan Chin       5.1     .95     4       15
    southeast       SE      Patricia Hemenway       4.0     .7      4       17
    eastern         EA      TB Savage       4.4     .84     5       20
    northeast       NE      AM Main Jr.     5.1     .94     3       13
    north           NO      Margot Weber    4.5     .89     5       9
    central         CT      Ann Stephens    5.7     .94     5       13

    说明:删除第 行。默认情况下,其余的行都被打印到屏幕上。 

    案例 4: 

    [root@localhost shell]# sed '3,$d' sed.txt 
    northwest       NW      Charles Main    3.0     .98     3       34
    western         WE      Sharon Gray     5.3     .97     5       23

    说明:删除从第三行到最后一行内容,剩余各行被打印。地址范围是开始第 行,结束最后一行。

    案例 5

    [root@localhost shell]# sed '/north/d' sed.txt 
    western         WE      Sharon Gray     5.3     .97     5       23
    southwest       SW      Lewis Dalsass   2.7     .8      2       18
    southern        SO      Suan Chin       5.1     .95     4       15
    southeast       SE      Patricia Hemenway       4.0     .7      4       17
    eastern         EA      TB Savage       4.4     .84     5       20
    central         CT      Ann Stephens    5.7     .94     5       13

    替换: s 命令

    命令 s 是替换命令。替换和取代文件中的文本可以通过 sed 中的 s 来实现, s 后包含在斜杠中的文本是正则表达式,后面跟着的是需要替换的文本。可以通过 g 标志对行进行全局替换
    案例 6:

    [root@localhost shell]# sed 's/west/north/g' sed.txt 
    northnorth       NW      Charles Main    3.0     .98     3       34
    northern         WE      Sharon Gray     5.3     .97     5       23
    southnorth       SW      Lewis Dalsass   2.7     .8      2       18
    southern        SO      Suan Chin       5.1     .95     4       15
    southeast       SE      Patricia Hemenway       4.0     .7      4       17
    eastern         EA      TB Savage       4.4     .84     5       20
    northeast       NE      AM Main Jr.     5.1     .94     3       13
    north           NO      Margot Weber    4.5     .89     5       9
    central         CT      Ann Stephens    5.7     .94     5       13

    说明:命令用于替换。命令末端的 表示在行内全局替换;也就是说如果每一行里出现多个west,所有的 west 都会被替换为 north。如果没有 命令,则只将每一行的第一 west 替换为 north
    案例 7

    [root@localhost shell]# sed -n 's/^west/north/p' sed.txt 
    northern         WE      Sharon Gray     5.3     .97     5       23

    说明:命令用于替换。选线-n 与命令行末尾的标志 结合,告诉 sed 只打印发生替换的那些行;也就是说,如果只有在行首找到 west 并替换成 north 时才会打印此行。
    案例 8

    [root@localhost shell]# sed 's/[0-9][0-9]$/&.5/' sed.txt 
    northwest       NW      Charles Main    3.0     .98     3       34.5
    western         WE      Sharon Gray     5.3     .97     5       23.5
    southwest       SW      Lewis Dalsass   2.7     .8      2       18.5
    southern        SO      Suan Chin       5.1     .95     4       15.5
    southeast       SE      Patricia Hemenway       4.0     .7      4       17.5
    eastern         EA      TB Savage       4.4     .84     5       20.5
    northeast       NE      AM Main Jr.     5.1     .94     3       13.5
    north           NO      Margot Weber    4.5     .89     5       9
    central         CT      Ann Stephens    5.7     .94     5       13.5

    说明:当“与”符号( &)用在替换串中时,它代表在查找串中匹配到的内容时。这个示例中所有以 位数结尾的行后面都被加上.5
    案例 9

    [root@localhost shell]# sed -n 's/Hemenway/Jones/gp' sed.txt
    southeast       SE      Patricia Jones       4.0     .7      4       17

    说明:文件中出现的所有的 Hemenway 都被替换为 Jones,只有发生变化的行才会打印出来。选项-n 与命令 的组合取消了默认的输出。标志 的含义是表示在行内全局替换

    案例 10

    [root@localhost shell]# sed 's/(Mar)got/1linanne/p' sed.txt 
    northwest       NW      Charles Main    3.0     .98     3       34
    western         WE      Sharon Gray     5.3     .97     5       23
    southwest       SW      Lewis Dalsass   2.7     .8      2       18
    southern        SO      Suan Chin       5.1     .95     4       15
    southeast       SE      Patricia Hemenway       4.0     .7      4       17
    eastern         EA      TB Savage       4.4     .84     5       20
    northeast       NE      AM Main Jr.     5.1     .94     3       13
    north           NO      Marlinanne Weber    4.5     .89     5       9
    north           NO      Marlinanne Weber    4.5     .89     5       9
    central         CT      Ann Stephens    5.7     .94     5       13

    说明:包含在圆括号里的模式 Mar 作为标签 保存在特定的寄存器中。替换串可以通过来引用它。则 Margot 被替换为 Marlinane
    案例 11

    [root@localhost shell]# sed 's#3#88#g' sed.txt 
    northwest       NW      Charles Main    88.0     .98     88       884
    western         WE      Sharon Gray     5.88     .97     5       288
    southwest       SW      Lewis Dalsass   2.7     .8      2       18
    southern        SO      Suan Chin       5.1     .95     4       15
    southeast       SE      Patricia Hemenway       4.0     .7      4       17
    eastern         EA      TB Savage       4.4     .84     5       20
    northeast       NE      AM Main Jr.     5.1     .94     88       188
    north           NO      Margot Weber    4.5     .89     5       9
    central         CT      Ann Stephens    5.7     .94     5       188

    说明:紧跟在 命令后的字符就是查找串和替换串之间的分隔符。分隔符默认默认为正斜杠,但可以改变。无论什么字符(换行符,反斜线除外),只要紧跟在 命令,就成了新的串分隔符。这个方法在查找包含正斜杠模式时很管用,例如查找路径名或生日。

    指定行的范围:逗号

    行的范围从文件中的一个地址开始,在另一个地址结束。地址范围可以是行号(例如5,10),正则表达式(例如/Dick/和/Joe/),或者两者的结合(例如/north/,$)范围是闭合的——包含开始条件的行,结束条件的行,以及两者之间的行。如果结束条件无法满足,就会一直操作到文件结尾。如果结束条件满足,则继续查找满足开始条件的位置,范围重新开始。
    案例 12

    [root@localhost shell]# sed -n '/west/,/east/p' sed.txt 
    northwest       NW      Charles Main    3.0     .98     3       34
    western         WE      Sharon Gray     5.3     .97     5       23
    southwest       SW      Lewis Dalsass   2.7     .8      2       18
    southern        SO      Suan Chin       5.1     .95     4       15
    southeast       SE      Patricia Hemenway       4.0     .7      4       17

    说明:打印模式 west 和 east 之间所有的行。如果 west 出现在 east 之后的某一行,则打印的范围从 west 所在行开始,到下一个出现 east 的行或文件的末尾。

    案例 13

    [root@localhost shell]# sed -n '5,/northeast/p' sed.txt 
    southeast       SE      Patricia Hemenway       4.0     .7      4       17
    eastern         EA      TB Savage       4.4     .84     5       20
    northeast       NE      AM Main Jr.     5.1     .94     3       13

    说明:打印从第 行开始第一个以 northeast 开头的行之间的所有行。

    案例 14

    [root@localhost shell]# sed '/west/,/east/s/$/**VACA**/' sed.txt 
    northwest       NW      Charles Main    3.0     .98     3       34**VACA**
    western         WE      Sharon Gray     5.3     .97     5       23**VACA**
    southwest       SW      Lewis Dalsass   2.7     .8      2       18**VACA**
    southern        SO      Suan Chin       5.1     .95     4       15**VACA**
    southeast       SE      Patricia Hemenway       4.0     .7      4       17**VACA**
    eastern         EA      TB Savage       4.4     .84     5       20
    northeast       NE      AM Main Jr.     5.1     .94     3       13
    north           NO      Margot Weber    4.5     .89     5       9
    central         CT      Ann Stephens    5.7     .94     5       13
  • 相关阅读:
    C语言初学者应该知道的(一)
    【文摘】中国IT从业者的职业——软件项目管理师
    只有荒凉的沙漠,没有荒凉的人生 ——黄渤
    定时更改桌面背景
    C语言初学者应该知道的(二)
    整理那些书
    Linux操作系统———李纳斯
    【c笔记】一个很好的编程题
    【c 笔记】windows的wsprintf不支持浮点型的%f
    【文摘】中国IT从业者的职业——软件测试员
  • 原文地址:https://www.cnblogs.com/jkin/p/10417230.html
Copyright © 2020-2023  润新知