标签:char 模糊 line sed的用法 div 注意 netmask ike 行存储
sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。然后读入下行,执行下一个循环。如果没有使诸如‘D’的特殊命令,那会在两个循环之间清空模式空间,但不会清空保留空间。这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出或者-i进行修改。
功能:主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等
参考:http://www.gnu.org/software/sed/manual/sed.html
sed
[option]...
‘script‘
inputfile...
-n:不输出模式空间内容到屏幕,即不自动打印 -e:多点编辑 -f: /PATH/SCRIPT_FILE从指定文件中读取编辑脚本 -r:支持使用扩展正则表达式 -i:直接将处理的结果写入文件 -i.bak:备份文件并原处编辑
(1) 不给地址:对全文进行处理 (2) 单地址: #: 指定的行,$:最后一行 /pattern/:被此处模式所能够匹配到的每一行 (3) 地址范围: #,# #,+# /pat1/,/pat2/ #,/pat1/ (4) ~:步进 1~2 奇数行 2~2 偶数行
d: 删除模式空间匹配的行,并立即启用下一轮循环 p: 打印当前模式空间内容,追加到默认输出之后 a: [\]text在指定行后面追加文本,支持使用\n实现多行追加 i: [\]text在行前面插入文本 c: [\]text替换行为单行或多行文本 w: /path/file保存模式匹配的行至指定文件 r: /path/file读取指定文件的文本至模式空间中匹配到的行后 =: 为模式空间中的行打印行号 !: 模式空间中匹配行取反处理
s/// 查找替换,支持使用其它分隔符,s@@@,s###
替换标记:
g:行内全局替换 p:显示替换成功的行 w:/PATH/FILE将替换成功的行保存至文件中 sed命令的指示信息 p print 输出信息 i insert 输入信息,在指定信息前面插入新的信息 a append 附加信息,在指定信息后面附加新的信息 d delete 删除指定信息 s substitute 替换信息 s###g(全局替换) c 替换修改指定的一整行信息
1 行号 3p # p print 打印输出
2 连续的行号 3,5p # 输出第3行到第5行 ,表示序列
3 过滤找出包含xxx的行 /oldboy/p # 按照匹配字符串的方式模式查找
4 过滤范围 //,//p
[root@oldboyedu-lnb ~]# sed -n ‘3p‘ oldboy.txt test
[root@oldboyedu-lnb ~]# sed -n ‘3,6p‘ oldboy.txt test I like badminton ball ,billiard ball and chinese chess! my blog is http: blog.51cto.com
[root@oldboyedu-lnb ~]# sed -n ‘$p‘ oldboy.txt 11111111818181818 [root@oldboyedu-lnb ~]# sed -n ‘15,$p‘ oldboy.txt hehe $m 11111111818181818
[root@oldboyedu-lnb ~]# ifconfig eth0|sed -n ‘2p‘ inet 10.0.0.200 netmask 255.255.255.0 broadcast 10.0.0.255
[root@oldboyedu ~]# sed -n ‘1p;3p‘ person.txt 101,oldboy,CEO 103,Alex,COO
根据内容信息,输出多行内容(连续)
[root@oldboyedu ~]# sed -n ‘/oldboy/,/Alex/p‘ person.txt 101,oldboy,CEO 102,zhaoyao,CTO 103,Alex,COO
[root@oldboyedu ~]# sed -n ‘/oldboy/p;/Alex/p‘ person.txt 101,oldboy,CEO 103,Alex,COO 106,oldboy,CIO
[root@oldboyedu ~]# sed ‘1i100,oldgirl,UFO‘ person.txt 100,oldgirl,UFO 101,oldboy,CEO 102,zhaoyao,CTO 103,Alex,COO 104,yy,CFO 105,feixue,CIO 106,oldboy,CIO
[root@oldboyedu ~]# sed ‘$a108,oldgirl,UFO‘ person.txt 100,oldgirl,UFO 101,oldboy,CEO 102,zhaoyao,CTO 103,Alex,COO 104,yy,CFO 105,feixue,CIO 106,oldboy,CIO 108,oldgirl,UFO
sed ‘3aoldboy.txt‘ person.txt
sed ‘2ioldboy.txt‘ person.txt
sed -e ‘/oldboy/ioldgirl‘ -e ‘/oldboy/aolddog‘ person.txt
-rw-r--r--. 1 root root 0 Oct 10 02:10 oldboy01.txt -rw-r--r--. 1 root root 0 Oct 10 02:10 oldboy02.txt -rw-r--r--. 1 root root 0 Oct 10 02:10 oldboy03.txt -rw-r--r--. 1 root root 0 Oct 10 02:10 oldboy04.txt -rw-r--r--. 1 root root 0 Oct 10 02:10 oldboy05.txt -rw-r--r--. 1 root root 0 Oct 10 02:10 oldboy06.txt -rw-r--r--. 1 root root 0 Oct 10 02:10 oldboy07.txt -rw-r--r--. 1 root root 0 Oct 10 02:10 oldboy08.txt -rw-r--r--. 1 root root 0 Oct 10 02:10 oldboy09.txt -rw-r--r--. 1 root root 0 Oct 10 02:10 oldboy10.txt
[root@oldboyedu oldboy]# ls oldboy*.txt|sed -r ‘s#(.*)txt#mv &(表示oldboy*.txt) \1(表 示oldboy*)jpg#g‘ mv oldboy01.txt oldboy01.jpg mv oldboy02.txt oldboy02.jpg mv oldboy03.txt oldboy03.jpg mv oldboy04.txt oldboy04.jpg mv oldboy05.txt oldboy05.jpg mv oldboy06.txt oldboy06.jpg mv oldboy07.txt oldboy07.jpg mv oldboy08.txt oldboy08.jpg mv oldboy09.txt oldboy09.jpg mv oldboy10.txt oldboy10.jpg mv oldboy.txt oldboy.jpg
语法格式:
[root@oldboyedu-lnb ~]# sed -n ‘/http/p‘ oldboy.txt my blog is http: blog.51cto.com our site is http:www.lizhenya.com
语法格式:
[root@oldboyedu-lnb ~]# sed -nr ‘/http|lizhenya/p‘ oldboy.txt I am lizhenya teacher! my blog is http: blog.51cto.com our site is http:www.lizhenya.com lizhenyalizhenyalizhenyaaaaaaaaa
语法格式:
sed ‘/字符串/,/字符串/p‘ file
[root@oldboyedu-lnb ~]# sed -n ‘/test/,/not/p‘ oldboy.txt test I like badminton ball ,billiard ball and chinese chess! my blog is http: blog.51cto.com our site is http:www.lizhenya.com my qq num is 593528156 aaaa, not 572891888887.
工作中匹配日志时间区间
[root@oldboyedu-lnb ~]# sed -n ‘/Aug 6 08:09:26/,/Aug 7 08:09:23/p‘ /var/log/messages
区间模糊匹配的注意事项:
sed 增加内容 临时增加内容 只输出到屏幕上 不会更改源文件
vi vim echo cat
语法格式:
sed ‘3a 插入的内容‘ file
[root@oldboyedu-lnb ~]# sed ‘3i alex‘ oldboy.txt.bak I am lizhenya teacher! I teach linux. hehe test
[root@oldboyedu-lnb ~]# sed ‘3a prot=9777‘ oldboy.txt.bak I am lizhenya teacher! I teach linux. test prot=9777
[root@oldboyedu-lnb ~]# sed ‘3,5a alex‘ oldboy.txt.bak I am lizhenya teacher! I teach linux. test alex alex I like badminton ball ,billiard ball and chinese chess! alex
[root@oldboyedu-lnb ~]# sed ‘3c oldboyedu.com‘ oldboy.txt.bak I am lizhenya teacher! I teach linux. oldboyedu.com
[root@oldboyedu-lnb ~]# sed -n ‘/^SELINUX=/c SELINUX=enforcing‘ /etc/selinux/config SELINUX=enforcing [root@oldboyedu-lnb ~]# sed -in ‘/^SELINUX=/c SELINUX=enforcing‘ /etc/selinux/config [root@oldboyedu-lnb ~]# sed -n ‘/^SELINUX=/c SELINUX=disabled‘ /etc/selinux/config SELINUX=disabled
[root@oldboyedu-lnb ~]# sed -n ‘8c SELINUX=disabled‘ /etc/selinux/config SELINUX=disabled [root@oldboyedu-lnb ~]# sed -in ‘8c SELINUX=disabled‘ /etc/selinux/config [root@oldboyedu-lnb ~]# sed ‘a oldboyedu.com‘ oldboy.txt.bak 了解 I am lizhenya teacher! oldboyedu.com I teach linux. oldboyedu.com test oldboyedu.com
语法结构:
sed ‘/模式/动作‘ file # 不加参数只是临时删除 不影响源文件
[root@oldboyedu-lnb ~]# sed ‘3d‘ oldboy.txt.bak I am lizhenya teacher! I teach linux.
[root@oldboyedu-lnb ~]# sed ‘1,3d‘ oldboy.txt.bak [root@oldboyedu-lnb ~]# sed ‘1,$d‘ oldboy.txt.bak
[root@oldboyedu-lnb ~]# sed ‘/linux/d‘ oldboy.txt.bak
[root@oldboyedu-lnb ~]# sed ‘/test/,/our/d‘ oldboy.txt.bak I am lizhenya teacher! I teach linux. my qq num is 593528156
语法格式:
sed ‘s#被替换内容#替换成什么#g‘ file
s### 替换文件中每行的第一个test
[root@oldboyedu-lnb ~]# sed ‘s#test#oldboyedu.com#‘ oldboy.txt.bak I am lizhenya teacher! I teach linux. oldboyedu.com test test test
[root@oldboyedu-lnb ~]# sed ‘s#test#oldboyedu.com#g‘ oldboy.txt.bak
[root@oldboyedu-lnb ~]# sed -i ‘s#test#oldboyedu.com#g‘ oldboy.txt.bak
[root@oldboyedu-lnb ~]# sed -n ‘3,5p‘ oldboy.txt.bak|sed ‘s#[a-z]##g‘ . . . . I ., ! [root@oldboyedu-lnb ~]# sed -n ‘3,5s#[a-z]##gp‘ oldboy.txt.bak . . . . I ., !
[root@oldboyedu-lnb ~]# head -1 passwd |sed ‘s//root/test/g‘ sed: -e expression #1, char 9: unknown option to `s‘ [root@oldboyedu-lnb ~]# head -1 passwd |sed ‘s/\/root/test/g‘ root:x:0:0:root:test:/bin/bash [root@oldboyedu-lnb ~]# [root@oldboyedu-lnb ~]# head -1 passwd |sed ‘s#/root#test#g‘ root:x:0:0:root:test:/bin/bash [root@oldboyedu-lnb ~]# head -1 passwd |sed ‘s#\#hehe#test#g‘ root:x:test0:0:root:/root:/bin/bash
语法格式: 先保护 后引用
调用()内容的前面和后面可以加任意的内容
[root@oldboyedu-lnb ~]# echo 123456|sed -r ‘s#(.*)#\1#g‘ 123456 [root@oldboyedu-lnb ~]# echo 123456|sed -r ‘s#(.*)#<<\1>>#g‘ <<123456>> [root@oldboyedu-lnb ~]# echo 123456|sed -r ‘s#(.*)#hehe\1hehe#g‘ hehe123456hehe [root@oldboyedu-lnb ~]# echo 123456|sed -r ‘s#(.*)#1111\11111#g‘ 11111234561111 [root@oldboyedu-lnb ~]# echo 123456|sed -r ‘s#([0-9]{3})#\1#g‘ 123456 [root@oldboyedu-lnb ~]# echo 123456|sed -r ‘s#([0-9]{3})#<\1>#g‘ <123><456> [root@oldboyedu-lnb ~]# echo 123456|grep ‘[0-9]{3}‘ -o [root@oldboyedu-lnb ~]# echo 123456|egrep ‘[0-9]{3}‘ -o 123 456
[root@oldboyedu-lnb ~]# ifconfig eth0|sed -n ‘2p‘|sed -r ‘s#^.*inet (.*) netmask.*$#\1#g‘ 10.0.0.200 [root@oldboyedu-lnb ~]# ifconfig eth0|sed -n ‘2p‘|sed -r ‘s#(^.*inet )(.*) netmask.*$#\1#g‘ inet [root@oldboyedu-lnb ~]# ifconfig eth0|sed –n ‘2p‘|sed-r‘s#(^.*inet )(.*)( netmask.*$)#\3#g‘ netmask 255.255.255.0 broadcast 10.0.0.255 [root@oldboyedu-lnb ~]# ifconfig eth0|sed -n ‘2p‘|sed -r ‘s#(^.*inet )(.*)( netmask.*$)#\2#g‘ 10.0.0.200
[root@oldboyedu-lnb ~]# ip add|grep -n . [root@oldboyedu-lnb ~]# ip add|sed -nr ‘9s#^.*net (.*)/24.*$#\1#gp‘ 10.0.0.200 [root@oldboyedu-lnb~]# ip add |sed -n ‘9p‘ |sed -r ‘s#^.*brd(.*)scop.*$#\1#g‘ 10.0.0.255 [root@oldboyedu-lnb~]# ip add |sed -nr ‘9s#^.*brd(.*)scop.*$#\1#gp‘ 10.0.0.255
[root@oldboyedu-lnb~]# cat /etc/passwd |sed -n ‘3,7p‘ > /tmp/passwd [root@oldboyedu-lnb~]# cat /tmp/passwd daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
good good study, day day up.
Come here, change yourself.
[root@oldboyedu-lnb~]# sed ‘3i\ngood good study, day day up.\nCome here,change yourself.‘ /tmp/passwd daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin ngood good study, day day up. Come here,change yourself. lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@oldboyedu-lnb~]# sed -n ‘s#/sbin/nologin#/bin/bash#gp‘ /tmp/passwd daemon:x:2:2:daemon:/sbin:/bin/bash adm:x:3:4:adm:/var/adm:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/bin/bash
[root@oldboyedu-lnb~]# sed ‘/\<bin\>/d‘ /tmp/passwd1 daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
增删改查替换
查看:
-n 取消默认输出 -r 支持扩展正则 -i 更改源文件 sed -n ‘3p‘ file 1,3p 3,$p sed -n ‘/root/p‘ file sed -n ‘/root/,/alex/p‘ file
增加替换:
-i -a -c sed ‘3i 新的内容‘ file sed ‘3a 追加内容‘ file sed ‘3c 替换整行内容‘ file
组合使用:
sed ‘3,5c oldboyedu‘ file sed ‘/root/c oldboyedu‘ file 修改源文件 -i参数 sed的替换: sed ‘s#替换谁#替换的内容#g‘ file cat file |sed ‘s###g‘ sed ‘3,5p‘ file|sed ‘s###g‘ 组合使用 sed -n ‘3,5s###gp‘ file sed的后向引用 sed -r ‘s#()#\1#g‘
PS: Linux中大部分的命令 -V 都是查看版本信息
grep的参数 -n 显示匹配到内容的行号 -v 取反 -o 显示匹配过程 -i 不区分大小写 -E 支持扩展正则 -R 递归查询文件内容 -w 过滤单词 -c 统计字符串在文件中出现的次数 -A 显示当前查找到的内容往下n行 了解 -B 显示当前查找到的内容往上n行 了解 -C 显示当前查找到的内容上下各n行 了解
[root@oldboyedu-lnb ~]# grep oldboy 1.txt alexoldboylidao oldboy oldboyedu.com [root@oldboyedu-lnb ~]# grep -w oldboy 1.txt oldboy [root@oldboyedu-lnb ~]# grep linux -A3 oldboy.txt.bak I teach linux. oldboyedu.com oldboyedu.com oldboyedu.com oldboyedu.com I like badminton ball oldboyedu.com,billiard ball and chinese chess! [root@oldboyedu-lnb ~]# grep like -B3 oldboy.txt.bak I teach linux. oldboyedu.com oldboyedu.com oldboyedu.com oldboyedu.com I like badminton ball oldboyedu.com,billiard ball and chinese chess! [root@oldboyedu-lnb ~]# grep linux -C1 oldboy.txt.bak I am lizhenya teacher! I teach linux. oldboyedu.com oldboyedu.com oldboyedu.com oldboyedu.com
标签:char 模糊 line sed的用法 div 注意 netmask ike 行存储
原文地址:https://www.cnblogs.com/strugger-0316/p/13622461.html