上一篇总结了sed的基础应用(Linux Bash文本操作之sed篇其一),内容实在有够多,这里再对稍微高级一些的用法做一个整理,以方便使用时查阅。

查看文本内容

示例1表示在第一到第四行匹配到的行后面添加一行空行。

示例2带行号显示文本,行号与内容间隔两个空格,也可以是使用  \t  换成制表符。

示例3可以用来实现类似于 grep 的操作。使用正则表达式显示文中匹配到  sed  的行。

示例4中使用正则表达式配合  d  指令实现了与3相同的结果。

示例5是  grep  命令得到的结果,与前两个示例所不同的是  grep  显示出来的是带有颜色信息的,清晰地指明了匹配发生的位置。

利用标注的标签进行跳转,相当于分支,用于改变流处理顺序。

示例6显式匹配到字符串的下一行的内容,使用  n  来读取下一行内容到模式空间,替换当前行内容。

示例7使文本显示为左对齐。

: label     Label for b and t commands.
b label Branch to label; if label is omitted, branch to end of script.
t label If a s/// has done a successful substitution since the last input line was read and
since the last t or T command, then branch to label; if label is omitted, branch to
end of script.
cv@cv:~/myfiles$ sed -n '1,4{/stream/G;p}' test.txt    #example-
NAME
sed - stream editor for filtering and transforming text SYNOPSIS
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
cv@cv:~/myfiles$ sed = test.txt | sed 'N;s/\n/ /' #example-
NAME
sed - stream editor for filtering and transforming text
SYNOPSIS
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an
editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text
in a pipeline which particularly distinguishes it from other types of editors. -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]
cv@cv:~/myfiles$ sed -n '/sed/p' test.txt #example-
sed - stream editor for filtering and transforming text
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an
editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ sed '/sed/!d' test.txt #example-
sed - stream editor for filtering and transforming text
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an
editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ grep 'sed' test.txt #example-
sed - stream editor for filtering and transforming text
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an
editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text
cv@cv:~/myfiles$ sed -n '/OPTION/{n;p}' test.txt         #example-
DESCRIPTION cv@cv:~/myfiles$ sed ':a;s/^.\{1,70\}$/ &/;ta;s/\( *\)//' test.txt #example-
NAME
sed - stream editor for filtering and transforming text
SYNOPSIS
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an
editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text
in a pipeline which particularly distinguishes it from other types of editors. -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]

我们还可以结合临时空间与模式空间之间的特性实现将文本前后各行顺序颠倒的效果,也就是第一行变成最后一行,第二行变成倒数第二行,第三行变成倒数第三行……最后一行变成第一行,类似于  tac  的操作。

下面有一张图清晰的表明了整个转换过程,图的来源是参考文献[3]给出的链接。

cv@cv:~/myfiles$ sed '1!G;h;$!d;' test.txt
-i[SUFFIX], --in-place[=SUFFIX]
follow symlinks when processing in place
--follow-symlinks
add the contents of script-file to the commands to be executed
-f script-file, --file=script-file
add the script to the commands to be executed
-e script, --expression=script
suppress automatic printing of pattern space
-n, --quiet, --silent in a pipeline which particularly distinguishes it from other types of editors.
editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an
DESCRIPTION
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
SYNOPSIS
sed - stream editor for filtering and transforming text
NAME

还可以实现类似于tail的效果。

如示例1相当于 tail -n  ,除最后两行之外合并,以  \n  相连接,然后再从模式空间删除。

D表示删除模式空间中从第一个字符到第一个换行符的内容,并且跳转到命令开头重新执行。并且当模式空间仍有内容时,不读入新的输入行,类似形成一个循环。

P命令仅打印模式空间中从第一个字符到第一个换行符的内容,重新在模式空间的内容上执行编辑命令,类似形成一个循环。

示例2打印最后  m-  行,通过  N  和  D  命令的循环来实现。如果到最后一行,终止退出,否则的话读入下一行追加到当前模式空间。

示例3用于打印匹配行的下一行内容。

示例4用于打印匹配行的上一行与下一行。

D   If pattern space contains no newline, start a normal new cycle as if the d command was issued.
Otherwise, delete text in the pattern space up to the first newline, and restart cycle with the
resultant pattern space, without reading a new line of input.
cv@cv:~/myfiles$ sed '$!N;$!D' test.txt             #example-

cv@cv:~/myfiles$ sed ':a;$q;N;3,$D;ba;' test.txt     #example-
follow symlinks when processing in place
-i[SUFFIX], --in-place[=SUFFIX]
cv@cv:~/myfiles$ sed -n '/sed/{g;1!p;};h' test.txt    #example-
NAME
SYNOPSIS
DESCRIPTION
DESCRIPTION cv@cv:~/myfiles$ sed -n '/sed/{x;1!p;=;g;$!N;p;D;};h' test.txt #example-
NAME sed - stream editor for filtering and transforming text
SYNOPSIS
SYNOPSIS sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION
DESCRIPTION Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an
editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text
in a pipeline which particularly distinguishes it from other types of editors.

删除文本内容

还有一种  addr1  为   的地址范围表示法,不过这时  addr2  只能使用正则表达式来表示。其结果与前面给出的两个地址的结果相同。

示例1删除每一行开头所有空格。

示例2表示删除每一行末尾所有空格。

示例3是1和2的综合作用,删除每一行开头和末尾的所有空格。

示例4表示只删除匹配到的第一行内容。

,addr2    Start out in "matched first address" state, until addr2 is found. This is similar to ,addr2, except that if addr2 matches the
very first line of input the ,addr2 form will be at the end of its range, whereas the ,addr2 form will still be at the beginning
       of its range. This works only when addr2 is a regular expression.
cv@cv:~/myfiles$ sed -n 's/^[ ^t]*//p' test.txt    #example-

cv@cv:~/myfiles$ sed -n 's/[ ^t]*$//p' test.txt    #example-

cv@cv:~/myfiles$ sed -n -e 's/^[ ^t]*//;s/[ ^t]*$//p' test.txt    #example-
cv@cv:~/myfiles$ sed '0,/sed/{//d;}' test.txt #example-
NAME
SYNOPSIS
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to a
editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter tex
in a pipeline which particularly distinguishes it from other types of editors. -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]

对于空行的删除我们还可以有很多不同的方法。可以在我们的测试文件里添加一些空行试试效果。

示例1表示删除文本中正文第一行之前的若干空行。

示例2删除文中连续的空行,只保留一行,也可以理解为将中间连续的空行合并成一行。

示例3将多于一行的连续空行删除掉,或者说合并成一行,与上面的命令效果相同。

示例4将多于两行的连续空行删减成两行,或者说只保留两行。

示例5删除最尾部的空白行,找到最后一行则删除,不是最后一行用N命令追加到其下一行。

cv@cv:~/myfiles$ sed '/./,$!d' test.txt              #example-

cv@cv:~/myfiles$ sed '/./,/^$/!d' test.txt           #example-

cv@cv:~/myfiles$ sed '/^$/N;/\n$/D' file.txt         #example-

cv@cv:~/myfiles$ sed '/^$/N;/\n$/N;//D' test.txt     #example-

cv@cv:~/myfiles$ sed ':a;/^\n*$/{$d;N;ba}' test.txt  #example-

当我们想要删除文本最后一行时非常容易,直接 sed -i '$d' test.txt 即可,而当我们想要删除最后几行时又该如何操作呢?

诚然我们可以写一个脚本循环删除最后一行,如示例1所示。或者根据文本总行数进行行范围删除操作,如示例2。

我们还可以结合  N P D  来操作,如示例3。推荐使用这种方式

如果要删除的行数比较少,如最后两行,可以使用示例4或者示例5。

示例5大括号内的N有几个就表示删除几加一行。

#!/bin/bash
#myscript for example-1 num=
count=
while [ $count -le $num ];do
sed -i '$d' test.txt
let count=count+
done
echo "Done!"
#!/bin/bash
#myscript for example-2
n=
file=cptest.txt
A=$(sed -n '$=' $file)
sed $(($A-$n+)),${A}d $file
cv@cv:~/myfiles$ bash ./myscript.sh    #example-
cv@cv:~/myfiles$ bash ./myscript.sh #example- cv@cv:~/myfiles$ n=;sed '1{:a;N;'$n'!ba};$d;N;P;D' test.txt #example- cv@cv:~/myfiles$ sed 'N;$!P;$!D;$d' test.txt #example- cv@cv:~/myfiles$ sed -n '1{N;N;};:a;N;P;D;ta' test.txt #example-

增加文件内容

下面的指令用于交换模式空间和临时空间的内容。

示例1实现在每一个匹配行的前面添加一行空行。读到文本的一行后,模式空间内为该行内容,临时空间暂时为空,此时如果交换两者内容,模式空间变为空,打印出来就是空行;

然后再将两者交换回来,模式空间又变成了刚才读到的一行内容,这是再打印就是文本内容了。最终结果就是在文本之前多了一行空行。

示例2实现在每一个匹配行的前面和后面各添加一行空行。

第二次交换后模式空间中为原来读到的一行内容,临时空间为空,这时将临时空间的内容追加到模式空间之后,就相当于在行后又添加了一行空行。

x   Exchange the contents of the hold and pattern spaces.
cv@cv:~/myfiles$ sed -n '/sed/{x;p;x;p}' test.txt    #example-

       sed - stream editor for filtering and transforming text

       sed [OPTION]... {script-only-if-no-other-script} [input-file]...

       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an

       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

cv@cv:~/myfiles$ sed -n '/sed/{x;p;x;G;p}' test.txt | sed '$d'    #example-

       sed - stream editor for filtering and transforming text

       sed [OPTION]... {script-only-if-no-other-script} [input-file]...

       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an

       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

替换/转换文本内容

之前我们使用的都是基本正则表达式,如果在环境选择时加上-r的选项,则可以使用扩展正则表达式。

在基本正则中,几个字符(  ? + {} () |  )默认被解释为普通字符,也即字面意思,跟  a b c  这样的字符一样表示 ASCII 字符,如果想要使用其对应的特殊含义,如使用  ( )  表示选项列表,  |  表示选项等,则需要加反斜杠  \  转义。

而在扩展正则中,默认情况下上述字符  ? + {} () |  被解释为特殊含义,如果想要使用其所对应的普通含义,作为一个普通字符来使用,需要加反斜杠  \  转义。

-r, --regexp-extended    use extended regular expressions in the script.

示例1表示将所有以s或S开头的单词转换成全部大写的形式。  \<  词首锚定,用于匹配单词词首。

示例2是使用之前所说的单词边界匹配符,可以得到同样的结果,注意这里的  () +  在普通正则中使用时需要转义成特殊含义。

示例3表示在匹配到字符串的第一行,将其中的sed替换成目标字符串。

cv@cv:~/myfiles$ sed -n -r 's/\<(s|S)[a-z]+/\U&/gp' test.txt    #example-
SED - STREAM editor for filtering and transforming text
SED [OPTION]... {SCRIPT-only-if-no-other-SCRIPT} [input-file]...
SED is a STREAM editor. A STREAM editor is used to perform basic text transformations on an input STREAM (a file or input from a pipeline). While in SOME ways SIMILAR to an
editor which permits SCRIPTED edits (SUCH as ed), SED works by making only one pass over the input(s), and is consequently more efficient. But it is SED's ability to filter text
-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
cv@cv:~/myfiles$ sed -n -e 's/\b\(s\|S\)[a-z]\+/\U&/gp' test.txt #example-2
SED - STREAM editor for filtering and transforming text
SED [OPTION]... {SCRIPT-only-if-no-other-SCRIPT} [input-file]...
SED is a STREAM editor. A STREAM editor is used to perform basic text transformations on an input STREAM (a file or input from a pipeline). While in SOME ways SIMILAR to an
editor which permits SCRIPTED edits (SUCH as ed), SED works by making only one pass over the input(s), and is consequently more efficient. But it is SED's ability to filter text
-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
cv@cv:~/myfiles$ sed -n '0,/sed/s//to_that/p' test.txt #example-
to_that - stream editor for filtering and transforming text

在根据匹配进行字符串等的替换时,我们可以指定替换第几个匹配。

如示例1与示例2,在s//num中num指的是第几个匹配位置,比如1表示该行第一个匹配,2表示第二个。

示例1就表示将每行第一个sed替换成目标字符串。

示例2就表示将每行第二个替换掉。

示例3表示替换倒数第二个匹配。

示例4表示替换倒数第一个,也就是最后一个匹配。

cv@cv:~/myfiles$ sed -n 's/sed/XXX/1p' test.txt    #example-
XXX - stream editor for filtering and transforming text
XXX [OPTION]... {script-only-if-no-other-script} [input-file]...
Sed is a stream editor. A stream editor is uXXX to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an
editor which permits scripted edits (such as ed), XXX works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ sed -n 's/sed/XXX/2p' test.txt #example-
editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is XXX's ability to filter text cv@cv:~/myfiles$ sed -n 's/\(.*\)sed\(.*sed\)/\1XXX\2/p' test.txt #example-
editor which permits scripted edits (such as ed), XXX works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ sed -n 's/\(.*\)sed/\1XXX/p' test.txt #example-
XXX - stream editor for filtering and transforming text
XXX [OPTION]... {script-only-if-no-other-script} [input-file]...
Sed is a stream editor. A stream editor is uXXX to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an
editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is XXX's ability to filter text

参考文献

[1] Learning Linux Commands: sed

[2] Shell正则表达式

[3] 【系统工程师的自我修养】sed篇

Linux Bash文本操作之sed篇其二的更多相关文章

  1. Linux Bash文本操作之sed篇其一

    作为Linux系统中文本处理的强力工具之一,sed功能强大,用法多变,值得我们好好学习. sed是用于过滤和转换文本的流编辑器. 一般情况下sed把当前处理的行存储在临时缓冲区,按指定命令处理之后将缓 ...

  2. Linux Bash文本操作之grep篇

    Linux grep命令用于查找文件里符合条件的字符串.是文本检索中常用的工具之一. grep  指令在文件中查找能够匹配指定模式字符串的行.如果没有指定文件名,或者文件名为  -  ,则从标准输入设 ...

  3. 【Linux】linux中文本操作利器grep,awk,sed

    grep命令 grep(global search regular expression)是一种强大的文本搜索工具,它可以使用正则表达式搜索文本,并把匹配的行打印出来.平时搜索文本中内容的时候是非常方 ...

  4. linux 常用文本操作相关命令

    平时工作经常会对文本进行相关操作,包括读写.替换.统计等等,借此整理和学习一下有关命令. 1. cat 查看文件中的内容, -n 查看时为每一行加编号; -b 和-n类似,只不过对于空白行不编号: 2 ...

  5. linux下文本三剑客之sed

    继上一篇对正则表达式以及linux三剑客grep的讲解后,这一篇对sed重点介绍. 一.sed简介   sed表示流编辑器(Stream Editor).这是一个简单但功能强大的工具,分析文本,并无缝 ...

  6. linux之文本编辑器vi常用命令

    由于经常在linux下面文本操作,所以这里稍微系统的总结一下自己常用的vi命令 1.打开命令: vi+filename  (还有各种打开的姿势,只不过我比较顺手这个) 2.退出命令: :q   退出而 ...

  7. Linux的文本处理工具浅谈-awk sed grep

    Linux的文本处理工具浅谈 awk   老大 [功能说明] 用于文本处理的语言(取行,过滤),支持正则 NR代表行数,$n取某一列,$NF最后一列 NR==20,NR==30 从20行到30行 FS ...

  8. Linux命令-文件文本操作grep

    文件文本操作 grep 在文件中查找符合正则表达式条件的文本行 cut 截取文件中的特定字段 paste 附加字段 tr 字符转换或压缩 sort 调整文本行的顺序,使其符合特定准则 uniq 找出重 ...

  9. 看完这篇Linux基本的操作就会了

    前言 只有光头才能变强 这个学期开了Linux的课程了,授课的老师也是比较负责任的一位.总的来说也算是比较系统地学习了一下Linux了~~~ 本文章主要是总结Linux的基础操作以及一些简单的概念~如 ...

随机推荐

  1. MySQL 高可用架构 之 MHA (Centos 7.5 MySQL 5.7.18 MHA 0.58)

    目录 简介 环境准备 秘钥互信 安装基础依赖包 安装MHA组件 安装 MHA Node组件 安装 MHA Manager 组件 建立 MySQL 一主三从 初始化 MySQL 启动MySQL 并简单配 ...

  2. C标准输入输出库

    这样的代码有什么问题? char c; while((c = getchar()) != EOF) ... 首先,保存getchar的返回值的变量必须是int型.EOF是getchar返回的“超出范围 ...

  3. for循环、while循环、break跳出循环、continue结束本次循环、exit退出整个脚本

    7月13日任务 20.10 for循环20.11/20.12 while循环20.13 break跳出循环20.14 continue结束本次循环20.15 exit退出整个脚本扩展select用法 ...

  4. 【Flask系列】开发一个简单的Flask程序

    知识点 初始化:每一个flask程序都必须创建一个程序实例,遵循WSGI(Web Server Gateway interface)协议,把请求->flask Obj; 创建实例: app = ...

  5. 关于iframe/子窗体与父窗体的交互

    父子窗体交互方式 通过contentWindow交互 通过postMessage交互 通过contentWindow交互 主窗体内嵌的iframe或者是其通过js打开的新窗口都可以通过contentW ...

  6. flutter最简单轻量便捷的路由管理方案NavRouter

    大家好,我是CrazyQ1,今天给大家推荐一个路由管理方案,用的非常不错的,叫nav_router. 项目地址是:https://github.com/fluttercandies/nav_route ...

  7. 洛谷 题解 P2296 【寻找道路】

    Problem P2296 [寻找道路] solution 首先声明,这题我用了spfa,而: 关于spfa:它死了. 杀手: NOI 2018−T1 出题人 感谢出题人,没有卡spfa 用时: 20 ...

  8. windows程序设计03_读取utf8文件

    这里用到的读取utf8文件的思路特别朴素.先把utf8文件按char读取到内存里.因为utf8是变长的,为了处理方便,在内存里把char转化成wchar_t,这样一个字符就是一个wchar_t.把ut ...

  9. 基于python的种子搜索网站(三)项目部署

    项目部署过程 系统要求:ubuntu 16.04(或以上) 环境搭建和配置,必须严格按照以下步骤来安装部署!如有问题可以咨询(weixin:java2048) 安装部分 安装nginx sudo ap ...

  10. 【HTML5】296- 重新复习 HTML5 的 5大存储方式

    点击上方"前端自习课"关注,学习起来~ 一.介绍 在 HTML5 规范之前,存储主要是用 cookies . cookies 的缺点有: 在请求头上带着数据: 大小是 4k 之内: ...