Linux基础-固化命令的方式grep

配置文件目录:

/etc/profile
/etc/bashrc
/root/.bashrc
/root/.bash_profile


/bin/bash

root用户一登陆,给他提供什么命令解释器

 

什么是grep?

grep(缩写来自Globally search a Regular Expression and Print)是一种强大的全局文本搜索工具,它能使用特定模式匹配(包括正则表达式)搜索文本,并默认输出匹配行。

被shell解释的,被命令解释的,而命令是被shell解释的,所以shell>命令>正则表达式。

shell是父辈,通配符和命令一个级别是儿子辈,正则表达式,是孙子辈的。
shell >命令/通配符->正则表达式

Linux 中,命令、通配符是被shell解释的,正则表达式由命令解释

管道符 过滤 ls | grep hello

grep -A 2 'root' /etc/passwd 关键字匹配处后两行
grep -B 2 'root' /etc/passwd 关键字匹配处前两行
grep -C 2 'root' /etc/passwd 关键字匹配处前后各两行
grep -c 2 'root' /etc/passwd 统计数量

grep -i 'hello' a.txt 大小写都输出来
grep -iv 'hello' a.txt 对大小写都输出来的取反

grep '^root' test
grep 'root$' test

grep 'ab*' a.txt
grep 'ab?' a.txt

egrep 'r[abc123\.\/\-]t' b.txt

注意转义\- 必须放到结尾才能转义结束

grep -v "^#" /etc/ssh/ssh_config 过滤掉开头为#的注释
grep -v "^ *$" /etc/ssh/sshd_config 匹配空行并过滤

egrep 'compan(y|ies)' a.txt

 

posted @ 2017-09-06 16:57  Adamanter  阅读(264)  评论(0编辑  收藏  举报