grep命令应用举例 查询多个文件,可以使用通配符“*” grep "math2"*.txt grep "12"* ◆反向匹配 ps aux I grep "ssh" grep -v "grep" ◆ 匹配空行 grep -n datafile grep -v datafile datafile2
6 grep 命令应用举例 查询多个文件,可以使用通配符 “ * ” grep "math2" *.txt grep "12" * 反向匹配 ps aux | grep "ssh" | grep –v "grep" 匹配空行 grep -n '^$' datafile grep -v '^$' datafile > datafile2
grep命令应用举例 ◆精确匹配单词:1<和小> ●找出所有包含以north开头的单词的行 grep '\<north'datafile ●找出所有包含以west结尾的单词的行 grep 'west\>datafile ●找出所有包含north单词的行 grep \<north\>datafile
7 精确匹配单词: \< 和 \> grep 'west\>' datafile grep '\<north' datafile grep '\<north\>' datafile grep 命令应用举例 z 找出所有包含 以 north 开头 的单词的行 z 找出所有包含 以 west 结尾 的单词的行 z 找出所有包含 north 单词的行
grep命令应用举例 ◆递归搜索目录中的所有文件:-x grep -r "north"datafile ~/Teaching/linux/ ◆关于某个字符连续出现次数的匹配 grep 'o\2,\}'helloworld 'o\{2,4\}',o\{2,4\',1o\{2,4\} 8
8 递归搜索目录中的所有文件:-r grep -r "north" datafile ~/Teaching/linux/ 关于某个字符连续出现次数的匹配 grep 'o\{2,\}' helloworld 'o\{2,4\}' , 'o\{2,4\}' , 'lo\{2,4\}' grep 命令应用举例
grep命令应用举例 ◆其它 grep n'datafile grep 'ys datafile grep 'r\.'datafile grep [we]'datafile grep -i'ss*'datafile grep -n [a-z]\{9\}'datafile grep -c '\<[a-z].*n\>datafile
9 其它 grep '^n' datafile grep 'y$' datafile grep 'r\.' datafile grep '^[we]' datafile grep –i 'ss*' datafile grep –n '[a-z]\{9\}' datafile grep –c '\<[a-z].*n\>' datafile grep 命令应用举例
grep与管道 1s -1 I grep d' 如果传递给grep的文件名参数中有目录的话, 需使用“-d”选项 grep -d [ACTION]directory name 其中ACTION可以是 read:把目录文件当作普通文件来读取 skip:目录将被忽略而跳过 recurse:递归的方式读取目录下的每一个文件,可以用 选项"-r"代替"-d recurse" grep -rl "etho"/etc 10
10 ls –l | grep '^d' 如果传递给 grep 的文件名参数中有目录的话, 需使用 “ -d ” 选项 grep -d [ACTION] directory_name 其中 ACTION 可以是 read:把目录文件当作普通文件来读取 skip:目录将被忽略而跳过 recurse:递归的方式读取目录下的每一个文件,可以用 选项 "-r" 代替 "-d recurse" grep -rl "eth0" /etc grep 与管道