grep-1.txt:
beyes
root
admin
qinhe
duoduo
qingqing
xiaohe
duoduo
--------------------------------------
grep-2.txt
sort it
2009
2009
2008
Jul
jul
jUl
juL
2009:22
2009:22:51
2100
01200
[beyes@localhost basic_test]$ grep "duoduo" *.txt
grep-1.txt:duoduo
grep-1.txt:duoduo
[beyes@localhost basic_test]$ grep "sort it" *
grep-2.txt:sort it
[beyes@localhost basic_test]$ grep -c "duoduo" grep-1.txt
2
[beyes@localhost basic_test]$ grep -n "duoduo" grep-1.txt
5:duoduo
8:duoduo
[beyes@localhost basic_test]$ grep -i "jul" grep-2.txt
Jul
jul
jUl
juL
[beyes@localhost basic_test]$ grep -v "2009:22" grep-2.txt #过滤掉含有 2009 的一行后并打印文本
sort it
2008
Jul
jul
jUl
juL
2100
01200
[beyes@localhost basic_test]$ grep "2009:22:5[0-9]" grep-2.txt
2009:22:51
[beyes@localhost basic_test]$ grep "[2]" grep-2.txt
2009
2009
2008
2009:22
2009:22:51
2100
01200
[beyes@localhost basic_test]$ grep "^2" grep-2.txt
2009
2009
2008
2009:22
2009:22:51
2100
[beyes@localhost basic_test]$ grep "^[^2]" grep-2.txt #再取一次 ^ 则为过滤(补集),^^2 -- 这样的形式无效
sort it
Jul
jul
jUl
juL
01200
# echo GNU is not UNIX |grep -o "UNIX"
UNIX
root@bt:~# echo "GNU is not UNIX" |grep -b -o "UNIX"
11:UNIX
root@bt:~# echo hello www groad net |grep -e "groad" -e "net" -o
groad
net
root@bt:~# cat pfile.txt
groad
net
root@bt:~# echo hello www groad net |grep -f pfile.txt -o
groad
net
root@bt:~/src# grep "hello_test()" . -R -n
./misc/hello.c:3:int hello_test()
./misc/hello.c:11: hello_test();
root@bt:~/src# grep "hello_test()" . -r --include *.{c,cpp}
./misc/hello.c:int hello_test()
./misc/hello.c: hello_test();
root@bt:~/src# grep "hello_test()" . -r --exclude *.c
./misc/hello.txt:hello_test()
# seq 10 | grep 5 -A 3
5
6
7
8
root@bt:~/src# echo -e "a\nb\nc\na\nb\nc" | grep a -A 1
a
b
--
a
b
root@bt:~/src# echo -e "a\nb\nc\nd\na\nb" | grep a -A 1
a
b
--
a
b
root@bt:~/src# echo -e "a\nb\nc\nd\nc\na" | grep a -A 1
a
b
--
a
# seq 10 | grep 5 -B 3
2
3
4
5
# seq 10 | grep 5 -C 3
2
3
4
5
6
7
8
groad.net
beyes@groad.net
g-d-m
NO:561
NO:873
445511
455114
444511
411111
404040404
444441111
111114444
??hello
how are you??
[quote]
列出含有以 g 开头且以 d 结尾字符串的行:
[quote]
[beyes@localhost basic_test]$ grep "g*d" grep-3.txt
groad.net
beyes@groad.net
g-d-m
[beyes@localhost basic_test]$ grep "[5-8][6-9][0-3]" grep-3.txt
NO:561
NO:873
[beyes@localhost basic_test]$ grep "4\\{2\\}" grep-3.txt
445511
444511
444441111
111114444
[beyes@localhost basic_test]$ grep "4\\{2,\\}" grep-3.txt
445511
444511
444441111
111114444
[beyes@localhost basic_test]$ grep "4\\{2,4\\}" grep-3.txt
445511
444511
444441111
111114444
[beyes@localhost basic_test]$ grep "^$" -n grep-3.txt
7:
9:
11:
[beyes@localhost basic_test]$ grep "\\?" grep-3.txt
??hello
how are you??
[beyes@localhost basic_test]$ grep "^h" grep-3.txt
how are you??
[beyes@localhost basic_test]$ grep "^[^g]" grep-3.txt
beyes@groad.net
NO:561
NO:873
445511
455114
444511
411111
404040404
444441111
111114444
??hello
how are you??
类 | 等价的正则表达式 |
[[:upper:]] | [A-Z] |
[[:alnum:]] | [0-9a-zA-Z] |
[[:lower:]] | [a-z] |
[[:space:]] | 空格或tab键 |
[[:digit:]] | [0-9] |
[[:alpha:]] | [a-zA-Z] |
# touch "www groad.txt"
# vi groad\\ net.txt
# cat groad\\ net.txt
groad
# echo groad > groad.txt
# echo "good website" > www.txt
# grep "groad" *.txt -lZ |xargs rm
xargs: Warning: a NUL character occurred in the input. It cannot be passed through in the argument list. Did you mean to use the --null option?
rm: cannot remove `groad': No such file or directory
rm: cannot remove `net.txt': No such file or directory
root@bt:~/src# grep "groad" *.txt -lZ |xargs -0 rm
root@bt:~/src# ls
misc www.txt
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |