grep与正则表达式
grep与正则表达式
grep命令
grep(global searchregular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。
用法: grep [选项]... PATTERN [FILE]...
在每个 FILE 或是标准输入中查找 PATTERN。 默认的 PATTERN 是一个基本正则表达式(缩写为 BRE)。 例如: grep -i 'hello world' menu.h main.c
正则表达式选择与解释: -E,--extended-regexp PATTERN 是一个可扩展的正则表达式(缩写为 ERE) -F,--fixed-strings PATTERN 是一组由断行符分隔的定长字符串。 -G,--basic-regexp PATTERN 是一个基本正则表达式(缩写为 BRE) -P,--perl-regexp PATTERN 是一个 Perl 正则表达式 -e,--regexp=PATTERN 用 PATTERN 来进行匹配操作 -f,--file=FILE 从 FILE 中取得 PATTERN -i,--ignore-case 忽略大小写 -w,--word-regexp 强制 PATTERN 仅完全匹配字词 -x,--line-regexp 强制 PATTERN 仅完全匹配一行 -z,--null-data 一个 0 字节的数据行,但不是空行
Miscellaneous: -s,--no-messages 不显示错误信息 -v,--invert-match 反转查找 -V,--version 显示版本信息并退出 --help display thishelp text and exit
输出控制: -m,--max-count=NUM NUM 次匹配后停止 -b,--byte-offset 输出的同时打印字节偏移 -n,--line-number 输出的同时打印行号 --line-buffered 每行输出清空 -H,--with-filename 为每一匹配项打印文件名 -h,--no-filename 输出时不显示文件名前缀 --label=LABEL 将LABEL 作为标准输入文件名前缀 -o,--only-matching 只输出文件中匹配到的部分 -q,--quiet,--silent 不显示任何信息 --binary-files=TYPE assume thatbinary files are TYPE; TYPE is 'binary','text',or 'without-match' -a,--text equivalent to--binary-files=text -Iequivalent to --binary-files=without-match -d,--directories=ACTION how to handledirectories; ACTION is 'read','recurse',or 'skip' -D,--devices=ACTION how to handledevices,FIFOs and sockets; ACTION is 'read' or'skip' -r,--recursive like--directories=recurse -R,--dereference-recursive likewise,butfollow all symlinks --include=FILE_PATTERN search only filesthat match FILE_PATTERN --exclude=FILE_PATTERN skip files anddirectories matching FILE_PATTERN --exclude-from=FILE skip filesmatching any file pattern from FILE --exclude-dir=PATTERN directories that match PATTERN will be skipped. -L,--files-without-match print only names of FILEs containing no match -l,--files-with-matches print only names ofFILEs containing matches -c,--count print only a countof matching lines per FILE -T,--initial-tab make tabs line up(if needed) -Z,--null print 0 byte afterFILE name
文件控制: -B,--before-context=NUM 打印以文本起始的NUM 行 -A,--after-context=NUM 打印以文本结尾的NUM 行 -C,--context=NUM 打印输出文本NUM 行 -NUM same as--context=NUM --group-separator=SEP use SEP as a group separator --no-group-separator use emptystring as a group separator --color[=WHEN], --colour[=WHEN] use markersto highlight the matching strings; WHEN is 'always','never',or 'auto' -U,--binary do not strip CRcharacters at EOL (MSDOS/Windows) -u,--unix-byte-offsets report offsets asif CRs were not there (MSDOS/Windows)
‘egrep’即‘grep -E’。‘fgrep’即‘grep -F’。 直接使用‘egrep’或是‘fgrep’均已不可行了。 若FILE 为 -,将读取标准输入。不带FILE,读取当前目录,除非命令行中指定了-r 选项。 如果少于两个FILE 参数,就要默认使用-h 参数。 如果有任意行被匹配,那退出状态为 0,否则为 1; 如果有错误产生,且未指定 -q 参数,那退出状态为 2。
正则表达式 正则表达式就是处理字串的方法,他是以行为单位来进行字串的处理行为,正则表达式透过一些特殊符号的辅助,可以让使用者轻易的达到『搜寻/删除/取代』某特定字串的处理程序
基础正则表达式字符 (characters) 练习
2./etc/passwd文件中的两位数或三位数 解答: 3.'netstat -tan'命令结果中以‘LISTEN’后跟0个、1个或多个空白字符结尾的行; 解答: 4.添加用户bash、testbashbasher以及nologin用户(用户的shell为/sbin/nologin);而后找出文件中用户名与其名相同的行; 解答: 5.显示当前系统上rootcentos或者user1用户的默认和UID (请事先创建这些用户,若不存在) 解答 6.找出/etc/rc.d/init.d/functions文件中某单词(单词中间可以存在下划线)后面跟着一组小括号的行 解答: 7.使用echo输出一个路径,而后egrep找出其路径基名;进一步的使用取出其目录名 解答: 8.找出ifconfig命令执行结果中1-255之间的数字 解答: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ruby-on-rails – ActionMailer raise_delivery_errors在生
- xml 格式数据+详细页
- Ioc容器-Autofac之三-三种注册方式
- 关于近期曝光的针对银行SWIFT系统攻击事件综合分析
- reactjs – React-native / react-navigation:如何从`stat
- JSON.parse()和JSON.stringify()
- c# – 我的ArrayList在哪里?地铁应用
- c# – 是否可以使用Microsoft Automation UI单击WPF / WinF
- Function Maps: A Flexible Representation of Maps Betwee
- objective-c – Xcode – 这是MVC吗?