Grep 命令 用法大全
查找x文件 find / -name "x*" -ls
查找文件中x所在的行数
grep -n "x" -r *
find . -name "*.java" | xargs grep -n "x*"
1、 参数:
-I :忽略大小写
-c :打印匹配的行数
-l :从多个文件中查找包含匹配项
-v :查找不包含匹配项的行
-n:打印包含匹配项的行和行标
2、RE(正则表达式)
\ 忽略正则表达式中特殊字符的原有含义
^ 匹配正则表达式的开始行
$ 匹配正则表达式的结束行
\< 从匹配正则表达式的行开始
\> 到匹配正则表达式的行结束
[ ] 单个字符;如[A] 即A符合要求
[ - ] 范围 ;如[A-Z]即A,B,C一直到Z都符合要求
. 所有的单个字符
* 所有字符,长度可以为0
3、举例
# ps -ef | grep in.telnetd
root 19955 181 0 13:43:53 ? 0:00 in.telnetd
# more size.txt size文件的内容
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
a013386
b044525
m8987131
B081016
M45678
B103303
BADc2345
# more size.txt | grep '[a-b]' 范围 ;如[A-Z]即A,B,C一直到Z都符合要求
b124230
b034325
a081016
a022021
a061048
b103303
a013386
b044525
# more size.txt | grep '[a-b]'*
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
a013386
b044525
m8987131
B081016
M45678
B103303
BADc2345
# more size.txt | grep 'b' 单个字符;如[A] 即A符合要求
b124230
b034325
b103303
b044525
# more size.txt | grep '[bB]'
b124230
b034325
b103303
b044525
B081016
B103303
BADc2345
# grep 'root' /etc/group
root::0:root
bin::2:root,bin,daemon
sys::3:root,bin,sys,adm
adm::4:root,adm,daemon
uucp::5:root,uucp
mail::6:root
tty::7:root,tty,adm
lp::8:root,lp,adm
nuucp::9:root,nuucp
daemon::12:root,daemon
# grep '^root' /etc/group 匹配正则表达式的开始行
root::0:root
# grep 'uucp' /etc/group
uucp::5:root,uucp
nuucp::9:root,nuucp
# grep '\<uucp' /etc/group
uucp::5:root,uucp
# grep 'root$' /etc/group 匹配正则表达式的结束行
root::0:root
mail::6:root
# more size.txt | grep -i 'b1..*3' -i :忽略大小写
b124230
b103303
B103303
# more size.txt | grep -iv 'b1..*3' -v :查找不包含匹配项的行
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
a013386
b044525
m8987131
B081016
M45678
BADc2345
# more size.txt | grep -in 'b1..*3'
1:b124230
9:b103303
15:B103303
# grep '$' /etc/init.d/nfs.server | wc -l
128
# grep '\$' /etc/init.d/nfs.server | wc –l 忽略正则表达式中特殊字符的原有含义
15
# grep '\$' /etc/init.d/nfs.server
case "$1" in
>/tmp/sharetab.$$
[ "x$fstype" != xnfs ] &&
echo "$path\t$res\t$fstype\t$opts\t$desc"
>>/tmp/sharetab.$$
/usr/bin/touch -r /etc/dfs/sharetab /tmp/sharetab.$$
/usr/bin/mv -f /tmp/sharetab.$$ /etc/dfs/sharetab
if [ -f /etc/dfs/dfstab ] && /usr/bin/egrep -v '^[ ]*(#|$)'
if [ $startnfsd -eq 0 -a -f /etc/rmmount.conf ] &&
if [ $startnfsd -ne 0 ]; then
elif [ ! -n "$_INIT_RUN_LEVEL" ]; then
while [ $wtime -gt 0 ]; do
wtime=`expr $wtime - 1`
if [ $wtime -eq 0 ]; then
echo "Usage: $0 { start | stop }"
# more size.txt
the test file
their are files
The end
# grep 'the' size.txt
the test file
their are files
# grep '\<the' size.txt
the test file
their are files
# grep 'the\>' size.txt
the test file
# grep '\<the\>' size.txt
the test file
# grep '\<[Tt]he\>' size.txt
the test file
==================================================================
1,简介
使用正则表达式的一个多用途文本搜索工具.这个php?name=%C3%FC%C1%EE" onclick="tagshow(event)" class="t_tag">命令本来是ed行编辑器中的一个php?name=%C3%FC%C1%EE" onclick="tagshow(event)" class="t_tag">命令/过滤器:
g/re/p -- global - regular expression - print.
基本格式
grep pattern [file...]
(1)grep 搜索字符串 [filename]
(2)grep 正则表达式 [filename]
在文件中搜索所有 pattern 出现的位置, pattern 既可以是要搜索的字符串,也可以是一个正则表达式.
注意:在输入要搜索的字符串时最好使用双引号/而在模式匹配使用正则表达式时,注意使用单引号
2,grep的选项
-c 只输出匹配行的计数
-i 不区分大小写(用于单字符)
-n 显示匹配的行号
-v 不显示不包含匹配文本的所以有行
-s 不显示错误信息
-E 使用扩展正则表达式
更多的选项请查看:man grep
3,常用grep实例
(1)多个文件查询
grep "sort" *.doc #见文件名的匹配
(2)行匹配:输出匹配行的计数
grep -c "48" data.doc #输出文档中含有48字符的行数
(3)显示匹配行和行数
grep -n "48" data.doc #显示所有匹配48的行和行号
(4)显示非匹配的行
grep -vn "48" data.doc #输出所有不包含48的行
(4)显示非匹配的行
grep -vn "48" data.doc #输出所有不包含48的行
(5)大小写敏感
grep -i "ab" data.doc #输出所有含有ab或Ab的字符串的行
4, 正则表达式的应用
(1)正则表达式的应用 (注意:最好把正则表达式用单引号括起来)
grep '[239].' data.doc #输出所有含有以2,3或9开头的,并且是两个数字的行
(2)不匹配测试
grep '^[^48]' data.doc #不匹配行首是48的行
(3)使用扩展模式匹配
grep -E '219|216' data.doc
(4) ...
这需要在实践中不断应用和总结,熟练掌握正则表达式。
5, 使用类名
可以使用国际模式匹配的类名:
[[:upper:]] [A-Z]
[[:lower:]] [a-z]
[[:digit:]] [0-9]
[[:alnum:]] [0-9a-zA-Z]
[[:space:]] 空格或tab
[[:alpha:]] [a-zA-Z]
(1)使用
grep '5[[:upper:]][[:upper:]]' data.doc #查询以5开头以两个大写字母结尾的行
Grep 命令 用法大全的更多相关文章
- linux命令——Grep 命令 用法大全
1. 参数: -I :忽略大小写 -c :打印匹配的行数 -l :从多个文件中查找包含匹配项 -v :查找不包含匹配项的行 -n:打印包含匹配项的行和行标 2.RE(正则表达式) \ 忽略正则表达式中 ...
- grep命令用法
linux中grep命令的用法 作为linux中最为常用的三大文本(awk,sed,grep)处理工具之一,掌握好其用法是很有必要的. 首先谈一下grep命令的常用格式为:grep [选项] ”模 ...
- <三剑客> 老三:grep命令用法
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...
- ADB命令用法大全
一.ADB简介 Android Debug Bridge,安卓调试桥,它借助adb.exe(Android SDK安装目录platform-tools下),用于电脑端与模拟器或者真实设备交互:使用 ...
- Linux中find命令用法大全
Linux 查找命令是Linux系统中最重要和最常用的命令之一.查找用于根据与参数匹配的文件指定的条件来搜索和查找文件和目录列表的命令.查找可以在各种条件下使用,您可以通过权限,用户,组,文件类型,日 ...
- linux find命令用法大全
本文档格式如下,命令说明在命令的上一行,尝试使用 Ctrl+f 组合键快速在页面内搜索. 命令说明 命令 参数起始目录:查找文件的起始目录.实例# 当前目录搜索所有文件,文件内容 包含 &qu ...
- linux中的grep命令用法
原文请移驾:http://blog.csdn.net/greytree/article/details/428532 grep -- print lines matching a pattern (将 ...
- grep 命令操作
linux grep命令 1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expressi ...
- 由一条Linux的grep命令说起
今天在开发的时候,看到同事使用了这样的一条linux命令 grep 'class YourClass' -rwi * |grep -v svn 想到了 grep命令的,几个参数. -r 明确要求搜索子 ...
随机推荐
- UNIX command Questions Answers asked in Interview
UNIX or Linux operating system has become default Server operating system and for whichever programm ...
- 初识Redis
package com.wangzhu.redis; import java.util.List; import org.junit.After; import org.junit.Before; i ...
- SQL Server Profiler监控SQL Server性能
全面掌握SQL Server Profiler 1. 原理与相关概念介绍 SQL Server Profiler,大家已经非常熟悉.常常在性能优化中使用,本文档详细介绍SQL Server ...
- java:I/O 字节流和字符流
字节流 InputStream和OutputStream的子类:FileInputStream 和 FileOutputStream 方法: int read(byte[] b,int off,int ...
- 在java程序中访问windows有用户名和密码保护的共享目录
在java程序中访问windows有用户名和密码保护的共享目录 Posted on 2015-11-20 14:03 云自无心水自闲 阅读(3744) 评论(0) 编辑 收藏 --> Jav ...
- 转AOP 介绍
来自:http://blog.csdn.net/a906998248/article/details/7514969 这篇也不错,详细介绍了CGLIP http://blog.jobbole.com/ ...
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序(老罗学习笔记3)
简单来说,硬件驱动程序一方面分布在Linux内核中,另一方面分布在用户空间的硬件抽象层中.接着,在Ubuntu上为Android系统编写Linux内核驱动程序(老罗学习笔记1)一文中举例子说明了如何在 ...
- Android通过JNI调用驱动程序(完全解析实例)
要达到的目的:android系统中,用JAVA写界面程序,调用jni中间库提供的接口,去操作某个驱动节点,实现read,writer ioctl等操作!这对底层驱动开发人员是很重要的一个调试通道,也是 ...
- Android开发之关于ListView中adapter调用notifyDataSetChanged无效的原因
1.数据源没有更新,调用notifyDataSetChanged无效. 2.数据源更新了,但是它指向新的引用,调用notifyDataSetChanged无效. 3.数据源更新了,但是adpter没有 ...
- WebActivatorEx
using System; using NLog; using System.Web.Optimization; [assembly: WebActivatorEx.PreApplicationSta ...