码迷,mamicode.com
首页 > 系统相关 > 详细

linux sed命令详解

时间:2017-03-28 23:47:39      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:流处理   mount   link   sdn   并且   自动换行   ash   nbsp   表达   

Linux环境:Ubuntu16.04

sed命令

http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html 

http://blog.csdn.net/yiqingnian28/article/details/23133043

 正则表达式

http://blog.csdn.net/wzzfeitian/article/details/8842371

sed -help 查看sed的用法

-n, --quiet, --silent suppress automatic printing of pattern space
阻止默认输出(显示)模版空间里的数据,此时只输出p命令指定的内容。
-e script, --expression=script add the script to the commands to be executed 将指定脚本SCRIPT中的命令增加到处理输入文件的命令组中
-f script-file, --file=script-file add the contents of script-file to the commands to be executed
将指定脚本SCRIPT-FILE中的命令增加到处理输入文件的命令组中。
--follow-symlinks follow symlinks when processing in place 
只在支持符号连接的系统中,并且同时用选项-i时才有作用:若指定的输入文件是一个符号连接,SED会处理符号连接的目标文件,并保持连接。默认SED会断开连接,使符号连接的目标文件不会被修改。

-i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if SUFFIX supplied)
将文件放在in-place里编辑处理。GNU是通过创建一个临时文件,并将SED的输出送到此文件,而不是送到标准输出的方式来实现。应用于命令:=、a、c、i、l、p中,但同时还可用命令w或W及将输出文件指定为/dev/stdout,实现SED的输出也同时送到标准输出。
此选项已隐含了另一选项-s。i与SUFFIX之间不能有空格
一个文件处理完时,SED将创建的临时文件名换成输出文件的原名(即被处理的输入文件名)。若指定有后缀SUFFIX,则会先将旧文件换名成SUFFIX后缀而形成备份;否则旧文件被覆盖而无备份。
换名规则是:若SUFFIX不含有星号(*),将SUFFIX作为后缀直接追加在旧文件名后;否则,SUFFIX中的每个星号(*)都被旧文件名取代。好处是:还允许为备份文件指定前缀,而不仅仅是后缀,甚至将可备份放到另一个目录中(目录必须已存在)
-l N, --line-length=N specify the desired line-wrap length for the `l command --posix disable all GNU extensions. 
指定l(L)命令自动换行的长度为N,0表示不对长行进行自动换行处理,默认是70。

-r, --regexp-extended use extended regular expressions in the script.
用扩展正则表达式规则来解释匹配模版。

-s, --separate consider files as separate rather than as a single continuous long stream.
默认SED将命令行上指定的多个输入文件看作成一个连续的长数据流处理。用这个GNU的扩展选项,SED将会看作各个独立的文件来处理,
但注意的是:指定的地址范围不能跨度到其他文件;显示的行号都是相对于各自文件的首行;字符$会指示每个文件的最后一行;来自R命令的调用文件对每个输入文件都会重新执行一次。

-u, --unbuffered load minimal amounts of data from the input files and flush the output buffers more often
对输入和输出都根据实际,尽可能小地进行缓冲处理,或不缓冲。这对SED命令的输入是来自类似于命令tail –f,而你又想尽可能快地看到处理后的输出,是特别有用的。

-z, --null-data separate lines by NUL characters
--help display this help and exit
显示sed参数信息后退出。
--version output version information and exit
显示sed版本信息后退出。

截取第五行的数据:

sed -n 5p /etc/passwd 

多个空格替换成一个空格:

root@ubuntu:/home/jianeng# tail test.txt 
How are   you!
Fine! Thank   you! 
root@ubuntu:
/home/jianeng# sed ‘s/\ */\ /g‘ test.txt How are you! Fine! Thank you!

删除man后面的数据:

root@ubuntu:/home/jianeng# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
root@ubuntu:
/home/jianeng# head /etc/passwd | grep ‘man:‘ | sed ‘s/:.*//g‘ man

 

linux sed命令详解

标签:流处理   mount   link   sdn   并且   自动换行   ash   nbsp   表达   

原文地址:http://www.cnblogs.com/jianeng/p/6637511.html

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