ifconfig 取出IP
[root@zhangguokui ~]# ifconfig eth0|sed -rn '2s#^.*dr:(.*)Bc.*$#\1#gp'
192.168.144.129
-
^^oldboy代表以oldboy开头的行 -
$oldboy$以oldboy为结尾 -
^$空行 -
.匹配任意一个字符(不能空行) -
\转义,取消特殊含义 -
*匹配前一个字符 -
.*匹配所有内容 -
^.*匹配任何多个字符开头 -
.*$匹配任意多个字符结尾 -
[abc]匹配[]集合内任意一个字符a或b或c -
[^abc]匹配不包含^后的任意字符,a或b或c,这里的^表示对[abc] 取反
[root@zhangguokui test]# cat -n oldboy.txt
1 I am oldboy teacher!
2 i teach linux
3
4 i like badminton ball ,billiard ball and chinese chess!
5 our site is http://www.oldboyedu.com
6 my qq num is 49000448.
7
8 not 4900000448.
9 my god , i am not oldbey,but OLDBOY!
^m输出m开头的所有行且打行号[root@zhangguokui test]# grep -n "^m" oldboy.txt 6:my qq num is 49000448. 9:my god , i am not oldbey,but OLDBOY!
m$输出以m结尾的行[root@zhangguokui test]# grep -n "m$" oldboy.txt 5:our site is http://www.oldboyedu.com
^$显示空行[root@zhangguokui test]# grep "^$" -n oldboy.txt 3: 7:
.匹配任意字符(即不显示空行)[root@zhangguokui test]# grep "." -n oldboy.txt 1:I am oldboy teacher! 2:i teach linux 4:i like badminton ball ,billiard ball and chinese chess! 5:our site is http://www.oldboyedu.com 6:my qq num is 49000448. 8:not 4900000448. 9:my god , i am not oldbey,but OLDBOY!
\.$显示以点.为结尾的行,注意用撬棍\,不用撬棍就变成任意字符结尾[root@zhangguokui test]# grep "\.$" -n oldboy.txt 6:my qq num is 49000448. 8:not 4900000448.
0*输出连续出现0及0次以上数字0的内容(*表示前一个字符连续出现0次)[root@zhangguokui test]# grep "0*" -n oldboy.txt 1:I am oldboy teacher! ... 9:my god , i am not oldbey,but OLDBOY!
.*所有内容,包括空行[root@zhangguokui test]# grep ".*" -n oldboy.txt 1:I am oldboy teacher! 2:i teach linux 3: ... 9:my god , i am not oldbey,but OLDBOY!
[]匹配所有大写[root@zhangguokui test]# grep [A-Z] -n oldboy.txt 1:I am oldboy teacher! 9:my god , i am not oldbey,but OLDBOY!
0+0出现1次或多次[root@zhangguokui test]# egrep '0+' oldboy.txt my qq num is 49000448. not 4900000448.
|或者[root@zhangguokui test]# egrep '3307|3308' /etc/services opsession-prxy 3307/tcp # OP Session Proxy opsession-prxy 3307/udp # OP Session Proxy tns-server 3308/tcp # TNS Server tns-server 3308/udp # TNS Server
\b单词边界,只匹配单词不匹配字符串[root@zhangguokui test]# grep "like\b" oldboy.txt i like badminton ball ,billiard ball and chinese chess!
-
egrep "(oldboy)(oldgirl)(littleboy)"\1\2\3分别对应前面的()
{n,m}重复前一个字符各种次数[root@zhangguokui test]# egrep "0{3,5}" oldboy.txt #<==3到5次 my qq num is 49000448. not 4900000448. [root@zhangguokui test]# egrep "0{,5}" oldboy.txt #<==最多5次 I am oldboy teacher! i teach linux i like badminton ball ,billiard ball and chinese chess! our site is http://www.oldboyedu.com my qq num is 49000448. not 4900000448. my god , i am not oldbey,but OLDBOY! [root@zhangguokui test]# egrep "0{3,}" oldboy.txt #<==最少3次 my qq num is 49000448. not 4900000448. [root@zhangguokui test]# egrep "0{3}" oldboy.txt #<==3次 my qq num is 49000448. not 4900000448.
sed
[root@zhangguokui test]# cat test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
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
[root@zhangguokui test]# sed -n '1~2p' test.txt #<==隔行波浪线
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
- 输出2-4行
[root@zhangguokui test]# sed '2,4p' -n oldboy.txt
i teach linux
i like badminton ball ,billiard ball and chinese chess!
- 输出含有
oldboy的字符串的行
[root@zhangguokui test]# sed -n '/oldboy/p' oldboy.txt #<==-n表示默认不输出,输出用p
I am oldboy teacher!
our site is http://www.oldboyedu.com
- 删除含有
oldboy字符串的行(如需更改文件,需要加-i)
[root@zhangguokui test]# sed '/oldboy/d' oldboy.txt #<==用d符号删除行
i teach linux
i like badminton ball ,billiard ball and chinese chess!
my qq num is 49000448.
not 4900000448.
my god , i am not oldbey,but OLDBOY!
- 改字符串(如需修改文件加
-i)
[root@zhangguokui test]# sed 's#oldboy#oldgirl#g' oldboy.txt
I am oldgirl teacher!
i teach linux
i like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldgirledu.com
my qq num is 49000448.
not 4900000448.
my god , i am not oldbey,but OLDBOY!
- 多项编辑用
-e
[root@zhangguokui test]# sed -e 's#oldboy#oldgirl#g' -e 's#49000448#123321#g' oldboy.txt
I am oldgirl teacher!
i teach linux
i like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldgirledu.com
my qq num is 123321.
not 4900000448.
my god , i am not oldbey,but OLDBOY!
- 增加文本用
a(更改文件用-i)
[root@zhangguokui test]# sed '3a i like linux' oldboy.txt
I am oldboy teacher!
i teach linux
i like linux #<==在第三行后增加
i like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
my qq num is 49000448.
not 4900000448.
my god , i am not oldbey,but OLDBOY!
- 插入文本用
i(更改文件用-i)
[root@zhangguokui test]# sed '3a i like linux\nhow about you?\ntell me' oldboy.txt
I am oldboy teacher!
i teach linux
i like linuxi #<==在第三行后加,\n多行
how about you?
tell me
i like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
my qq num is 49000448.
not 4900000448.
my god , i am not oldbey,but OLDBOY!
[root@zhangguokui test]# sed '3i i like linux\nhow about you?\ntell me' oldboy.txt
I am oldboy teacher!
i teach linux
i like linux #<==在第三行插入,\n多行
how about you?
tell me
i like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
my qq num is 49000448.
not 4900000448.
my god , i am not oldbey,but OLDBOY!
- 取ip的三个方法
[root@zhangguokui test]# ifconfig eth0|sed -n '2s#^.*ddr:##gp'|sed -n 's#\ Bc.*$##gp' #<==\空格匹配,-n默认不输出,后面接p
192.168.144.129
[root@zhangguokui test]# ifconfig eth0|sed -ne '2s#^.*ddr:##g' -ne '2s#\ Bc.*$##gp' #<==使用-e 多项编辑
192.168.144.129
[root@zhangguokui test]# ifconfig eth0|sed -nr '2s#^.*ddr:(.*)\ Bc.*$#\1#gp' #<==-r表示支持小括号功能,\1取小括号内容
192.168.144.129
- 替换练习
[root@zhangguokui tmp]# cat person.txt
101,lodboy,CEO
102,zhangyao,CTO
103,alex,COO
104,yy,CFO
105,FIEXUE,CIO
[root@zhangguokui tmp]# sed '1,3s#C#--&--#g' person.txt
101,lodboy,--C--EO
102,zhangyao,--C--TO
103,alex,--C--OO
104,yy,CFO
105,FIEXUE,CIO
- 作业,取644
[root@zhangguokui test]# stat /etc/hosts
File: `/etc/hosts'
Size: 158 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 390175 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-12-29 23:14:35.801999244 +0800
Modify: 2019-12-27 13:25:25.966727272 +0800
Change: 2019-12-27 13:25:25.969727260 +0800
[root@zhangguokui test]# stat /etc/hosts|sed -nr '4s#^.*cess:\ \((.*)/-r.*$#\1#gp'
0644
[root@zhangguokui test]# stat /etc/hosts|sed -ne '4s#^.*cess:\ (##g' -ne '4s#/-.*$##gp'
0644
[root@zhangguokui test]# stat /etc/hosts|sed -n '4s#^.*cess:\ (##gp'|sed -n 's#/-r.*$##gp'
0644
awk
[root@zhangguokui test]# cat test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
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
取2-3行
[root@zhangguokui test]# awk 'NR>1&&NR<4' test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@zhangguokui test]# awk 'NR==2,NR==3' test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
过滤出含有
root
[root@zhangguokui test]# awk '/root/' test.txt
root:x:0:0:root:/root:/bin/bash
删除含
root的行
[root@zhangguokui test]# awk '/[^r]/' test.txt #<==过滤非r字符的行,都有非r,所以全部打印
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
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
[root@zhangguokui test]# awk '/^[^r]/' test.txt #<==过滤非r开头
bin:x:1:1:bin:/bin:/sbin/nologin
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
打印第一列,第三列,最后一列,及行号
[root@zhangguokui test]# awk -F ":" '{print NR,$1,$3,$NF}' test.txt
1 root 0 /bin/bash
2 bin 1 /sbin/nologin
3 daemon 2 /sbin/nologin
4 adm 3 /sbin/nologin
5 lp 4 /sbin/nologin
- 取ip的三个方法
[root@zhangguokui test]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:B7:56:CC
inet addr:192.168.144.129 Bcast:192.168.144.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feb7:56cc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:158709 errors:0 dropped:0 overruns:0 frame:0
TX packets:55463 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:162970290 (155.4 MiB) TX bytes:12525795 (11.9 MiB)
[root@zhangguokui test]# ifconfig eth0|awk -F ":" 'NR==2{print $2}'|awk '{print $1}'
192.168.144.129
[root@zhangguokui test]# ifconfig eth0|awk NR==2|awk -F "[ :]+" '{print $4}'
192.168.144.129
[root@zhangguokui test]# ifconfig eth0|awk NR==2|awk -F "inet addr:|Bcast" '{print $2}'
192.168.144.129
- 几个小题
[root@zhangguokui tmp]# ls -l | awk '{if ($2>1) print $0}'
total 24
drwxr-xr-x 2 root root 4096 Dec 27 23:11 bbb
-rw-r--r-- 2 root root 4 Dec 30 02:29 oldboy
-rw-r--r-- 2 root root 4 Dec 30 02:29 oldboy_hardlink
drwxr-xr-x 3 root root 4096 Dec 30 03:35 oldgirl
drwxr-xr-x 2 root root 4096 Dec 30 16:57 test
[root@zhangguokui tmp]# ls -l|sed -n '/^d/p'
drwxr-xr-x 2 root root 4096 Dec 27 23:11 bbb
drwxr-xr-x 3 root root 4096 Dec 30 03:35 oldgirl
drwxr-xr-x 2 root root 4096 Dec 30 16:57 test
[root@zhangguokui tmp]# ls -l |awk '/^d/'
drwxr-xr-x 2 root root 4096 Dec 27 23:11 bbb
drwxr-xr-x 3 root root 4096 Dec 30 03:35 oldgirl
drwxr-xr-x 2 root root 4096 Dec 30 16:57 test
[root@zhangguokui tmp]# ls -F |awk '/\/$/'
bbb/
oldgirl/
test/
[root@zhangguokui tmp]# ls -F |sed -n '/\/$/p'
bbb/
oldgirl/
test/