码迷,mamicode.com
首页 > 其他好文 > 详细

sed命令

时间:2016-09-27 13:28:54      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

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 i\000000 test.txt

3)在最后一行插入

# sed -i $ i\endendend0000000 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

 

sed命令

标签:

原文地址:http://www.cnblogs.com/love3556/p/5912274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!