Sed学习笔记2

sed有以下几种常用的工作模式:
-n,静态处理命令,不涉及的行不打印;
-e,默认选项,把sed后的输入当做命令来执行;
-i,会直接修改文件;
-r,扩展正则表达式。
直接讲述实例:

cat sedExample.txt#输出文件查看一下内容,第一行是空行

today is May
today date is 24th May
today is Wednesday
what day is it today
what a beautiful
it is the last day of the word

打印指定的行(含有May字符的行)

cat sedExample.txt |sed -n '/May/p'
today is May
today date is 24th May

打印指定的行,加上-e,结果也一样,所以一般情况下可以省略-e

cat sedExample.txt |sed -ne '/May/p'
today is May
today date is 24th May

删除指定的行(含有May字符的行)

cat sedExample.txt |sed '/May/d'

today is Wednesday
what day is it today
what a beautiful
it is the last day of the word

删除以what开头的行

cat sedExample.txt |sed '/^what/d'

today is May
today date is 24th May
today is Wednesday
it is the last day of the word

先删除空行,再删除以May结尾的行,这个时候-e是不能省略的

cat sedExample.txt |sed -e '/^$/d' -e '/May/d'
today is Wednesday
what day is it today
what a beautiful
it is the last day of the word

或者,对命令加上{}

cat sedExample.txt |sed  '{/^$/d; /May/d}'
today is Wednesday
what day is it today
what a beautiful
it is the last day of the word

在today前添加this

cat sedExample.txt |sed  's/today/this &/'

this today is May
this today date is 24th May
this today is Wednesday
what day is it this today
what a beautiful
it is the last day of the word

在May后添加 Liunx

cat sedExample.txt |sed  's/May/& Linux/'

today is May Linux
today date is 24th May Linux
today is Wednesday
what day is it today
what a beautiful
it is the last day of the word

在有what行的行首,加上#

cat sedExample.txt |sed  '/what/s/^/#/'

today is May
today date is 24th May
today is Wednesday
#what day is it today
#what a beautiful
it is the last day of the word

在有day单词行的行尾部,加上#

cat sedExample.txt |sed  '/\<day\>/s/$/#/'

today is May
today date is 24th May
today is Wednesday
what day is it today#
what a beautiful
it is the last day of the word#

注意,以上的操作均未改动原文件

cat sedExample.txt

today is May
today date is 24th May
today is Wednesday
what day is it today
what a beautiful
it is the last day of the word

如果要改动原文件需要加上-i

此外sed还有一个比较6的功能,就是批量更改文件名
列出文件下的.txt文件

ls *.txt
dna.txt  geoMatrix.txt  geox.txt  gg.txt  sedExample.txt  x.txt

将geo...txt文件,改成GEO...txt

ls *.txt|sed -n 's/\(geo\(.*\)\)/mv \1 GEO\2/p'
mv geoMatrix.txt GEOMatrix.txt
mv geox.txt GEOx.txt

ls *.txt|sed -n 's/\(geo\(.*\)\)/mv \1 GEO\2/p'|bash
ls *.txt
dna.txt  GEOMatrix.txt  GEOx.txt  gg.txt  sedExample.txt  x.txt
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、前言 我们都知道,在Linux中一切皆文件,比如配置文件,日志文件,启动文件等等。如果我们相对这些文件进行一些...
    以七v为书阅读 1,690评论 0 5
  • 基础命令 主要的命令和快捷键 Linux系统命令由三部分组成:cmd + [options]+[operation...
    485b1aca799e阅读 1,239评论 0 0
  • 这篇是当初看完Chinaunix论坛的帖子“抛砖引玉----翻译加注sed1line”的笔记,最近无聊从Evern...
    magic5650阅读 1,296评论 0 4
  • 简介 sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(p...
    699b979fc4da阅读 1,147评论 0 1
  • 本文承接之前写的三十分钟学会AWK一文,在学习完AWK之后,趁热打铁又学习了一下SED,不得不说这两个工具真的堪称...
    mylxsw阅读 4,527评论 3 74

友情链接更多精彩内容