巧用Grep 命令的小案例集锦

   2017-01-06 0
核心提示:例1 在文件中查找模式(单词)在/etc/passwd文件中查找单词“linuxtechi”root@Linux-world:~#greplinuxtechi/etc/passwdlinuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#例2 在多个文件中查找模式root@Linux-world:~#grep

巧用Grep 命令的小案例集锦

例1 在文件中查找模式(单词)

在/etc/passwd文件中查找单词“linuxtechi”

root@Linux-world:~# grep linuxtechi /etc/passwd 
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash 
root@Linux-world:~# 

例2 在多个文件中查找模式

root@Linux-world:~# grep linuxtechi /etc/passwd /etc/shadow /etc/gshadow 
    /etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash 
    /etc/shadow:linuxtechi:$6$DdgXjxlM$4flz4JRvefvKp0DG6re:16550:0:99999:7:::/etc/gshadow:adm:*::syslog,linuxtechi 
    /etc/gshadow:cdrom:*::linuxtechi 
    /etc/gshadow:sudo:*::linuxtechi 
    /etc/gshadow:dip:*::linuxtechi 
    /etc/gshadow:plugdev:*::linuxtechi 
    /etc/gshadow:lpadmin:!::linuxtechi 
    /etc/gshadow:linuxtechi:!:: 
    /etc/gshadow:sambashare:!::linuxtechi 
root@Linux-world:~# 

例3 使用-L参数列出包含指定模式的文件的文件名

root@Linux-world:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab 
    /etc/passwd 
    /etc/shadow 
root@Linux-world:~# 

例4 使用-N参数,在文件中查找指定模式并显示匹配行的行号

root@Linux-world:~# grep -n linuxtechi /etc/passwd 
    39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash 
root@Linux-world:~# 

例5 使用-V参数输出不包含指定模式的行

输出/etc/passwd文件中所有不含单词“linuxtechi”的行

root@Linux-world:~# grep -v linuxtechi /etc/passwd 

例6 使用 ^ 符号输出所有以某指定模式开头的行

Bash脚本将 ^ 符号视作特殊字符,用于指定一行或者一个单词的开始。例如输出/etc/passes文件中所有以“root”开头的行

root@Linux-world:~# grep ^root /etc/passwd  
root:x:0:0:root:/root:/bin/bash  
root@Linux-world:~# 

例7 使用 $ 符号输出所有以指定模式结尾的行

输出/etc/passwd文件中所有以“bash”结尾的行

root@Linux-world:~# grepbash$ /etc/passwd  
root:x:0:0:root:/root:/bin/bash  
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash  
root@Linux-world:~# 

Bash脚本将美元($)符号视作特殊字符,用于指定一行或者一个单词的结尾

例8 使用 -R 参数递归地查找特定模式

root@Linux-world:~# grep -r linuxtechi /etc/ 
    /etc/subuid:linuxtechi:100000:65536 
    /etc/group:adm:x:4:syslog,linuxtechi 
    /etc/group:cdrom:x:24:linuxtechi 
    /etc/group:sudo:x:27:linuxtechi 
    /etc/group:dip:x:30:linuxtechi 
    /etc/group:plugdev:x:46:linuxtechi 
    /etc/group:lpadmin:x:115:linuxtechi 
    /etc/group:linuxtechi:x:1000: 
    /etc/group:sambashare:x:131:linuxtechi 
    /etc/passwd-:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash 
    /etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash 
............................................................................ 

上面的命令将会递归的在/etc目录中查找“linuxtechi”单词

例9 使用 Grep 查找文件中所有的空行

root@Linux-world:~# grep ^$ /etc/shadow  
root@Linux-world:~# 

由于/etc/shadow文件中没有空行,所以没有任何输出

例10 使用 -I 参数查找模式

grep命令的-i参数在查找时忽略字符的大小写。我们来看一个例子,在paswd文件中查找“LinuxTechi”单词

nextstep4it@localhost:~$ grep -i LinuxTechi /etc/passwd 
    linuxtechi:x:1001:1001::/home/linuxtechi:/bin/bash 
nextstep4it@localhost:~$ 

例11 使用 -E 参数查找多个模式

例如,我想在一条grep命令中查找‘linuxtechi’和‘root’单词,使用-e参数,我们可以查找多个模式

root@Linux-world:~# grep -e "linuxtechi" -e "root" /etc/passwd  
root:x:0:0:root:/root:/bin/bash  
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash  
root@Linux-world:~# 

例12 使用 -F 用文件指定待查找的模式

首先,在当前目录中创建一个搜索模式文件“grep_pattern”,我想文件中输入的如下内容

root@Linux-world:~# cat grep_pattern 
    ^linuxtechi 
    root 
    false$ 
root@Linux-world:~# 

现在,试试使用grep_pattern文件进行搜索

root@Linux-world:~# grep -f grep_pattern /etc/passwd 

例13 使用 -C 参数计算模式匹配到的数量

继续上面例子,我们在grep命令中使用-c命令计算匹配指定模式的数量

root@Linux-world:~# grep -c -f grep_pattern /etc/passwd 
    22 
root@Linux-world:~# 

例14 输出匹配指定模式行的前或者后面N行

a)使用-B参数输出匹配行的前4行

root@Linux-world:~# grep -B 4"games" /etc/passwd 

b)使用-A参数输出匹配行的后4行

root@Linux-world:~# grep -A 4"games" /etc/passwd 

c)使用-C参数输出匹配行的前后各4行

root@Linux-world:~# grep -C 4"games" /etc/passwd 
 
标签: Linux命令
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 每天一条linux命令——halt
    halt命令用来关闭正在运行的Linux操作系统。halt命令会先检测系统的runlevel,若runlevel为0或6,则关闭系统,否则即调用shutdown来关闭系统。 语法:halt(选项)选项:-d:不要在wtmp中记录;-f:不论目前的runlevel为何,不调用shutdown即强制关闭系统;-i:
    02-10
  • 几个linux命令查看堆栈内存问题 几个linux命令
      公司编写代码,发现整个在程序里面调用system时候总是失败,原因是can't allocate memory ,根本原因不是内存不足,而是堆栈内存不够。使用ulimit -s 2048后有所好转但是发现一旦系统进程开启多了后仍然会挂。后来使用objdump -x 后发现了原因有个动态库
    02-09
  • Linux命令总结--rm命令 linux的rmp命令
    (来源于http://www.cnblogs.com/xqzt/p/5398919.html)1、命令简介rm(Remove file 删除目录或文件)删除文件,对于链接文件,只是删除整个链接文件,而原有文件保持不变。新手在删除文件之前一定要知道这些:很重要1.可以用mv代替rm的绝对不要用rm,不要着急
    02-09
  • Linux命令行批量创建目录详解 linux创建目录
    以前一直用-p创建目录链,觉得很方便了。在空目录/opt/app/myapp里创建src,再创建main,再创建javamkdir -p /opt/app/myapp/src/main/java没想到还可以这样玩##¥%……*(root@vm1:~/tmp# mkdir -p src/{{main,test}/{java,resources},main/webapp}root@vm1:
    02-09
  • Linux命令之乐--script和scriptplay
    script和scriptplay可以把终端会话记录到一个文件中,可以用来制作命令行教学视屏。 开始录制会话[root@new test]# script -t 2timing.log -a output.sessionScript started, file is output.session[root@new test]# echo hellohello[root@new test]# echo
    02-09
  • java 开发常用的Linux命令
    1.查找文件 find / -name filename.txt 根据名称查找/目录下的filename.txt文件。find . -name "*.xml" 递归查找所有的xml文件find . -name "*.xml" |xargs grep "hello world" 递归查找所有文件内容中包含hello world的xml文件grep -H 'spring' *.xml 查找所
    02-09
  • 常用Linux命令整理 Linux常用命令总结
    常用Linux命令整理常见系统命令export 查看或修改环境变量# 例:临时修改命令提示符为字符串$export PS1=$# 例:临时修改命令提示符显示系统时间 时间使用\t 表示export PS1="[\u@\h \t \W]\$"man 查看linux系统的手册# 例:查看ls命令如何使用man ls # 回车
    02-09 Linux
  • linux命令行—《命令行快速入门》 linux常用命令csdn
    linux命令行—《命令行快速入门》 linux常用命
    pwd print working directory 打印工作目录hostname my computer's network name 电脑在网络中的名称mkdir make directory 创建路径cd change directory 改变路径ls list directory 列出路径下的内容rmdir remove directory 删除路径pushd push directory 推
    02-09
  • Linux命令(五) 五个常用的linux命令
    Linux命令(五) 五个常用的linux命令
    jenkins触发钉钉报警机制1、安装钉钉插件钉钉报警需要安装Ding Talk插件,支持钉钉报警2、打开钉钉创建钉钉机器人2.1选择机器人类型——自定义2.2添加机器人2.3填写机器人信息填写机器人姓名—设置安全设置为加密类型,并复制出来加密值2.4创建完成保持webhoo
    02-09
  • 03_Linux基础-文件类型-主辅提示符-第1提示符-Linux命令-内外部命令-快捷键-改为英文编码-3个时间-stat-其他基础命令
    03_Linux基础-文件类型-主辅提示符-第1提示符-L
    03_Linux基础-文件类型-主辅提示符-第1提示符-Linux命令-内外部命令-快捷键-改为英文编码-3个时间-stat-{1..100}-du-cd-cp-file-mv-echo-id-shell-ln-env-set-which-rpm-/usr-/与/root-hostname-vim-cat-pwd-alias-unalias-ls-ASCII-wc-chmod博客
    02-09
点击排行