sed是一个很强大的文件处理工具,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取等特定工作

格式:sed [option] [command] [file]

常用命令:

a   ∶新增
        c   ∶取代
        d   ∶删除
         i   ∶插入
         p  ∶列印
         s  ∶取代

选项:

  -i∶直接修改读取的档案内容,而不是由萤幕输出。

    -n∶使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到萤幕上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。

1,sed '1d' ghostwu.com   d代表删除 d前面的数字代表删除第一行,该命令不会修改文件本身

ghostwu@dev:~/linux/sed$ cat -n ghostwu.txt
this is ghostwu
how are you
hod old are you
and you
fine thank you
come with me!!!
ghostwu@dev:~/linux/sed$ sed '1d' ghostwu.txt
how are you
hod old are you
and you
fine thank you
come with me!!!
ghostwu@dev:~/linux/sed$ cat ghostwu.txt
this is ghostwu
how are you
hod old are you
and you
fine thank you
come with me!!!

2,删除最后一行,$代表最后一行

ghostwu@dev:~/linux/sed$ cat ghostwu.txt
this is ghostwu
how are you
hod old are you
and you
fine thank you
come with me!!!
ghostwu@dev:~/linux/sed$ sed '$d' ghostwu.txt
this is ghostwu
how are you
hod old are you
and you
fine thank you

3,删除第一行到第二行

ghostwu@dev:~/linux/sed$ cat ghostwu.txt
this is ghostwu
how are you
hod old are you
and you
fine thank you
come with me!!!
ghostwu@dev:~/linux/sed$ sed '1,2d' ghostwu.txt
hod old are you
and you
fine thank you
come with me!!!

4,删除第二行到最后一行

ghostwu@dev:~/linux/sed$ sed '2,$d' ghostwu.txt
this is ghostwu

5,查找包含'you'的行,  /you/ 这是正则表达式, p是打印,要跟n结合起来用

ghostwu@dev:~/linux/sed$ cat ghostwu.txt
this is ghostwu
how are you
hod old are you
and you
fine thank you
come with me!!!
ghostwu@dev:~/linux/sed$ sed -n '/you/p' ghostwu.txt
how are you
hod old are you
and you
fine thank you

6,匹配$符号结尾的行

$符号在正则表达式表示行尾,所以要用\ 转义

ghostwu@dev:~/linux/sed$ sed -n '/\$/p' ghostwu.txt
$

7,在第一行后面,增加一行“你好啊"

ghostwu@dev:~/linux/sed$ cat ghostwu.txt
this is ghostwu
how are you
hod old are you
and you
fine thank you
come with me!!!
how much do you have
$
oh, is it?
yes
ghostwu@dev:~/linux/sed$ sed '1a 你好啊' ghostwu.txt
this is ghostwu
你好啊
how are you
hod old are you
and you
fine thank you
come with me!!!
how much do you have
$
oh, is it?
yes

8,在第一行和第二行的后面,增加一行

ghostwu@dev:~/linux/sed$ sed '1,2a learning how to use sed command' ghostwu.txt this is ghostwu
learning how to use sed command
how are you
learning how to use sed command
fine thank you

9,也可以增加多行

ghostwu@dev:~/linux/sed$ cat ghostwu.txt
this is ghostwu
how are you
fine thank you
ghostwu@dev:~/linux/sed$ sed '1a 你好啊\n很高兴认识你' ghostwu.txt
this is ghostwu
你好啊
很高兴认识你
how are you
fine thank you

10, c为替换操作,数字的意思,跟上面的a一样,代表行

ghostwu@dev:~/linux/sed$ cat ghostwu.txt
this is ghostwu
how are you
fine thank you
ghostwu@dev:~/linux/sed$ sed '1,2c hey guy' ghostwu.txt
hey guy
fine thank you
ghostwu@dev:~/linux/sed$ sed '1c hey guy' ghostwu.txt
hey guy
how are you
fine thank you

11, s:替换匹配到的内容, s:替换开始 /is/ 匹配包含is的行   g:全部替换

ghostwu@dev:~/linux/sed$ cat ghostwu.txt
this is ghostwu
how are you
fine thank you
ghostwu@dev:~/linux/sed$ sed 's/is/IS/' ghostwu.txt
thIS is ghostwu
how are you
fine thank you
ghostwu@dev:~/linux/sed$ sed 's/is/IS/g' ghostwu.txt
thIS IS ghostwu
how are you
fine thank you
ghostwu@dev:~/linux/sed$ cat ghostwu.txt
this is ghostwu
how are you
fine thank you

12、-i :修改,插入文件,会影响文件的内容,在最后一行,插入bye bye

ghostwu@dev:~/linux/sed$ cat ghostwu.txt
this is ghostwu
how are you
fine thank you
ghostwu@dev:~/linux/sed$ sed -i '$a bye bye' ghostwu.txt
ghostwu@dev:~/linux/sed$ cat ghostwu.txt
this is ghostwu
how are you
fine thank you
bye bye

13,在1-3行,每一行的后面都插入一行数字

ghostwu@dev:~/linux/sed$ cat ghostwu.txt
this is ghostwu
how are you
fine thank you
bye bye
ghostwu@dev:~/linux/sed$ sed -i '1,3a 123457' ghostwu.txt
ghostwu@dev:~/linux/sed$ cat ghostwu.txt
this is ghostwu how are you fine thank you bye bye

Linux常用基本命令:三剑客命令之-sed的更多相关文章

  1. linux常用文本编缉命令(strings/sed/awk/cut)

    一.strings strings--读出文件中的所有字符串 二.sed--文本编缉 类型 命令 命令说明 字符串替换 sed -i 's/str_reg/str_rep/' filename 将文件 ...

  2. Linux常用基本命令(less)

    转: Linux常用基本命令(less) LESS:跟more命令的功能类似,都是用于分页显示内容,但是他的性能比more更高,功能比more更丰富,他读取文件是按需加载 格式: less [opti ...

  3. Linux 常用基本命令及应用技巧

    需要pdf 版 联系我 我的文件中有目录一.Linux 的常用基本命令................................................................. ...

  4. ## 本篇文章对linux常用的一些命令做一下总结,如有需要补充以及不懂得地方,请在下方留言 适合于linux初学者,以及对命令掌握不牢的用来备忘

    本篇文章对linux常用的一些命令做一下总结,如有需要补充以及不懂得地方,请在下方留言 适合于linux初学者,以及对命令掌握不牢的用来备忘一,磁盘管理1.显示当前目录位置 pwd2.切换目录 cd ...

  5. Linux 常用的压缩命令有 gzip 和 zip

    Linux 常用的压缩命令有 gzip 和 zip,两种压缩包的结尾不同:zip 压缩的后文件是 *.zip ,而 gzip 压缩后的文件 *.gz 相应的解压缩命令则是 gunzip 和 unzip ...

  6. Linux常用基本命令:三剑客命令之-awk基础用法

    awk是一个超级强大的文本格式化处理工具,他与grep, sed命令被成为linux 三剑客命令 三剑客命令的特点: grep:只要用来匹配和查找文本 sed: 编辑匹配到文本 awk: 格式化文本, ...

  7. linux 三剑客命令(grep,sed ,awk)

    grep 命令 :强大的文本’搜索’工具    1.grep   -n   'word'  file_name 在file_name文件中找到word所在的所有行并显示.-n 为显示行号.     2 ...

  8. LINUX常用配置及命令

    一.   Fedora系统配置 1.      [设置网卡IP] 步骤如下: 1)     用root用户登陆,打开/etc/sysconfig/network-scripts/ifcfg-eth0文 ...

  9. Linux常用的基础命令总结

    man 查看英文命令帮助   可以看作--help 拷贝目录的命令cp -a  包含所有 ls -a 显示所有文件包括隐藏文件  -ld ls -F 过滤目录文件(给不同类型文件结尾加上不同的符号) ...

随机推荐

  1. 3 week work—Grid Layout

    HTML: <div class="wrapper"> //建立一个三列轨道网格. <div class="one">One</d ...

  2. Eclipse 中 Java 代码报版本错误的问题

    今天碰到了eclipse中的代码一直报错,后来发现是编译环境的问题,记录一下. 项目build path的JDK版本是开发的时候编译器需要使用到的,例如,如果用的JDK1.4就不能使用泛型. 而jav ...

  3. 如何用impress.js写有逼格的ppt

    概述 这是我学习课程impress让你的内容"舞"起来而做的总结和练手. 你可以点这里在线预览我做的ppt 注意:等加载完了之后,点击空格键翻页! 简化模板 下面是一个简化的模板 ...

  4. Unicode 字符串排序规则(一):如何确定单个字符的顺序

    一.一个具体的例子引发的问题 当今是国际化的时代,多种语言可能同时显示在屏幕上.比如一个人可能喜欢听华语歌.英文歌.韩文歌和日语歌,又比如他的联系人中有中国人.英国人.日本人.韩国人以及有英文名字的中 ...

  5. c++中的.hpp文件

    http://blog.chinaunix.net/uid-24118190-id-75239.html hpp,其实质就是将.cpp的实现代码混入.h头文件当中,定义与实现都包含在同一文件,则该类的 ...

  6. 从CSR产品名录看公司发展历程和业务方向

    产品型号                                                                           主要功能 BC3系列(例BC31A223, ...

  7. requestAnimFrame 动画的使用方法

    //requestAnimFrame 封装,可以兼容所有浏览器 window.requestAnimFrame = (function(){ return window.requestAnimatio ...

  8. Linux学习笔记之五————Linux常用命令之用户、权限管理

    一.引言 用户是Unix/Linux系统工作中重要的一环,用户管理包括用户与组账号的管理. 在Unix/Linux系统中,不论是由本机或是远程登录系统,每个系统都必须拥有一个账号,并且对于不同的系统资 ...

  9. django--form相关

    简单用法: Django提供 Form组件:            1. 定义规则                from django.forms import Form               ...

  10. .net core内部分享ppt

    Microsoft .NET 自 2002 年发行 v1.0 以来,已经过了近 14 个年头,在这 14 年里面,.NET 日渐成熟并成为 Microsoft 的重要开发平台之一,只要是在 Windo ...