zoukankan      html  css  js  c++  java
  • sed

      • sed -e 4a estfour text  

      #在test文件第四行的下一行插入testfour

      [oracle@aaaserver ~]$ sed -e 4a estfour test
      one
      two
      three
      four
      testfour
      five
      six

      [oracle@aaaserver ~]$ sed '3a testthree' test
      one
      two
      three
      testthree
      four
      five
      six

      • sed -e 4i estfour test

      #在test文件第四行的上一行插入testfour

      [oracle@aaaserver ~]$ sed -e 4i estfour test
      one
      two
      three
      testfour
      four
      five
      six

    $表示文本的最后一行,a表示增加,在test文件的最后一行写入 over:(-i表示直接写入文本,慎用)

    sed -i  '$a  over'   test

    [root@joe ~]# sed -i '$a over' test
    [root@joe ~]# more test
    1
    2
    3
    4
    5
    6
    over

    • nl /etc/passwd

    #列出行号显示/etc/passwd的内容

    [oracle@aaaserver ~]$ nl /etc/passwd
    1 root:x:0:0:root:/root:/bin/bash
    2 bin:x:1:1:bin:/bin:/sbin/nologin
    3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
    4 adm:x:3:4:adm:/var/adm:/sbin/nologin
    5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    6 sync:x:5:0:sync:/sbin:/bin/sync
    7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    8 halt:x:7:0:halt:/sbin:/sbin/halt
    9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10 operator:x:11:0:operator:/root:/sbin/nologin

    • nl  etc/passwd |sed '3,7d'

    #去除3到7行内容显示/etc/passwd

    [oracle@aaaserver ~]$ nl /etc/passwd |sed '3,7d'
    1 root:x:0:0:root:/root:/bin/bash
    2 bin:x:1:1:bin:/bin:/sbin/nologin
    8 halt:x:7:0:halt:/sbin:/sbin/halt
    9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10 operator:x:11:0:operator:/root:/sbin/nologin
    11 games:x:12:100:games:/usr/games:/sbin/nologin

    • nl installActions2019-02-19_05-36-24PM.log |sed '5,$d'

    #将第5行至最后一行去除后显示

    [oracle@aaaserver logs]$ nl installActions2019-02-19_05-36-24PM.log |sed '5,$d'
    1 INFO: Using paramFile: /u02/software/database/install/oraparam.ini
    2 INFO: 
    3 INFO: 
    4 INFO: Checking Temp space: must be greater than 120 MB. Actual 267035 MB Passed

    • sed '3c testthree'

    #将第三行替换位testthree

    [oracle@aaaserver ~]$ sed '3c testthree' test 
    one
    two
    testthree
    four
    five
    six

    nl test |sed -n '3,6p'

    #仅显示3到6行的内容

    [oracle@aaaserver ~]$ nl test |sed -n '3,6p'
    3 three
    4 four
    5 five
    6 six

    nl alert_aaaserver.log|sed -n '/rror/p'

    #只显示xx文件中带有rror字符的行[

    oracle@aaaserver trace]$ nl alert_aaaserver.log |sed -n '/rror/p'
    4846 Fatal NI connect error 12170.
    4853 Tns error struct:
    4866 Fatal NI connect error 12170.
    4873 Tns error struct:
    4886 Fatal NI connect error 12170.
    4893 Tns error struct:
    4916 Fatal NI connect error 12170.
    4923 Tns error struct:
    4936 Fatal NI connect error 12170.
    4943 Tns error struct:
    4966 Fatal NI connect error 12170.
    4973 Tns error struct:
    5131 Fatal NI connect error 12170.
    5138 Tns error struct:

    [oracle@aaaserver trace]$ ifconfig|grep inet
    inet 192.168.0.186 netmask 255.255.255.0 broadcast 192.168.0.255
    inet6 fe80::4ccb:d7a5:7e0c:9a34 prefixlen 64 scopeid 0x20<link>
    inet 127.0.0.1 netmask 255.0.0.0
    inet6 ::1 prefixlen 128 scopeid 0x10<host>
    inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255

    sed 's/要被取代的字串/新的字串/g'

    [oracle@aaaserver trace]$ ifconfig|grep inet|sed -n 's/inet //p'
    192.168.0.186 netmask 255.255.255.0 broadcast 192.168.0.255
    127.0.0.1 netmask 255.0.0.0
    192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255

    [oracle@aaaserver trace]$ ifconfig|grep inet|sed -n -e 's/inet //p'|sed 's/netmask.*//'
    192.168.0.186 
    127.0.0.1 
    192.168.122.1

    sed -e 执行多个命令(或引号‘’内用分号;分开要执行命令),例:只显示test文件中包含two和six的行

    [oracle@ctp ~]$ sed -n -e '/two/p' -e '/six/p' test
    two
    six

    [oracle@ctp ~]$ sed -n '/two/p;/six/p' test
    two
    six

    • a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
    • c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
    • d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
    • i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
    • p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~

    由於 $ 代表的是最后一行,而 a 的动作是新增,因此该文件最后新增 # This is a test

    sed 的 -i 选项可以直接修改文件内容,

  • 相关阅读:
    JavaScript快速入门-DOM对象
    JavaScript快速入门-ECMAScript对象介绍
    JavaScript快速入门-ECMAScript函数
    JavaScript快速入门-ECMAScript运算符
    [转] boost::function用法详解
    [转] [翻译]图解boost::bind
    [转] Git 分支
    [转] 多线程下变量-gcc原子操作 __sync_fetch_and_add等
    [转] Boost智能指针——scoped_ptr
    [转] linux下的c/c++调试器gdb
  • 原文地址:https://www.cnblogs.com/joeshang/p/10503223.html
Copyright © 2011-2022 走看看