分类:
2005-08-30 16:14:12
g r e p一般格式为:
代码: |
grep [选项]基本正则表达式[文件] 这里基本正则表达式可为字符串。 |
引用: |
-c 只输出匹配行的计数。 -i 不区分大小写(只适用于单字符)。 -h 查询多文件时不显示文件名。 -l 查询多文件时只输出包含匹配字符的文件名。 -n 显示匹配行及行号。 -s 不显示不存在或无匹配文本的错误信息。 -v 显示不包含匹配文本的所有行。 |
引用: |
第1列:城市位置编号。 第2列:月份。 第3列:存储代码及出库年份。 第4列:产品代号。 第5列:产品统一标价。 第6列:标识号。 第7列:合格数量。 |
代码: |
$ cat data.f 48 Dec 3BC1977 LPSX 68.00 LVX2A 138 483 Sept 5AP1996 USP 65.00 LVX2C 189 47 Oct 3ZL1998 LPSX 43.00 KVM9D 512 219 dec 2CC1999 CAD 23.00 PLV2C 68 484 nov 7PL1996 CAD 49.00 PLV2C 234 483 may 5PA1998 USP 37.00 KVM9D 644 216 sept 3ZL1998 USP 86.00 KVM9E 234 |
代码: |
$ grep "sort it" * |
代码: |
$ grep "48"data.f |
代码: |
$ grep -c "48"data.f 4 |
代码: |
$ grep -n "48"data.f |
代码: |
$ grep -v "48"data.f |
代码: |
$grep "48>" data.f |
引用: |
另一种方法我试过,好像不行: 注意在每个匹配模式中抽取字符串后有一个< Ta b >键,所以应操作如下: < Ta b >表示点击t a b键。 $grep "48 |
代码: |
$grep -i "48" data.f |
代码: |
$ grep "48[34]" data.f 483 Sept 5AP1996 USP 65.00 LVX2C 189 484 nov 7PL1996 CAD 49.00 PLV2C 234 483 may 5PA1998 USP 37.00 KVM9D 644 |
代码: |
$ grep "^[^48]" data.f 219 dec 2CC1999 CAD 23.00 PLV2C 68 216 sept 3ZL1998 USP 86.00 KVM9E 234 |
代码: |
$ grep -v "^[^48]" data.f |
代码: |
[sam@chenwy sam]$ grep -i "sept" data.f 483 Sept 5AP1996 USP 65.00 LVX2C 189 216 sept 3ZL1998 USP 86.00 KVM9E 234 |
代码: |
[sam@chenwy sam]$ grep '[sS]ept' data.f |
代码: |
[sam@chenwy sam]$ grep '[sS]ept' data.f | grep 48 483 Sept 5AP1996 USP 65.00 LVX2C 189 |
代码: |
[sam@chenwy sam]$ grep 'K...D' data.f 47 Oct 3ZL1998 LPSX 43.00 KVM9D 512 483 may 5PA1998 USP 37.00 KVM9D 644 |
代码: |
[sam@chenwy sam]$ grep '[A-Z]..C' data.f 483 Sept 5AP1996 USP 65.00 LVX2C 189 219 dec 2CC1999 CAD 23.00 PLV2C 68 484 nov 7PL1996 CAD 49.00 PLV2C 234 |
代码: |
[sam@chenwy sam]$ grep '5..199[6,8]' data.f 483 Sept 5AP1996 USP 65.00 LVX2C 189 483 may 5PA1998 USP 37.00 KVM9D 644 |
代码: |
[sam@chenwy sam]$ grep '[0-9][0-5[0-6]' data.f 48 Dec 3BC1977 LPSX 68.00 LVX2A 138 483 Sept 5AP1996 USP 65.00 LVX2C 189 47 Oct 3ZL1998 LPSX 43.00 KVM9D 512 219 dec 2CC1999 CAD 23.00 PLV2C 68 484 nov 7PL1996 CAD 49.00 PLV2C 234 483 may 5PA1998 USP 37.00 KVM9D 644 216 sept 3ZL1998 USP 86.00 KVM9E 234 |
代码: |
[sam@chenwy sam]$ grep '^[0-9][0-5][0-6]' data.f 219 dec 2CC1999 CAD 23.00 PLV2C 68 216 sept 3ZL1998 USP 86.00 KVM9E 234 |
代码: |
[sam@chenwy sam]$ grep '4{2,}' data.f 483 may 5PA1998 USP 37.00 KVM9D 644 |
代码: |
[sam@chenwy sam]$ grep '9{3,}' data.f 219 dec 2CC1999 CAD 23.00 PLV2C 68 |
代码: |
[sam@chenwy sam]$ grep '9{3}' data.f 219 dec 2CC1999 CAD 23.00 PLV2C 68 [sam@chenwy sam]$ grep '9{2}' data.f 483 Sept 5AP1996 USP 65.00 LVX2C 189 47 Oct 3ZL1998 LPSX 43.00 KVM9D 512 219 dec 2CC1999 CAD 23.00 PLV2C 68 484 nov 7PL1996 CAD 49.00 PLV2C 234 |
代码: |
[sam@chenwy sam]$ cat myfile 83 888883 8884 88883 [sam@chenwy sam]$ grep '8{2,6}3' myfile 888883 88883 |
代码: |
[sam@chenwy sam]$ grep -E '219|216' data.f 219 dec 2CC1999 CAD 23.00 PLV2C 68 216 sept 3ZL1998 USP 86.00 KVM9E 234 |
代码: |
[sam@chenwy sam]$ grep -c '^$' myfile |
代码: |
[sam@chenwy sam]$ grep -c '^$' myfile |
代码: |
[sam@chenwy sam]$ grep '.' myfile |
代码: |
[sam@chenwy sam]$ grep '"' myfile |
代码: |
[sam@chenwy sam]$ grep 'conftroll.conf' myfile |
代码: |
[sam@chenwy sam]$ grep '^[a-z]{1,6}.[A-Z]{1,2}' filename |
代码: |
[0-9]{3}.[0-9]{3}.' |
引用: |
[ [ : u p p e r : ] ] [ A - Z ] [ [ : a l n u m : ] ] [ 0 - 9 a - zA-Z] [ [ : l o w e r : ] ] [ a - z ] [ [ : s p a c e : ] ] 空格或t a b键 [ [ : d i g i t : ] ] [ 0 - 9 ] [ [ : a l p h a : ] ] [ a - z A - Z ] |
代码: |
$grep '5[[:upper:]][[:upper]]' data.f |
代码: |
grep '[[:upper:]][[:upper:]][P,D]' data.f |
代码: |
$cat testfile looks likes looker long |
代码: |
grep "l.*s" testfile |
代码: |
grep "ng$" testfile |
代码: |
[root@Linux_chenwy sam]# grep "sam" /etc/passwd sam:x:506:4::/usr/sam:/bin/bash |
代码: |
[root@Linux_chenwy sam]# grep "sam" /etc/password grep: /etc/password: 没有那个文件或目录 |
代码: |
[root@Linux_chenwy sam]# grep -s "sam" /etc/password |
代码: |
[root@Linux_chenwy sam]# grep "sam" /tec/password >/dev/null 2>&1 |
代码: |
[root@Linux_chenwy sam]# grep "sam" /etc/passwd >/usr/sam/passwd.out [root@Linux_chenwy sam]# cat /usr/sam/passwd.out sam:x:506:4::/usr/sam:/bin/bash |
代码: |
[root@Linux_chenwy sam]# ps ax|grep "named" 2897 pts/1 S 0:00 grep named |
代码: |
[root@Linux_chenwy sam]# STR="Mary Joe Peter Pauline" [root@Linux_chenwy sam]# echo $STR | grep "Mary" Mary Joe Peter Pauline |
代码: |
[root@Linux_chenwy sam]# echo $STR | grep "Simon" |
代码: |
[root@Linux_chenwy sam]# vi grepstrings [root@Linux_chenwy sam]# cat grepstrings 484 47 |
代码: |
[root@Linux_chenwy sam]# egrep -f grepstrings data.f 47 Oct 3ZL1998 LPSX 43.00 KVM9D 512 484 nov 7PL1996 CAD 49.00 PLV2C 234 |
代码: |
[root@Linux_chenwy sam]# egrep '(3ZL|2CC)' data.f 47 Oct 3ZL1998 LPSX 43.00 KVM9D 512 219 dec 2CC1999 CAD 23.00 PLV2C 68 216 sept 3ZL1998 USP 86.00 KVM9E 234 |
代码: |
$who |egrep (louise|matty|pauline) |
代码: |
$who |egrep -v '^(matty|pauline)' |
代码: |
$egrep '(shutdown |reboot) (s)?' * |