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. Floyd多源最短路

    可以对每一个顶点使用Dijkstra算法求多源最短路. 这里我们来介绍另一种解法:Floyd Floyd算法的主要思想是迭代.每次迭代会朝着答案更近一步. 首先定义一个二维数组Dk[i][j](k初始 ...

  2. 记一次安装VS2015后启动失败的修复过程

    安装过程没有提示任何问题,然而启动vs时提示没有安装 .Net Framework 4.6,那就安装吧,但是安装 4.6 时却提示 Windows Moudle Installer 服务没有启动,于是 ...

  3. rabbitMq 初步

    RabbitMQ的工作原理 它的基本结构 组成部分说明如下: Broker:消息队列服务进程,此进程包括两个部分:Exchange和Queue. Exchange:消息队列交换机,按一定的规则将消息路 ...

  4. vue高级组件之provide / inject

    转载:https://blog.csdn.net/Garrettzxd/article/details/81407199 在vue中不同组件通信方式如下 1.父子组件,通过prop 2.非父子组件,通 ...

  5. 了解甚少的GNU C的__attribute__ 机制

    平时忙着赶项目,很多东西都是不求甚解,当工作中遇到的一些比较刁钻的问题时,才发现自己和那些大牛的 差距---内功.熟练码农和码神的最大区别估计就是内功是否深厚了.在自我反思的过程中,也要逐渐的积累一些 ...

  6. kafka之consumer参数auto.offset.reset 0.10+

    https://blog.csdn.net/dingding_ting/article/details/84862776 https://blog.csdn.net/xianpanjia4616/ar ...

  7. Java:Cookie实现记住用户名、密码

    package com.gamecenter.api.util; import java.io.IOException; import java.io.PrintWriter; import java ...

  8. Go语言总结

  9. centos下如何使用sendmail发送邮件

    最近在实施服务端日志监控脚本,需要对异常情况发送邮件通知相关责任人,记录下centos通过sendmail发送邮件的配置过程. 一.安装sendmail与mail 1.安装sendmail:  1) ...

  10. Android快速实现二维码扫描--Zxing

    Android中二维码扫描的最常用库是zxing和zbar,zxing项目地址为https://github.com/zxing/zxing,目前还有多个人在维护.zbar主要用C来写的,对速度有要求 ...