Linux查⽂件中的字符串命令
正则匹配文件名grep -nr 'archermind'
-r, --recursive
Read all files under each directory, recursively, following symbolic links
only if they are on the command line. This is equivalent to the -d
recurse option.
-n, --line-number
Prefix each line of output with the 1-based line number within its input
file. (-n is specified by POSIX.)
grep介绍  (1)grep 的⼀般格式为 grep [options] 基本正则表达式 [⽂件]
字符串参数最好采⽤是双引号括,⼀是以防被误解为shell命令,⼆是可以⽤来查多个单词组成的字符串      -c:只输出匹配⾏的记数
-i:不区分⼤⼩写(只适⽤于单个字符)
-h:查询多个⽂件时不显⽰⽂件名
-H:只显⽰⽂件名
-l:查询多⽂件时只输出包含匹配字符的⽂件名
-n:只显⽰匹配⾏及其⾏号
-s:不显⽰不存在或⽆匹配⽂本的错误信息。
-v:显⽰不包含匹配⽂本的所有⾏。
(2)举例说明:
grep ^[^210] myfile 匹配myfile中以⾮2、1、0开头的⾏
grep "[5-8][6-9][0-3]" myfile 匹配myfile中第⼀位为5|6|7|8,第⼆位6|7|8|9,第三位为0|1|2|3的三个字符的⾏      grep "4\{2,4\}" myfile 匹配myfile中含有44,444或4444的⾏
grep "\?" myfile匹配myfile中含有任意字符的⾏
(3)grep命令类名
[[:upper:]]  表⽰[A-Z]
[[:alnum:]]  表⽰[0-9a-zA-Z]
[[:lower:]]  表⽰[a-z]
[[:space:]]  表⽰空格或者tab键
[[:digit:]]  表⽰[0-9]
[[:alpha:]]  表⽰[a-zA-Z]
如:grep "5[[:digit:]][[:digit:]]" myfile 匹配myfile中含有5开头接下去两位都是数字的⾏。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。