grep, egrep, fgrep, rgrep - print lines matching a pattern

grep, egrep, fgrep, rgrep - print lines matching a pattern

14.1. -v, --invert-match

grep -v "grep"

[root@development ~]# ps ax | grep httpd
 6284 ?        Ss     0:10 /usr/local/httpd-2.2.14/bin/httpd -k start
 8372 ?        S      0:00 perl ./wrapper.pl -chdir -name httpd -class com.caucho.server.resin.Resin restart
19136 ?        S      0:00 /usr/local/httpd-2.2.14/bin/httpd -k start
19749 pts/1    R+     0:00 grep httpd
31530 ?        Sl     0:57 /usr/local/httpd-2.2.14/bin/httpd -k start
31560 ?        Sl     1:12 /usr/local/httpd-2.2.14/bin/httpd -k start
31623 ?        Sl     1:06 /usr/local/httpd-2.2.14/bin/httpd -k start
[root@development ~]# ps ax | grep httpd | grep -v grep
 6284 ?        Ss     0:10 /usr/local/httpd-2.2.14/bin/httpd -k start
 8372 ?        S      0:00 perl ./wrapper.pl -chdir -name httpd -class com.caucho.server.resin.Resin restart
19136 ?        S      0:00 /usr/local/httpd-2.2.14/bin/httpd -k start
31530 ?        Sl     0:57 /usr/local/httpd-2.2.14/bin/httpd -k start
31560 ?        Sl     1:12 /usr/local/httpd-2.2.14/bin/httpd -k start
31623 ?        Sl     1:06 /usr/local/httpd-2.2.14/bin/httpd -k start
		

14.2. Output control

14.2.1. 递归操作

递归查询

$ sudo grep -r 'neo' /etc/*
			

递归替换

for file in $( grep -rl '8800.org' *  | grep -v .svn ); do
    echo item: $file
	[ -f $file ] && sed -e 's/8800\.org/sf\.net/g' -e 's/netkiller/neo/g' $file >$file.bak; cp $file.bak $file;
done

14.2.2. -c, --count print only a count of matching lines per FILE

$ cat /etc/resolv.conf
nameserver localhost
nameserver 208.67.222.222
nameserver 208.67.220.220
nameserver 202.96.128.166
nameserver 202.96.134.133
$ grep -c nameserver /etc/resolv.conf
5
			
# grep -c GET /www/logs/access.log
188460

# grep -c POST /www/logs/access.log
421
			

14.3. Context control

14.3.1. -A, --after-context=NUM print NUM lines of trailing context

返回匹配当前行至下面N行

# grep -A1 game /etc/passwd
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

# grep -A2 game /etc/passwd
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
			

14.3.2. -B, --before-context=NUM print NUM lines of leading context

返回匹配当前行至上面N行

# grep -B1 game /etc/passwd
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin

# grep -B2 game /etc/passwd
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin

14.3.3. -C, --context=NUM print NUM lines of output context

-NUM same as --context=NUM
neo@neo-OptiPlex-380:~$ grep -C 1 new /etc/passwd
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh

neo@neo-OptiPlex-380:~$ grep -C 5 new /etc/passwd
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
proxy:x:13:13:proxy:/bin:/bin/sh
www-data:x:33:33:www-data:/var/www:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
list:x:38:38:Mailing List Manager:/var/list:/bin/sh

# grep -3 game /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin

14.3.4. --color

# grep --color root  /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
			

可以通过alias别名启用--color选项

alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
			

加入.bashrc中,每次用户登录将自动生效

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

14.4. Regexp selection and interpretation

n 开头

$ grep '^n' /etc/passwd
news:x:9:9:news:/var/spool/news:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
neo:x:1000:1000:neo chan,,,:/home/neo:/bin/bash
nagios:x:116:127::/var/run/nagios2:/bin/false
		

bash 结尾

$ grep 'bash$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
neo:x:1000:1000:neo chan,,,:/home/neo:/bin/bash
postgres:x:114:124:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
cvsroot:x:1001:1001:cvsroot,,,,:/home/cvsroot:/bin/bash
svnroot:x:1002:1002:subversion,,,,:/home/svnroot:/bin/bash
		

中间包含 root

$ grep '.*root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
cvsroot:x:1001:1001:cvsroot,,,,:/home/cvsroot:/bin/bash
svnroot:x:1002:1002:subversion,,,,:/home/svnroot:/bin/bash
		
ps ax |grep -E "mysqld|httpd|resin"
		

14.4.1. 2010:(13|14|15|16)

regular 匹配一组

egrep "2010:(13|14|15|16)"  access.2010-11-18.log > apache.log
			

14.4.2. []与{}

源文件

# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Sep 10 00:25:46 2011
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=091f295e-ea6d-4f57-9314-e2333f7ebff7 /                       ext4    defaults        1 1
UUID=b3661a0b-2c50-4e18-8030-be2d043cbfc4 /www                    ext4    defaults        1 2
UUID=4d3468de-a2ac-451c-b693-3bdca8832096 swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

匹配每行包含4个连续字符的字符串的行。

# grep '[A-Z]\{4\}' /etc/fstab
UUID=091f295e-ea6d-4f57-9314-e2333f7ebff7 /                       ext4    defaults        1 1
UUID=b3661a0b-2c50-4e18-8030-be2d043cbfc4 /www                    ext4    defaults        1 2
UUID=4d3468de-a2ac-451c-b693-3bdca8832096 swap                    swap    defaults        0 0
			

14.5. ^M

fgrep -rl `echo -ne '\r'` .

find . -type f -exec grep $'\r' {} +
		

14.6. egrep

egrep = grep -E 在egrep中不许看使用转意字符,例如

# grep '\(oo\).*\1' /etc/passwd
root:x:0:0:root:/root:/bin/bash

# grep -E '(oo).*\1' /etc/passwd
root:x:0:0:root:/root:/bin/bash

# egrep '(oo).*\1' /etc/passwd
root:x:0:0:root:/root:/bin/bash
			
$ snmpwalk -v2c -c public 172.16.1.254 | egrep -i 'if(in|out)'

for pid in $(ps -axf |grep 'php-cgi' | egrep egrep "0:00.(6|7|8|9)"'{print $1}'); do kill -9 $pid; done

for pid in $(ps -axf |grep 'php-cgi' | egrep "0:(0|1|2|3|4|5)0.(6|7|8|9)" |awk '{print $1}'); do kill -9 $pid; 
时间: 2024-10-29 01:12:49

grep, egrep, fgrep, rgrep - print lines matching a pattern的相关文章

27.8. grep, egrep, fgrep, rgrep - print lines matching a pattern

27.8.1. 删除空行 $ cat file | grep '.' 27.8.2. -v, --invert-match grep -v "grep" [root@development ~]# ps ax | grep httpd 6284 ? Ss 0:10 /usr/local/httpd-2.2.14/bin/httpd -k start 8372 ? S 0:00 perl ./wrapper.pl -chdir -name httpd -class com.caucho.

5.8. grep, egrep, fgrep, rgrep - print lines matching a pattern

5.8.1. 删除空行 $ cat file | grep '.' 5.8.2. -v, --invert-match grep -v "grep" [root@development ~]# ps ax | grep httpd 6284 ? Ss 0:10 /usr/local/httpd-2.2.14/bin/httpd -k start 8372 ? S 0:00 perl ./wrapper.pl -chdir -name httpd -class com.caucho.se

linux系统下grep egrep fgrep用法以及正则表达式

一.grep.egrep.fgrep命令 本文中主要介绍了linux系统下grep egrep fgrep命令和正则表达式的基本参数和使用格式.方法.(注释:文中fg代表例子,) 1.1.基本定义: grep(global search regular RE ) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它只能使用基本的正则表达式来搜索文本,并把匹配的行打印出来. grep是很常见也很常用的命令,它的主要功能是进行字符串数据的比较,

第 5 章 Shell command

目录 5.1. Help Commands 5.1.1. man - an interface to the on-line reference manuals 5.1.1.1. manpath.config 5.1.1.2. 查看man手册位置 5.1.1.3. 指定手册位置 5.2. getconf - Query system configuration variables 5.3. Directory and File System Related 5.3.1. dirname 5.3.

Linux正则表达式,用户、组管理的入门知识

Linux基础-正则表达式 正则表达式简单来说就是一种行字符串处理的方式,通过一些特殊符号的辅助,可达到搜索.删除.替换等某些特定字符串的处理方式 通配符(Globbing) 通配符与元字符类似,通配符主要用于文件名的匹配,而元字符则主要用在字符串的匹配上: 下面介绍几种常用的通配符: * 表示匹配任意位数的任意字符 ? 表示匹配一位任意字符 ^ 表示取反,不包含的意思 [] 表示此区间内的任意一个字符 {} 表示一种集合 \ 转义字符,使具有特殊意义的字符失去原有意义 | 表示'或',匹配一组

Linux基础之正则表达式,用户、组管理命令介绍_Linux

通配符(Globbing) 通配符与元字符类似,通配符主要用于文件名的匹配,而元字符则主要用在字符串的匹配上: 下面介绍几种常用的通配符: * 表示匹配任意位数的任意字符 ? 表示匹配一位任意字符 ^ 表示取反,不包含的意思 [] 表示此区间内的任意一个字符 {} 表示一种集合 \ 转义字符,使具有特殊意义的字符失去原有意义 | 表示'或',匹配一组可选的字符 元字符 元字符是用来描述字符的特殊字符. 常用的元字符及意义如下: * 重复前面的字符0次或者多次 . 匹配任意字符一次 \+ 匹配前面

github usage 1

postgresql 就放在github上, github上有很多开源软件. 本文简单的介绍一下github的使用. 以后的代码可以尽量往上放, 大家可以帮忙修修补补.  1. 注册github账号 https://github.com/  假设注册的信息如下, 后面需要用到. 用户名 : digoal 邮箱 : digoal@126.com 密码 : test 2. 新建一个Repository 例如 : sky_postgresql_cluster 编写 README.md 3. 安装git

Version Control with Git (Udacity)优达学城笔记--L1:What is Version Control

What is Version Control version control: control version Version Cotrol Systems: Git, Subversion, Mercurial Centralized:all users connect to a central, master repository Distributed: each user has the entire repository on their computer git: Distribu

Bash Shell字符串操作小结_linux shell

1. 取长度 复制代码 代码如下: str="abcd"expr length $str   # 4echo ${#str}       # 4expr "$str" : ".*" # 4 好像一般使用第二种2. 查找子串的位置 复制代码 代码如下: str="abc"expr index $str "a"  # 1expr index $str "b"  # 2expr index $