原文引用:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html

[root@www ~]# sed [-nefr] [动作]
选项与参数:
-n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到终端上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
-e :直接在命令列模式上进行 sed 的动作编辑;
-f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作;
-r :sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
-i :直接修改读取的文件内容,而不是输出到终端。
动作说明: [n1[,n2]]function
n1, n2 :不见得会存在,一般代表『选择进行动作的行数』,举例来说,如果我的动作是需要在 10 到 20 行之间进行的,则『 10,20[动作行为] 』
function:
a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p :列印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是

对输出进行行删除

[root@local yang]# nl passwd |sed '2,33d'
1 root:x:0:0:root:/root:/bin/bash
34 yang:x:500:500:yang:/home/yang:/bin/bash
35 www:x:501:501::/home/www:/sbin/nologin
36 mysql:x:502:502::/home

替换行

[root@local yang]# nl passwd |sed '2,5c 1111'
1 root:x:0:0:root:/root:/bin/bash
1111
6 www:x:501:501::/home/www:/sbin/nologin
7 mysql:x:502:502::/home/m

列出指定行内容

[root@local yang]# nl passwd |sed -n '2,3p'
2 saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
3 postfix:x:89:89::/var/spool/postfix:/sbin/nologin

搜索关键字所在行

[root@local yang]# nl passwd |sed -n '/root/p' #p 打印 d 删除.....
1 root:x:0:0:root:/root:/bin/bash

搜索并执行命令

[root@local yang]# nl passwd |sed -n '/root/{s/root/aaa/;p;q}'  #  :q执行一次就退出
1 aaa:x:0:0:root:/root:/bin/bash

搜索与替换

[yang@local ~]$ ifconfig eth0|grep 'inet addr'|sed 's/^.*addr://g' |sed 's/Bcast.*$//g'

对文件的搜索与替换

[root@local yang]# nl passwd
1 root:x:0:0:root:/root:/bin/bash
2 saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/---/login
3 postfix:x:89:89::/var/spool/postfix:/---/login [root@local yang]# sed -n 's/---/sbin/g;p' passwd     #替换预览
root:x:0:0:root:/root:/bin/bash
saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/login
postfix:x:89:89::/var/spool/postfix:/sbin/login [root@local yang]# sed -i 's/---/sbin/g' passwd    #替换 sed -i 's/---/sbin/g;p' passwd 加p将会将输出追加到文件内容
[root@local yang]# nl passwd
1 root:x:0:0:root:/root:/bin/bash
2 saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/login
3 postfix:x:89:89::/var/spool/postfix:/sbin/login

linux sed使用的更多相关文章

  1. Linux sed 替换第一次出现的字符串

    /********************************************************************************* * Linux sed 替换第一次 ...

  2. linux sed命令参数及用法详解

    linux sed命令参数及用法详解 http://blog.csdn.net/namecyf/article/details/7336308 1. Sed简介 sed 是一种在线编辑器,它一次处理一 ...

  3. linux sed 批量替换多个文件中的字符

    格式: sed -i "s/查找字段/替换字段/g" `grep 查找字段 -rl 路径` linux sed 批量替换多个文件中的字符串 sed -i "s/oldst ...

  4. [转帖]linux sed命令

    linux sed命令就是这么简单 https://www.cnblogs.com/wangqiguo/p/6718512.html 用到的最多的就是一个sed -i 's/nn/mm/' 的命令了. ...

  5. 理解linux sed命令

    理解linux sed命令(2010-02-27 18:21:20) 标签:linuxshellsed替换 分类:革命本钱 1. Sed简介sed是一种在线编辑器,它一次处理一行内容.处理时,把当 前 ...

  6. learn Linux sed command

    learn Linux sed command 一.参考文档: . sed命令详解 http://qifuguang.me/2015/09/21/sed%E5%91%BD%E4%BB%A4%E8%AF ...

  7. 【转】linux sed命令

    转自:linux sed命令就是这么简单 参考:Linux三大剑客之sed:https://blog.csdn.net/solaraceboy/article/details/79272344 阅读目 ...

  8. Linux sed Examples--转载

    原文地址:https://www.systemcodegeeks.com/shell-scripting/bash/linux-sed-examples/?ref=dzone Sed is basic ...

  9. Linux sed命令常用方法

    sed也成stream editor,流编辑器,是Linux上常用的文本处理工具. 通用格式:sed  行范围  模式/RegExp/  文件 模式: d 删除 p 打印符合条件的行 a \strin ...

  10. Linux sed 和 awk的用法

    sed用法: 原文链接:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html sed是一个很好的文件处理工具,本身是一个管 ...

随机推荐

  1. 利用TEA算法进行数据加密

    TEA(Tiny Encryption Algorithm)是一种小型的对称加密解密算法,最初是由剑桥计算机实验室的 David Wheeler 和 Roger Needham 在 1994 年设计. ...

  2. android Service开机启动及debug

    开机启动一个service需要做的工作如下: 1.开发一个receiver用于接收系统广播: public class BootReceiver extends BroadcastReceiver { ...

  3. 轻松学习RSA加密算法原理 (转)

    轻松学习RSA加密算法原理 (转) http://blog.csdn.net/q376420785/article/details/8557266 http://www.ruanyifeng.com/ ...

  4. BZOJ_1202_狡猾的商人_(并查集)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1202 n 个月的账单,共 m 组数据,每一组数据包括 x , y , t ,表示从 x 月到 ...

  5. 常用SQL语句(增删查改、合并统计、模糊搜索)

    转自:http://www.cnblogs.com/ljianhui/archive/2012/08/13/2695906.html 常用SQL语句 首行当然是最基本的增删查改啦,其中最重要的是查. ...

  6. win8 mysqlzip install

    1. 下载MySQL Community Server 5.6.142. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下.3. 添加环境变量 变量名:MYSQL_HOME 变量值: ...

  7. Linux创建新用户以及useradd adduser的区别

    从阿里云那弄了个机子玩玩,系统用的是Ubuntu12.04.刚等上去时候是用root登录的,首先想到的就是创建一个用户. 使用 useradd myname 发现/home目录下没有myname的家目 ...

  8. 搭建Git远程仓库(也就是Git服务器,不用再连Github了)

    github上一般托管的代码都是公开的,任何人都可以查看.复制下载等,而私有的项目则需要付费.所以可以自己搭建一个git服务,自己人用. 首先安装git: sudo apt-get install g ...

  9. hibernate数据库方言

    hibernate数据库方言 mark一下 RDBMS 方言 DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect ...

  10. HDOJ/HDU 1088 Write a simple HTML Browser(HTML字符串)

    Problem Description If you ever tried to read a html document on a Macintosh, you know how hard it i ...