sed

来源:互联网 发布:淘宝客服刚去要学什么 编辑:程序博客网 时间:2024/06/05 11:02

sed

sed处理流程

  1. 读入新的一行内容到缓存空间(模式空间);
  2. 从指定的操作指令中取出第一条指令,判断是否匹配pattern;
  3. 如果不匹配,则忽略后续的编辑命令,回到第2步继续取出下一条指令;
  4. 如果匹配,则针对缓存的行执行后续的编辑命令;完成后,回到第2步继续取出下一条指令;
  5. 当所有指令都应用之后,输出缓存行的内容;回到第1步继续读入下一行内容;
  6. 当所有行都处理完之后,结束;

sed命令参数

  • s 替换
  • p 打印
  • g 全局
  • -n 抑制默认输出
  • a 追加
  • c 行替换
  • d 删除
  • i 插入
  • r 读文件
  • w 写文件

实例

替换
  • -s 同时替换不同的字符串

-

[root@server6 ~]# sed 's/root/ROOT/;s/:/;/' passwd ROOT;x:0:0:root:/root:/bin/bashbin;x:1:1:bin:/bin:/sbin/nologindaemon;x:2:2:daemon:/sbin:/sbin/nologinadm;x:3:4:adm:/var/adm:/sbin/nologinlp;x:4:7:lp:/var/spool/lpd:/sbin/nologinsync;x:5:0:sync:/sbin:/bin/syncshutdown;x:6:0:shutdown:/sbin:/sbin/shutdownhalt;x:7:0:halt:/sbin:/sbin/haltmail;x:8:12:mail:/var/spool/mail:/sbin/nologin

-

[root@server6 ~]# sed 's/root/ROOT/g;s/:/;/g' passwd ROOT;x;0;0;ROOT;/ROOT;/bin/bashbin;x;1;1;bin;/bin;/sbin/nologindaemon;x;2;2;daemon;/sbin;/sbin/nologinadm;x;3;4;adm;/var/adm;/sbin/nologinlp;x;4;7;lp;/var/spool/lpd;/sbin/nologinsync;x;5;0;sync;/sbin;/bin/syncshutdown;x;6;0;shutdown;/sbin;/sbin/shutdownhalt;x;7;0;halt;/sbin;/sbin/haltmail;x;8;12;mail;/var/spool/mail;/sbin/nologin

-

[root@server6 ~]# sed -n 's/root/ROOT/p' passwd ROOT:x:0:0:root:/root:/bin/bash[root@server6 ~]# sed -n 's/root/ROOT/gp' passwd ROOT:x:0:0:ROOT:/ROOT:/bin/bash[root@server6 ~]# sed -n 's/root/ROOT/pg' passwd ROOT:x:0:0:ROOT:/ROOT:/bin/bash
-s空格和Tab和空行
  • 源文本

-

假设[ ]代表一个空格假设[\t]代表一个Tab[root@server6 ~]# cat index.html         "meizhiyuan"    : 789779,        "coffe"         : 2378263,        "wanghaha"      : 239274,事实上,肉眼凡胎看不出哪个是空格,哪个是Tab,转换为真实格式[][][][][\t]"meizhiyuan"[\t]:[]789779,[\t]"coffe"[][\t]:[]2378263,[][][][][\t]"wanghaha"[][][][][][]:[]239274,
  • 删除行首空格

-

[root@server6 ~]# sed 's/^ //g' index.html         "meizhiyuan"    : 789779,        "coffe"         : 2378263,        "wanghaha"      : 239274,
  • 去除所有空格

-

[root@server6 ~]# sed 's/ //g' index.html         "meizhiyuan"    :789779,        "coffe" :2378263,        "wanghaha":239274,
  • 去除所有tab

-

[root@server6 ~]# sed 's/[\t]//g' index.html     "meizhiyuan": 789779,"coffe" : 2378263,    "wanghaha"      : 239274,
  • 去除所有的空格和Tab

-

[root@server6 ~]# sed 's/[[:space:]]//g' index.html "meizhiyuan":789779,"coffe":2378263,"wanghaha":239274,[root@server6 ~]# sed 's/[ ]*[\t]*//g' index.html "meizhiyuan":789779,"coffe":2378263,"wanghaha":239274,
  • 删除空行

-

[root@server6 ~]# cat index.html         "meizhiyuan"    : 789779,        "coffe"         : 2378263,        "wanghaha"      : 239274,[root@server6 ~]# sed '/^$/d' index.html         "meizhiyuan"    : 789779,        "coffe"         : 2378263,        "wanghaha"      : 239274,
-d 删除
  • 原文本

-

[root@server6 ~]# cat  index.html         "meizhiyuan"    : 789779,        "coffe"         : 2378263,        "wanghaha"      : 239274,
  • 删除最后一行

-

[root@server6 ~]# sed '$d' index.html         "meizhiyuan"    : 789779,        "coffe"         : 2378263,
  • 删除第一行

-

[root@server6 ~]# sed '1d' index.html         "coffe"         : 2378263,        "wanghaha"      : 239274,
  • 删除第二行

-

[root@server6 ~]# sed '2d' index.html         "meizhiyuan"    : 789779,        "wanghaha"      : 239274,
  • 删除包含‘meizhi’所在行

-

[root@server6 ~]# sed '/meizhi/d' index.html         "coffe"         : 2378263,        "wanghaha"      : 239274,
  • 删除2到最后一行

-

[root@server6 ~]# sed -e '2,$d' index.html         "meizhiyuan"    : 789779,
= l p

-

[root@server6 ~]# sed '=' index.html 1        "meizhiyuan"    : 789779,2        "coffe"         : 2378263,3        "wanghaha"      : 239274,

-

[root@server6 ~]# sed 'l' index.html     \t"meizhiyuan"\t: 789779,$        "meizhiyuan"    : 789779,\t"coffe" \t: 2378263,$        "coffe"         : 2378263,    \t"wanghaha"      : 239274,$        "wanghaha"      : 239274,

-

[root@server6 ~]# sed 'p' index.html         "meizhiyuan"    : 789779,        "meizhiyuan"    : 789779,        "coffe"         : 2378263,        "coffe"         : 2378263,        "wanghaha"      : 239274,        "wanghaha"      : 239274,
添加
  • 原文件

-

[root@server6 ~]# cat index.html         "meizhiyuan"    : 789779,        "coffe"         : 2378263,        "wanghaha"      : 239274,
  • 行首添加

-

[root@server6 ~]# sed 's/^/head&/g' index.html head            "meizhiyuan"    : 789779,head    "coffe"         : 2378263,head            "wanghaha"      : 239274,
  • 行尾添加

-

[root@server6 ~]# sed 's/$/&tail/g' index.html         "meizhiyuan"    : 789779,tail        "coffe"         : 2378263,tail        "w

原创粉丝点击