• sed命令


    1  sed命令 帮助

    Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
    
      -n, --quiet, --silent    ###安静模式,只显示被sed处理过的行         
                     suppress automatic printing of pattern space
      -e script, --expression=script ###默认选项,不用写,在命令行操作
                     add the script to the commands to be executed
      -f script-file, --file=script-file  ### 将sed操作写在一个文件里
                     add the contents of script-file to the commands to be executed
      --follow-symlinks
                     follow symlinks when processing in place; hard links
                     will still be broken.
      -i[SUFFIX], --in-place[=SUFFIX]   ### 插入当前行的上一行
                     edit files in place (makes backup if extension supplied).
                     The default operation mode is to break symbolic and hard links.
                     This can be changed with --follow-symlinks and --copy.
      -c, --copy
                     use copy instead of rename when shuffling files in -i mode.
                     While this will avoid breaking links (symbolic or hard), the
                     resulting editing operation is not atomic.  This is rarely
                     the desired mode; --follow-symlinks is usually enough, and
                     it is both faster and more secure.
      -l N, --line-length=N
                     specify the desired line-wrap length for the `l' command
      --posix
                     disable all GNU extensions.
      -r, --regexp-extended   ###支持扩展正则表达式
                     use extended regular expressions in the script.
      -s, --separate ###表示搜索
                     consider files as separate rather than as a single continuous
                     long stream.
      -u, --unbuffered
                     load minimal amounts of data from the input files and flush
                     the output buffers more often
          --help     display this help and exit
          --version  output version information and exit

    例子:

       1)删除2,5行

        

    # cat -n  /etc/passwd |sed '2,5d'
         1    root:x:0:0:root:/root:/bin/bash
         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    uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

    2) 添加 在第2行后添加 hello world

    # cat -n /etc/passwd|sed '2a hello world' 
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin
    hello world

    3)添加2行,在第1行后面添加两行 (以续航符号 \, 按Enter键继续输入,以'结束)

    ]# cat -n /etc/passwd|sed '1a this is frist line 
    > this is second line  '
         1    root:x:0:0:root:/root:/bin/bash
    this is frist line 
    this is second line  

    4) 替换,以2,5行替换成 “我 是 谁”

    # cat -n /etc/passwd |sed '2,5c 我是谁! '
         1    root:x:0:0:root:/root:/bin/bash
    我是谁! 
         6    sync:x:5:0:sync:/sbin:/bin/sync

    5)显示特定行,只显示5-7行,注意n和p同时使用

    # cat -n /etc/passwd |sed -n '5,7p'
         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

    6)显示出ifconfig中的ip地址

    # ifconfig eth0 | grep 'inet addr' |sed 's/^.*addr://g' |sed 's/Bcast.*$//g'
    192.168.0.35 
    # ifconfig eth0 | grep 'inet addr' |awk -F[:] '{print $2}'  | awk '{print $1}'
    192.168.0.35

    ----------------------------------------------------------------------------------------------------------------------------------------------------------

    2. sed的基础用法

      1)删除行首空格

          

    # sed 's/^[ ]* //g' test.txt 
    zero line
    first line
    # sed 's/^ * //g' test.txt
    zero line
    first line
    # sed 's/^[[:space:]]* //g' test.txt

    2)在第一行插入文本

    # sed -i '1 i00000' test.txt

    3)在最后一行插入

    # sed -i '$ iendendend0000000' test.txt 

    4)在匹配行前插入

    # sed -i '/zero/ i the 3 line' test.txt

    5)在匹配行后插入

    sed -i '/the end/ a "this is the end line"' test.txt 

    6) 删除空行及行首空格和#号的行

    # grep -v  ^# test.txt | sed /^[[:space:]]*$/d | sed '/^$/d'
  • 相关阅读:
    Gym
    数学公式头文件
    除法取模(比赛常用)
    ACM-ICPC 2017 Asia Urumqi A. Coins【期望dp】
    P1494 小Z的袜子 【普通莫队】
    Codeforces Round #642 (Div. 3) E—K-periodic Garland dp
    luogu P4568 [JLOI2011]飞行路线 最短路Dijkstra+dp
    luogu P2015 二叉苹果树 树形dp
    luogu P1462 通往奥格瑞玛的道路 二分+spfa
    luogu P1879 [USACO06NOV]Corn Fields G 状态压缩dp
  • 原文地址:https://www.cnblogs.com/love3556/p/5912274.html
Copyright © 2020-2023  润新知