1. and 查询
$ ps -ef | grep postgres | grep -v grep (-v 不显示 grep 所在行)
postgres 2884 1 0 09:02 ? 00:00:00 /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data
postgres 3108 2884 0 09:02 ? 00:00:00 postgres: writer process
postgres 3109 2884 0 09:02 ? 00:00:00 postgres: wal writer process
postgres 3110 2884 0 09:02 ? 00:00:00 postgres: autovacuum launcher process
postgres 3111 2884 0 09:02 ? 00:00:00 postgres: stats collector process
------------------------------------------------------------
2. or 查询
2-1.
$ ls /etc/ | grep -e allow -e deny
at.deny
cron.deny
hosts.allow
hosts.deny
2-2.
$ ls /etc/ | grep -e allow -e deny
at.deny
cron.deny
hosts.allow
hosts.deny
2-3.
$ ls /etc/ | grep 'allow\|deny'
at.deny
cron.deny
hosts.allow
hosts.deny
2-4.
$ ls /etc/ | egrep 'allow|deny'
at.deny
cron.deny
hosts.allow
hosts.deny
2-5.
$ ls /etc/ | grep -E 'allow|deny'
at.deny
cron.deny
hosts.allow
hosts.deny
------------------------------------------------------------
3. 不区分大小写 (-i)
$ grep root /etc/passwd -i
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
$ grep ROOT /etc/passwd -i
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
------------------------------------------------------------
4. 显示行号 (grep, cat)
$ grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
12:operator:x:11:0:operator:/root:/sbin/nologin
4-1. 只显示匹配行的行号
$ grep -n root /etc/passwd | awk -F ":" '{print $1}'
1
12
$ cat -n /etc/passwd | grep root
1 root:x:0:0:root:/root:/bin/bash
12 operator:x:11:0:operator:/root:/sbin/nologin
------------------------------------------------------------
5. 搜索文件夹
5-1. 显示方式 (文件名字:所在行内容)
$ grep apache /etc/init.d/*
/etc/init.d/httpd:# http://www.apache.org/licenses/LICENSE-2.0
/etc/init.d/httpd:# one is reported. Run "apachectl help" for usage info
/etc/init.d/httpd:HTTPD='/usr/local/apache2/bin/httpd'
/etc/init.d/httpd:if test -f /usr/local/apache2/bin/envvars; then
/etc/init.d/httpd: . /usr/local/apache2/bin/envvars
/etc/init.d/httpd: echo and then use "apachectl start".
5-2. 不显示文件名 (-h)
$ grep -h apache /etc/init.d/*
# http://www.apache.org/licenses/LICENSE-2.0
# one is reported. Run "apachectl help" for usage info
HTTPD='/usr/local/apache2/bin/httpd'
if test -f /usr/local/apache2/bin/envvars; then
. /usr/local/apache2/bin/envvars
echo and then use "apachectl start".
5-3. 只显示文件名 (-l)
$ grep -l apache /etc/init.d/*
/etc/init.d/httpd
5-4. 递归子文件夹 (-r)
$ grep -r "apache" /etc/init.d
5-5. 指定文件类型
$ grep -rn "xxx" . --include=*.c --include=*.php --exclude=*.h
------------------------------------------------------------
6. 正则表达式 (???)
6-1. '^$' (空行)
6-2. '^#' (以 # 开头)
$ cat httpd.conf | grep -v '^#' | grep -v '^$'
cat httpd.conf | grep -v -E '^#|^$'
------------------------------------------------------------
7. 静默模式 (不显示查询到的结果, 可用在判断语句中)
#! /bin/bash
str="abcde"
if echo $str | grep -q "a"; then
echo "true"
fi
if ! echo $str | grep -q "aa"; then
echo "false"
fi
echo $str | grep -q "a" ; echo $? # 0
echo $str | grep -q "aa" ; echo $? # 1
8. 指定关键字颜色
$ export GREP_OPTIONS=--color=auto
$ export GREP_COLOR='01;33'
$ grep "keyword" filename
------------------------------------------------------------
9. tail
9-1. 观测持续变化 (增加) 的文件
$ tail -f filename
$ tail -f filename|grep keyword
0 件のコメント:
コメントを投稿