linux-sed

【sed基础使用】
[root@localhost test]#man sed
SED(1)                           User Commands                          SED(1)
NAME
       sed - stream editor for filtering and transforming text
  【命令参数格式】
SYNOPSIS
        sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION
       Sed  is  a stream editor.  A stream editor is used to perform basic text transformations on an input stream
       (a file or input from a pipeline).  While in some ways similar to an editor which  permits  scripted  edits
       (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But
       it is sed’s ability to filter text in a pipeline which particularly distinguishes it from  other  types  of
       editors.
【常用选项:】
          -n∶使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到萤幕上。但如果加上 -n 参数 后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
        -e∶直接在指令列模式上进行 sed 的动作编辑;
        -f∶直接将 sed 的动作写在一个档案内, -f filename 则可以执行 filename 内的sed 动作;
        -r∶sed 的动作支援的是延伸型正规表示法的语法。(预设是基础正规表示法语法)
        -i∶直接修改读取的档案内容,而不是由萤幕输出。
        -e∶直接在指令列模式上进行 sed 的动作编辑;
        -f∶直接将 sed 的动作写在一个档案内, -f filename 则可以执行 filename 内的sed 动作;
        -r∶sed 的动作支援的是延伸型正规表示法的语法。(预设是基础正规表示法语法)
        -i∶直接修改读取的档案内容,而不是由萤幕输出。       
【常用命令:】
         a   ∶新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
        c   ∶取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
        d   ∶删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
         i   ∶插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
         p  ∶列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
         s  ∶取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法
        c   ∶取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
        d   ∶删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
         i   ∶插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
         p  ∶列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
         s  ∶取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法
【案例】
【案例文本的内容】
[root@localhost test]# cat test 
111
222
333
444
555
666
777
$$
【显示操作】
[root@localhost test]# sed -n '1p' test #显示第1行 
111
[root@localhost test]# sed -n '$p' test #显示最后1行
$$
[root@localhost test]# sed -n '1,3p' test   #显示第1行到第3行
111
222
333
[root@localhost test]# sed -n '6,$p' test #显示第6行到最后1行
666
777
$$
[root@localhost test]# sed -n '/3/p' test 
333
[root@localhost test]# sed -n '/\$/p' test 

【删除操作】
[root@localhost test]# sed '1d' test #删除第1行 
222
333
444
...
[root@localhost test]# sed '$d' test #删除最后1行
111
222
333
...
[root@localhost test]# sed '1,3d' test #删除第1行到第3行
444
555
666
...
[root@localhost test]# sed '6,$d' test  #删除第6行到最后1行
111
222
...
【使用模式进行查询】
[root@localhost test]# sed -n '/3/p' test  #查询包括关键字3所在所有行
333
[root@localhost test]# sed -n '/\$/p' test #查询包括关键字$所在所有行,使用反斜线\屏蔽特殊含义
$$
【增加一行或多行字符串】
[root@localhost test]# sed '1a add first content' test #第一行后增加字符串" add first content"
111
add first content
222
...

[root@localhost test]# sed '1,3a add first content' test    #第一行到第三行后增加字符串"drink tea"
[root@localhost test]# sed '1,3a add first content\nadd send content' test      #第一行后增加多行,使用换行符\n
【代替一行或多行】
[root@localhost test]# sed '1c change' test    #第一行代替为change
change
222
...
【替换操作】
格式:sed  's#要替换的字符串#新的字符串# g'   (要替换的字符串可以用正则表达式)
[root@localhost test]# sed 's#111#xxx#g' test #替换111为xxx
xxx
222
...
【插入操作】
 [root@localhost test]# sed -i '$a yyy' test            #在文件ab中最后一行直接输入"bye"
【删除匹配行操作】
sed -i '/匹配字符串/d'  filename  (注:若匹配字符串是变量,则需要“”,而不是‘’。记得好像是)
替换匹配行中的某个字符串
 sed -i '/匹配字符串/s/替换源字符串/替换目标字符串/g' filename
[root@localhost test]# sed -i '/111/d' test 






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值