Last login: Tue Aug 15 19:46:48 2017 from10.0.0.1
[root@oldboyedu ~]# cat>person.txt<<EOF
> 101,oldboy,CEO
> 102,zhangyao,CTO
> 103,Alex,COO
> 104,yy,CFO
> 105,feixue,CIO
> EOF
[root@oldboyedu ~]# sed -n "2p" person.txt
102,zhangyao,CTO
[root@oldboyedu ~]# sed -n ‘$p‘ person.txt
105,feixue,CIO
[root@oldboyedu ~]# sed -n ‘1,4p‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
[root@oldboyedu ~]# sed -n ‘/oldboy|yy/p‘ person.txt
[root@oldboyedu ~]# sed -nr ‘/oldboy|yy/p‘ person.txt
101,oldboy,CEO
104,yy,CFO
[root@oldboyedu ~]# sed -n ‘/101/,/104/p‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
[root@oldboyedu ~]# sed -n ‘/Alex/,/yy/p‘ person.txt
103,Alex,COO
104,yy,CFO
[root@oldboyedu ~]# sed -n ‘1p;3p;5p‘ person.txt
101,oldboy,CEO
103,Alex,COO
105,feixue,CIO
[root@oldboyedu ~]# cat >>person.txt <<EOF
> 106,lidao,UFO
> 107,bingbing,CEO
> EOF
[root@oldboyedu ~]# sed ‘2a oldboyedu.m‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
oldboyedu.m
103,Alex,COO
104,yy,CFO
105,feixue,CIO
106,lidao,UFO
107,bingbing,CEO
[root@oldboyedu ~]# sed ‘$a 106,lidao\n107,bingbing,CEO‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
106,lidao
107,bingbing,CEO
[root@oldboyedu ~]# sed ‘$a 106\n107‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
106,lidao,UFO
107,bingbing,CEO
106
107
[root@oldboyedu ~]# sed ‘/oldboy/d‘ person.txt
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
106,lidao,UFO
107,bingbing,CEO
[root@oldboyedu ~]# grep -v "oldboy" person.txt
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
106,lidao,UFO
107,bingbing,CEO
[root@oldboyedu ~]# awk ‘!/oldboy/‘ person.txt
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
106,lidao,UFO
107,bingbing,CEO
[root@oldboyedu~]# sed -n ‘/oldboy/!p‘ person.txt
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
106,lidao,UFO
107,bingbing,CEO
[root@oldboyedu ~]# sed ‘s#[0-9]#oldboy#‘ person.txt
oldboy01,oldboy,CEO
oldboy02,zhangyao,CTO
oldboy03,Alex,COO
oldboy04,yy,CFO
oldboy05,feixue,CIO
oldboy06,lidao,UFO
oldboy07,bingbing,CEO
[root@oldboyedu ~]# sed ‘s#[0-9]#oldboy#g‘ person.txt
oldboyoldboyoldboy,oldboy,CEO
oldboyoldboyoldboy,zhangyao,CTO
oldboyoldboyoldboy,Alex,COO
oldboyoldboyoldboy,yy,CFO
oldboyoldboyoldboy,feixue,CIO
oldboyoldboyoldboy,lidao,UFO
oldboyoldboyoldboy,bingbing,CEO
[root@oldboyedu-39-nb~]# sed -r ‘s#(oldboy)+#101#‘ person.txt
[root@oldboyedu-39-nb~]# sed -r ‘s#(oldboy)+#101#‘ person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
[root@oldboyedu-39-nb~]# sed -ir.bak ‘s#(oldboy)+#101#‘ person.txt
[root@oldboyedu-39-nb~]# cat person.txt
oldboyoldboyoldboy,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
[root@oldboyedu-39-nb~]# ls -l person.txt*
-rw-r--r-- 1root root 86 Aug 16 12:05 person.txt
-rw-r--r-- 1root root 86 Aug 16 12:03 person.txtr.bak
[root@oldboyedu-39-nb~]#
[root@oldboyedu-39-nb~]# sed -ri.bak ‘s#(oldboy)+#101#‘ person.txt
[root@oldboyedu-39-nb~]# cat person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,COO
104,yy,CFO
105,feixue,CIO
[root@oldboyedu-39-nb~]# ls -l person.txt*
-rw-r--r-- 1root root 71 Aug 16 12:06 person.txt
-rw-r--r-- 1root root 86 Aug 16 12:05 person.txt.bak
本文出自 “linux基础” 博客,请务必保留此出处http://13131277.blog.51cto.com/13121277/1956767
原文地址:http://13131277.blog.51cto.com/13121277/1956767