bash编程之: Grep文本搜索

grep:文本搜索工具 根据用户给出的"模式",搜索出自己需要的信息

grep  选项   "模式"  文件名

      --color 高亮匹配到的字符

      -n    显示行号

      -v    取反操作

基本正则表达式:

元字符:

    .   任意单个字符

    []  集合中的单个字符

        [[:digit:]]  [0-9]

                     [a-z]

                     [A-Z]

                     [a-zA-Z]

                     [0-9a-zA-Z]

        [[:space:]]

        [[:punct:]]   匹配一个字符

    [^] 集合外的任意单个字符

匹配次数:

    * 匹配其前面字符任意次:0,1,。。。

        grep "[a-z]*y"

        xy  xxy   xxxy  y

    \? 匹配其前面字符0,1

            grep "x\?y"

        xy  xxy   xxxy  y

    \+  匹配其前面字符至少1次

    \{m\}  匹配其前面字符至少m次

    \{m,n\}   匹配其前面字符至少m次,最多出现n次

.*  匹配任意长度任意字符

    

位置锚定:

^   行首锚定

$   行尾锚定

^$  空白行  

\<  词首锚定

\>  词尾锚定

\<单词锚定\>

()  分组

\(模式1\|模式2\) 匹配模式1或模式2

后向引用 后文中引用前面匹配到的字符

\(匹配字符\)  \1    \1表示被匹配到的第一个字符

grep \(aa\)b\1    匹配含有aabaa的行

1、显示/proc/meminfo文件中以大写或小写S开头的行;

grep --color "^[Ss]"  /proc/meminfo

2、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;

grep  -v --color  "/sbin/nologin$"  /etc/passwd

3、显示/etc/passwd文件中其默认shell为/bin/bash的用户;   

grep  --color  "/bin/bash$"  /etc/passwd

4、找出/etc/passwd文件中的一位数或两位数;

grep --color "\<\([0-9]\|[1-9][0-9]\)\>" /etc/passwd

5、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;

grep --color "^[[:space:]]\+" /boot/grub/grub.conf

6、显示/etc/rc.d/rc.sysinit文件中,以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;

grep --color "^#[[:space:]]\+[^[:space:]]\+"  /etc/rc.d/rc.sysinit

7、找出netstat -tan命令执行结果中以'LISTEN'(后可有空白字符)结尾的行;

netstat -tan | grep --color "LISTEN[[:space:]]*$"

8、添加用户bash, testbash, basher, nologin(SHELL为/sbin/nologin),而找出当前系统上其用户名和默认shell相同的用户;

grep --color "\([0-9a-zA-Z]\)\+:.*\1$" /etc/passwd

--------------------------------------------------------------------------------------------

Grep 练习
文件名grepfile
Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100
Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900
Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900
Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500
Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700
James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500
Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500
Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
Zippy Pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500
Arthur Putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000
Popeye Sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600
Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
Yukio Takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000
Vinh Tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900

1.显示所有包含San的行

grep --color "\(San\)\+" grepfile

2.显示所有以J开始的人名所在的行

grep --color "\<J\+" grepfile

3.显示所有以700结尾的行

grep --color "700$" grepfile

4.显示所有不包括834的行

grep --color -v "\(834\)\+" grepfile

5.显示所有生日在December的行

grep --color "\(:12/\)\+" grepfile

6.显示所有这样的行:它包含一个大写字母,后跟四个小写字母,一个冒号,一个空格,和一个大写字母

grep --color "[A-Z][a-z]\{4\}:[[:space:]][A-Z]" grepfile

7.显示姓以K或k开头的行

grep --color "^\([K]\|[k]\)" grepfile

9.显示工资为六位数的行,并在前面加行号

grep --color -n "[[:digit:]]\{6\}" grepfile

10.显示包括Lincoln或lincoln的行,并且grep对大小写不敏感.

grep --color -i "lincoln" grepfile

备注:

http://www.cnblogs.com/end/archive/2012/02/21/2360965.html

猜你喜欢

转载自oracle-api.iteye.com/blog/2359049