grep 对字符的处理

  • grep 的格式
    grep [参数] 匹配条件 处理文件
    主要参数 [ ]:
    -c : 只输出匹配的行
    -i : 不区分大小写
    -h : 查询多文件时不显示文件名
    -l : 查询多文件时, 只输出包含匹配字符的文件名
    -n : 显示匹配的行号及行
    -v : 显示不包含匹配文本的所有行,即反转查找
    -E: 将范本样式为延伸的普通表示法来使用,意味着能使用扩展正则表达式

演示一
linux——grep 文本过滤器

  • grep root passwd
    linux——grep 文本过滤器
  • grep ^root passwd
    linux——grep 文本过滤器
  • grep -i ^root passwd
    linux——grep 文本过滤器
  • grep root$ passwd
    linux——grep 文本过滤器
  • grep -i -E "^root|root$" passwd
    linux——grep 文本过滤器
  • grep -i -E -v "^root|root$" passwd
    linux——grep 文本过滤器
  • egrep "^root|root$" passwdgrep -E "^root|root$" passwd
    linux——grep 文本过滤器
  • grep -i -E -v "^root|root$" passwd | grep root
    linux——grep 文本过滤器

演示二
* 字符出现[0-任意次]
? 字符出现[0-1次]
+ 字符出现[1-任意次]
{n} 字符出现[n次]
{m,n} 字符出现[最少m次,最多n次]
{,n} 字符出现[最多n次]
{m,} 字符出现[至少m次]
linux——grep 文本过滤器
linux——grep 文本过滤器
linux——grep 文本过滤器
(xy){n} 关键字出现xy出现n次]
.* 关键字之间匹配任意字符
linux——grep 文本过滤器
linux——grep 文本过滤器
grep -E 'rt' test
grep -E 'ro.*t' test
grep -E 'r....' test
grep -E 'r....\>' test
grep -E '...t' test
grep -E '\<...t' test
linux——grep 文本过滤器
linux——grep 文本过滤器
linux——grep 文本过滤器

相关文章: