标签:else 忽略 world st3 test 包括 名称 exclude bin
1. grep最简单的用法,匹配一个词:grep word filename
2. 能够从多个文件里匹配:grep word filename1 filenam2 filename3
3. 能够使用正則表達式匹配:grep -E pattern f1 f2 f3...
4. 能够使用-o仅仅打印匹配的字符,例如以下所看到的:
lichao@ubuntu:command$ echo this is a line. | grep -E -o "[a-z]*\." line.5. 打印除匹配行之外的其它行,使用-v
lichao@ubuntu:command$ echo -e "1\n2\n3\n4" | grep -v -E "[1-2]" 3 4
lichao@ubuntu:command$ echo -e "1111\n2222" | grep -E "[1-2]" -c 2
lichao@ubuntu:command$ echo -e "1111\n2222" | grep -o -E "[1-2]" | wc -l 8
lichao@ubuntu:command$ echo -e "1111\n2222\n33333\n44444" | grep -n -E "3" 3:33333
lichao@ubuntu:command$ echo "0123456789" | grep -b -o 4 4:4
11. -L与-l相对。仅仅打印不匹配的文件名称
lichao@ubuntu:command$ cat test1.txt linux is fun lichao@ubuntu:command$ cat test2.txt a very popular os, linux lichao@ubuntu:command$ cat test3.txt what the fxxk lichao@ubuntu:command$ grep -l linux test1.txt test2.txt test3.txt test1.txt test2.txt lichao@ubuntu:command$ grep -L linux test1.txt test2.txt test3.txt test3.txt
lichao@ubuntu:command$ grep -n -R linux . ./test2.txt:5:linux ./test1.txt:1:linux
lichao@ubuntu:command$ echo "HELLO WORLD" | grep -i "hello" HELLO WORLD
lichao@ubuntu:command$ echo "This is a line." | grep -e "This" -e "is" -e "line" -o This is line
lichao@ubuntu:command$ cat pattern.txt 1$ 2 3 lichao@ubuntu:command$ cat num.txt 1 2 3 4 5 6 7 8 9 10 lichao@ubuntu:command$ grep -f pattern.txt num.txt 1 2 3
示比例如以下:
lichao@ubuntu:command$ seq 1 10 | grep 5 -A 3 5 6 7 8 lichao@ubuntu:command$ seq 1 10 | grep 5 -B 3 2 3 4 5 lichao@ubuntu:command$ seq 1 10 | grep 5 -C 3 2 3 4 5 6 7 8 lichao@ubuntu:command$ echo -e "a\nb\nc\nd\na\nb\nc\nd\n" | grep a -A 2 a b c -- a b c
lichao@ubuntu:command$ cat tmp.txt hello world lichao@ubuntu:command$ cat tmp.csh #!/bin/bash if [ $# -ne 2 ]; then echo "Usage: $0 match_pattern file_name" exit fi match=$1 file=$2 grep -q $match $file if [ $?-ne 0 ]; then echo "$match not exist in $file" else echo "$match exist in $file" fi lichao@ubuntu:command$ ./tmp.csh hello tmp.txt hello exist in tmp.txt
lichao@ubuntu:command$ ls -llrt total 28 -rw-rw-r-- 1 lichao lichao 13 Nov 1 20:38 test1.txt -rw-rw-r-- 1 lichao lichao 27 Nov 1 20:39 test2.txt -rw-rw-r-- 1 lichao lichao 14 Nov 1 20:39 test3.txt -rw-rw-r-- 1 lichao lichao 21 Nov 1 20:45 num.txt -rw-rw-r-- 1 lichao lichao 7 Nov 1 20:45 pattern.txt -rw-rw-r-- 1 lichao lichao 12 Nov 1 21:25 tmp.txt -rwxr-xr-x 1 lichao lichao 217 Nov 1 21:27 tmp.csh lichao@ubuntu:command$ cat test1.txt linux is fun lichao@ubuntu:command$ cat test2.txt a very popular os, linux lichao@ubuntu:command$ grep "linux" * -lZ | xargs -0 rm lichao@ubuntu:command$ ls num.txt pattern.txt test3.txt tmp.csh tmp.txt以上命令将包括linux字符串的test1.txt和test2.txt删除。
19. 排除/包含文件或者文件夹:1)--include *{.c,.cpp} 仅仅在文件夹中搜索.c和.cpp文件;2)--exclude "README" 排除全部README文件 3) --include-dir 仅在某些文件夹中搜索 4) --exclude-dir 排除某些文件夹 5) --exclude-from FILE 从文件FILE中读取须要排除的文件列表
lichao@ubuntu:test$ ls dir1 dir2 exclude.config test1.txt test2.doc test3.word lichao@ubuntu:test$ cat test1.txt linux is fun lichao@ubuntu:test$ cat test2.doc wonderful os, linux lichao@ubuntu:test$ cat test3.word wonderful os, linux lichao@ubuntu:test$ ls dir1/ test1.txt test2.doc test3.word lichao@ubuntu:test$ ls dir2/ test1.txt test2.doc test3.word lichao@ubuntu:test$ cat exclude.config *.txt lichao@ubuntu:test$ grep "linux" -R -n . ./test2.doc:3:linux ./test3.word:3:linux ./test1.txt:1:linux ./dir2/test2.doc:3:linux ./dir2/test3.word:3:linux ./dir2/test1.txt:1:linux ./dir1/test2.doc:3:linux ./dir1/test3.word:3:linux ./dir1/test1.txt:1:linux lichao@ubuntu:test$ grep "linux" -R -n . --include *.txt --include *.doc ./test2.doc:3:linux ./test1.txt:1:linux ./dir2/test2.doc:3:linux ./dir2/test1.txt:1:linux ./dir1/test2.doc:3:linux ./dir1/test1.txt:1:linux lichao@ubuntu:test$ grep "linux" -R -n . --exclude *.txt --eclude *.doc grep: unrecognized option ‘--eclude‘ Usage: grep [OPTION]... PATTERN [FILE]... Try ‘grep --help‘ for more information. lichao@ubuntu:test$ grep "linux" -R -n . --exclude *.txt --exclude *.doc ./test3.word:3:linux ./dir2/test3.word:3:linux ./dir1/test3.word:3:linux lichao@ubuntu:test$ grep "linux" -R -n . --exclude-dir dir1 ./test2.doc:3:linux ./test3.word:3:linux ./test1.txt:1:linux ./dir2/test2.doc:3:linux ./dir2/test3.word:3:linux ./dir2/test1.txt:1:linux lichao@ubuntu:test$ grep "linux" -R -n . --exclude-dir dir1 --exclude-dir dir2 ./test2.doc:3:linux ./test3.word:3:linux ./test1.txt:1:linux lichao@ubuntu:test$ grep "linux" -R -n . --exclude-from exclude.config ./test2.doc:3:linux ./test3.word:3:linux ./dir2/test2.doc:3:linux ./dir2/test3.word:3:linux ./dir1/test2.doc:3:linux ./dir1/test3.word:3:linux
注意:转载请注明出处。
标签:else 忽略 world st3 test 包括 名称 exclude bin
原文地址:http://www.cnblogs.com/tlnshuju/p/7106790.html