1
2
3
4
5
6
7
8
9
10
11
|
# alias
alias cp = 'cp -i'
alias egrep = 'egrep --color=auto'
alias fgrep = 'fgrep --color=auto'
alias grep = 'grep --color=auto'
alias l.= 'ls -d .* --color=auto'
alias ll= 'ls -l --color=auto'
alias ls = 'ls --color=auto'
alias mv = 'mv -i'
alias rm = 'rm -i'
alias which = 'alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
|
1
2
3
4
5
6
7
8
9
10
11
|
# cat test
Hello World
How are you?
Mageedu is very good!
# grep "^H" test
Hello World
How are you?
# grep "^h" test
# grep -i "^h" test
Hello World
How are you?
|
1
2
3
4
|
# grep "good" test
Mageedu is very good!
[root@localhost ~] # grep -o "good" test
good
|
1
2
3
4
5
6
7
8
9
|
# cat test
Hello World
How are you?
Mageedu is very good!
[root@localhost ~] # grep "M.*[[:punct:]]$" test
Mageedu is very good!
[root@localhost ~] # grep -v "M.*[[:punct:]]$" test
Hello World
How are you?
|
1
2
3
4
5
6
|
# grep -q "good" test
# echo $?
0
# grep -q "goodddddddd" test
# echo $?
1
|
1
2
3
4
5
6
7
|
# cat test
Hello World
How are you?
Mageedu is very good!
[root@localhost ~] # grep -e "good" -e "you" test
How are you?
Mageedu is very good!
|
1
|
-f与-e类似,都是匹配多个模式,只是-f选项读取的是文本文件中的模式,逐行匹配。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# cat test
Hello World
How are you?
Mageedu is very good!
# grep -A 1 "you" test
How are you?
Mageedu is very good!
# grep -B 1 "you" test
Hello World
How are you?
# grep -C 1 "you" test
Hello World
How are you?
Mageedu is very good!
|
1
2
3
4
|
# grep r..t /etc/passwd
root:x:0:0:root: /root : /bin/bash
operator:x:11:0:operator: /root : /sbin/nologin
ftp :x:14:50:FTP User: /var/ftp : /sbin/nologin
|
1
2
|
# fdisk -l |grep "[sh]d[a-z]\>"
Disk /dev/sda : 128.8 GB, 128849018880 bytes, 251658240 sectors
|
1
2
3
4
5
6
|
# cat test
Hello World
how are you?
Mageedu is very good!
# grep "^[^[:upper:]]" test
how are you?
|
1
2
3
4
5
6
7
|
# cat test
Hello World
how are you?
Mageedu is very good!
# grep "[[:upper:]][[:lower:]].*[[:space:]]" test
Hello World
Mageedu is very good!
|
1
2
3
4
5
6
|
# grep "a*" test1
ab
aaaabcd
bcd
cd
axxxxxxxd
|
1
2
3
4
5
|
# grep "a.*" test1
ab
aaaabcd
axxxxxxxd
cccabbbd
|
1
2
3
4
5
|
# grep "a\+" test1
ab
aaaabcd
axxxxxxxd
cccabbbd
|
1
2
3
4
5
6
7
|
# grep "a\?" test1
ab
aaaabcd
bcd
cd
axxxxxxxd
cccabbbd
|
1
2
3
4
|
# grep "a\{2\}" test1
aaaabcd
aaxxxxxxxd
cccaabbbd
|
1
2
3
4
5
|
# grep "a\{1,3\}" test1
ab
aaaabcd
aaxxxxxxxd
cccaabbbd
|
1
2
|
# ps -ef|grep "^root.*bash$"
root 1143 1141 0 09:20 pts /0 00:00:00 - bash
|
1
2
3
4
5
|
# grep "^hello" /tmp/fstab
hello
hello world
# grep "^hello$" /tmp/fstab
hello
|
1
2
3
4
5
6
7
8
9
10
11
|
# cat test
Hello World
how are you?
Mageedu is very good!
# grep -v "^$" test
Hello World
how are you?
Mageedu is very good!
|
1
2
3
4
5
6
7
8
9
10
11
12
|
# grep -v "^$" test
Hello World
how are you?
Mageedu is very good!
# grep -v "^[[:space:]]*$" test
Hello World
how are you?
Mageedu is very good!
|
1
2
3
4
5
6
7
8
9
|
# fdisk -l|grep "/dev/[sh]d[a-z]"
Disk /dev/sda : 128.8 GB, 128849018880 bytes, 251658240 sectors
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 84912127 41943040 83 Linux
/dev/sda3 84912128 126855167 20971520 83 Linux
/dev/sda4 126855168 251658239 62401536 5 Extended
/dev/sda5 126859264 131055615 2098176 82 Linux swap / Solaris
# fdisk -l|grep "/dev/[sh]d[a-z]\>"
Disk /dev/sda : 128.8 GB, 128849018880 bytes, 251658240 sectors
|
1
2
3
|
# grep "\<root" /etc/passwd
root:x:0:0:root: /root : /bin/bash
operator:x:11:0:operator: /root : /sbin/nologin
|
1
2
3
4
5
6
7
8
9
10
|
# cat /tmp/ifconfig.test
/usr/sbin/ifconfig
/usr/sbin/ifconfig2
/usr/sbin/ifconfig3
# grep "ifconfig" /tmp/ifconfig.test
/usr/sbin/ifconfig
/usr/sbin/ifconfig2
/usr/sbin/ifconfig3
# grep "\<ifconfig\>" /tmp/ifconfig.test
/usr/sbin/ifconfig
|
1
2
3
4
5
|
# grep "^\([[:alnum:]]\+\)\>.*\1$" /etc/passwd
sync :x:5:0: sync : /sbin : /bin/sync
shutdown :x:6:0: shutdown : /sbin : /sbin/shutdown
halt:x:7:0:halt: /sbin : /sbin/halt
nologin:x:5010:5010:: /home/nologin : /sbin/nologin
|
1
2
3
4
|
# echo `which pwd`|egrep "[^/]+/?$"
/usr/bin/pwd
# echo `which pwd`|egrep "[^/]+/?$" -o
pwd
|
1
2
3
4
|
# egrep "^(root|mageedu|centos)\>" /etc/passwd
root:x:0:0:root: /root : /bin/bash
mageedu:x:1000:1000:mageedu: /home/mageedu : /bin/bash
centos:x:5008:5008:: /home/centos : /bin/bash
|
1
2
|
# fdisk -l|grep -E "/dev/(s|h)d[a-z]\>"
Disk /dev/sda : 128.8 GB, 128849018880 bytes, 251658240 sectors
|
!评论内容需包含中文