Linux文件内容处理-sed

sed命令是一个管道命令,具有替换,删除,新增, 选取特定行等功能。下面介绍一下其的简单用法:
源文件:

     1  "Open Source" is a good mechanism to develop programs.$
     2  apple is my favorite food.$
     3  Football game is not use feet only.$
     4  this dress doesn't fit me.$
     5  However, this dress is about $ 3183 dollars.^M$
     6  GNU is free air not free beer.^M$
     7  Her hair is very beauty.^M$
     8  I can't finish the test.^M$
     9  Oh! The soup taste good.^M$
    10  motorcycle is cheap than car.$
    11  This window is clear.$
    12  the symbol '*' is represented as start.$
    13  Oh!^IMy god!$
    14  The gd software is a library for drafting programs.^M$
    15  You are the best is mean you are the no. 1.$
    16  The world <Happy> is the same with "glad".$
    17  I like dog.$
    18  google is the best tools for search keyword.$
    19  goooooogle yes!$
    20  go! go! Let's go.$
    21  # I am VBird$
    22  $
  • 删除2-5行:nl regular_express.txt | sed '2,5d',这里2-5行都被删除。d代表删除 sed后面接的动作必须以两个单引号'括住
[root@192 Desktop]# nl regular_express.txt | sed '2,5d'
     1  "Open Source" is a good mechanism to develop programs.
     6  GNU is free air not free beer.
     7  Her hair is very beauty.
     8  I can't finish the test.
     9  Oh! The soup taste good.
    10  motorcycle is cheap than car.
    11  This window is clear.
    12  the symbol '*' is represented as start.
    13  Oh! My god!
    14  The gd software is a library for drafting programs.
    15  You are the best is mean you are the no. 1.
    16  The world <Happy> is the same with "glad".
    17  I like dog.
    18  google is the best tools for search keyword.
    19  goooooogle yes!
    20  go! go! Let's go.
    21  # I am VBird
  • 删除第3到最后一行:nl regular_express.txt | sed '3,$d',其中$代表最后一行
[root@192 Desktop]# nl regular_express.txt | sed '3,$d'
     1  "Open Source" is a good mechanism to develop programs.
     2  apple is my favorite food.
  • 在第二行后加上(即加在第三行)drink teanl regular_express.txt | sed '2a drink tea',如果在第二行前加上的话:nl regular_express.txt | sed '2i drink tea' 和删除类似,2是行数,a代表第二行后添加,i代表第二行前添加
[root@192 Desktop]# nl regular_express.txt | sed '2a drink tea'
     1  "Open Source" is a good mechanism to develop programs.
     2  apple is my favorite food.
drink tea
     3  Football game is not use feet only.
     4  this dress doesn't fit me.
[root@192 Desktop]# nl regular_express.txt | sed '2i drink tea'
     1  "Open Source" is a good mechanism to develop programs.
drink tea
     2  apple is my favorite food.
     3  Football game is not use feet only.
     4  this dress doesn't fit me.
  • 添加多行 ,比如在第二行后面添加两行:nl regular_express.txt | sed '2a drink tea or ......\ drink beer ?' 注意其中有一个反斜杠\来进行换行。
[root@192 Desktop]# nl regular_express.txt | sed '2a drink tea or ...... \
> drink beer ?'
     1  "Open Source" is a good mechanism to develop programs.
     2  apple is my favorite food.
drink tea or ...... 
drink beer ?
     3  Football game is not use feet only.
     4  this dress doesn't fit me.
     5  However, this dress is about $ 3183 dollars.
     6  GNU is free air not free beer.
     ...
  • 将第2-5行的内容替换为 NO 2-5 number:nl regular_express.txt | sed '2,5c No 2-5 number', 其中的c代表替换
[root@192 Desktop]# nl regular_express.txt  | sed '2,5c No 2-5 number'
     1  "Open Source" is a good mechanism to develop programs.
No 2-5 number
     6  GNU is free air not free beer.
     7  Her hair is very beauty.
     8  I can't finish the test.
     9  Oh! The soup taste good.
    10  motorcycle is cheap than car.
...
  • 仅列出 第5~7行:nl regular_express.txt | sed -n '3,7p' -n代表安静模式,在一般sed的用法中,所有来自STDIN的数据一般都会列出到屏幕上。但如果加上-n参数后,则只有经过sed特殊处理的那一行(或者操作)才会被列出来。
    我们对比一下两个结果:
[root@192 Desktop]# nl regular_express.txt | sed  '3,7p'
     1  "Open Source" is a good mechanism to develop programs.
     2  apple is my favorite food.
     3  Football game is not use feet only.
     3  Football game is not use feet only.
     4  this dress doesn't fit me.
     4  this dress doesn't fit me.
     5  However, this dress is about $ 3183 dollars.
     5  However, this dress is about $ 3183 dollars.
     6  GNU is free air not free beer.
     6  GNU is free air not free beer.
     7  Her hair is very beauty.
     7  Her hair is very beauty.
     8  I can't finish the test.
     9  Oh! The soup taste good.
    10  motorcycle is cheap than car.
    11  This window is clear.
    12  the symbol '*' is represented as start.
    13  Oh! My god!
    14  The gd software is a library for drafting programs.
    15  You are the best is mean you are the no. 1.
    16  The world <Happy> is the same with "glad".
    17  I like dog.
    18  google is the best tools for search keyword.
    19  goooooogle yes!
    20  go! go! Let's go.
    21  # I am VBird

加-n的时候,3-7行每一行都重复了一次。下面是不加-n的结果。只有被sed操作的行被显示出来。

[root@192 Desktop]# nl regular_express.txt | sed -n '3,7p'
     3  Football game is not use feet only.
     4  this dress doesn't fit me.
     5  However, this dress is about $ 3183 dollars.
     6  GNU is free air not free beer.
     7  Her hair is very beauty.
  • 将[.]替换为[*]:nl regular_express.txt | sed 's/.//g'* (sed 's/要被替换的字符串/新的字符串/g' g代表全局替换)
[root@192 Desktop]# nl regular_express.txt | sed 's/\./***/g'
     1  "Open Source" is a good mechanism to develop programs***
     2  apple is my favorite food***
     3  Football game is not use feet only***
     4  this dress doesn't fit me***
     5  However, this dress is about $ 3183 dollars***
     6  GNU is free air not free beer***
     7  Her hair is very beauty***
     8  I can't finish the test***
     9  Oh! The soup taste good***
    10  motorcycle is cheap than car***
    11  This window is clear***
    12  the symbol '*' is represented as start***
    13  Oh! My god!
    14  The gd software is a library for drafting programs***
    15  You are the best is mean you are the no*** 1***
    16  The world <Happy> is the same with "glad"***
    17  I like dog***
    18  google is the best tools for search keyword***
    19  goooooogle yes!
    20  go! go! Let's go***
    21  # I am VBird
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容

  • Linux sed命令是利用script来处理文本文件。sed可依照script的指令,来处理、编辑文本文件。Se...
    姜淑均阅读 688评论 1 2
  • Shell作为操作系统的外壳,为用户提供了使用操作系统的接口。Shell和核心是命令行提示符,它允许输入文本命令,...
    种豆瓜阅读 229评论 0 0
  • 本文承接之前写的三十分钟学会AWK一文,在学习完AWK之后,趁热打铁又学习了一下SED,不得不说这两个工具真的堪称...
    mylxsw阅读 4,361评论 3 74
  • 简介 sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(p...
    人在码途阅读 164评论 0 0
  • sed工具简介 用法:sed [-nefri] 动作参数:-n:使用安静模式(Silent)模式,一般在sed的...
    Bloo_m阅读 980评论 0 1