13 Shell Script grep命令

grep命令

一、grep命令介绍

​ 介绍:

​ Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户

​ grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板,搜索的结果被送到标准输出,不影响原文件内容

​ grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。我们利用这些返回值就可进行一些自动化的文本处理工作

二、grep命令格式

​ grep命令不仅仅用于过滤/搜索的特定字符。还可以使用正则表达式、多种命令配合使用,使用上十分灵活

​ grep命令格式

​ grep [option] pattern file

[root@localhost ~]# grep after /etc/profile
            if [ "$2" = "after" ] ; then
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after

三、grep命令选项

​ 命令参数:

​ -A除了显示符合范本样式的那一行之外,并显示该行之后的内容

[root@localhost ~]# grep -A 2 good /etc/profile 
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this

​ -b在显示符合样式的那一行之前,标示出该行下标位==(该行第一字符在整个文件中下标)

[root@localhost ~]# grep -b good /etc/profile  
123:# It's NOT a good idea to change this file unless you know what you

​ -B除了显示符合样式的那一行之外,并显示该行之前的内容

[root@localhost ~]# grep -B 2 good /etc/profile
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you

​ -c计算符合样式的行数

[root@localhost ~]# grep -c good /etc/profile
1

​ -d 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作

[root@localhost ~]# touch /opt/{a,b,c}.txt

# 跳过目录
[root@localhost ~]# ls /opt | grep -d skip c
c.txt

四、grep使用实例

一)查找指定进程

​ 说明:第一条记录是查找出的进程;第二条结果是grep进程本身,并非真正要找的进程

[root@localhost ~]# ps -ef | grep mysql
root       1587   1361  0 03:13 pts/0    00:00:00 grep --color=auto mysql

二)查找指定进程的个数

[root@localhost ~]# ps -ef | grep -c mysql
1

三)从文件中读取关键词进行搜索

​ grep -f参数的说明

​ -f<规则文件> --file=<规则文件> #指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式

[root@localhost ~]# cat keyword.txt                     
good
after

[root@localhost ~]# cat /etc/profile | grep -f keyword.txt 
# It's NOT a good idea to change this file unless you know what you
            if [ "$2" = "after" ] ; then
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after

四)从文件中查找关键词

​ 从单个文件中查找关键词

​ 参数-n --line-number #在显示符合样式的那一行之前,标示出该行的列数编号

[root@localhost ~]# grep -n good /etc/profile           
6:# It's NOT a good idea to change this file unless you know what you

​ 从多个文件中查找关键词

[root@localhost ~]# cat test1.txt
go

[root@localhost ~]# grep good /etc/profile test1.txt
/etc/profile:# It's NOT a good idea to change this file unless you know what you
test1.txt:good

五)找出自定义规则开头的行内容

​ 找出以#开头的行内容

[root@localhost ~]# grep ^# /etc/profile            
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
# Path manipulation
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file

​ 找出以非#开头的行内容:

[root@localhost ~]# grep ^[^#] /etc/profile
pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}
if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    fi
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done
unset i
unset -f pathmunge
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值