linux基础之grep
grep: Global search REgular expression and Print out the line
作用: 文本搜索工具,根据用户指定的模式对目标文本逐行进行匹配检查,打印匹配到的行
模式: 由 正则表达式字符及文本字符所编写的过滤条件
正则表达式内容可参考博文:https://www.cnblogs.com/ckh2014/p/10758769.html
grep [OPTIONS] PATTERN [FILE...]
选项:
--color=auto: 对匹配到的文本着色显示
-v : 显示不能够被pattern匹配到的行
-i : 忽略字符大小写
-o : 仅显示匹配到的单词
-q: 静默模式,不输出任何消息
-A # :after, 后#行
-B # :before,前#行
-C # : context, 前后各#行
-E ; 使用扩展的正则表达式
grep练习:
1. 显示/proc/meminfo 文件中以大小写s开头的行:(要求: 使用两种方式)
# grep "^[sS]" /proc/meninfo
# grep -i "^s" /proc/meminfo
2. 显示/etc/passwd文件中不以/bin/bash结尾的行
# grep -v "/bin/bash$" /etc/passwd
3. 显示/etc/passwd 文件中ID号最大的用户的用户名
# sort -t: -k 3 -n /etc/passwd | tail -1 | cut -d: -f1
4.如果用户root存在,显示其默认的shell程序
# grep "^root\>" /etc/passwd &> /dev/null | cut -d: -f7
# id root &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7
5. 找出/etc/passwd中的两位或三位数
# grep -o "\<[0-9]\{2,3\}\>" /etc/passwd
6.显示/etc/rc.d/rc.sysinit文件中,至少以一个空白字符开头的且后面存在非空白字符的行
# grep "^[[:space:]].*[^[:space:]].*"
# grep "^\([[:space:]]\).*[^\1].*"
# grep "^[[:space:]]"\+[^[:space:]]
7.找出netstat -tan 命令的结果中以 LISTEN 后跟0、1或多个空白字符结尾的行
# netstat -tan | grep "LISTEN[[:SPACE:]]*$"
8.添加用户bash、testbash、basher以及nologin(其shell为/sbin/nologin):而后找出/etc/passwd文件中用户名同shell名的行
# useradd -s /sbin/nologin nologin
# useradd bash
# grep "^\([[:alnum:]]\+\>\).*\1$" /etc/passwd
9. 写一个脚本,实现如下功能
如果user1用户存在,就显示其存在,否则添加之
显示添加的用户的id号等信息
#!/bin/bash
id user1 &> /dev/null && echo "user1已存在" || useradd user1
id user1
10. 写一个脚本,完成如下功能
如果root用户登录了当前系统,就显示root用户在线,否则说明其未登录
#!/bin/bash
w | grep "^root\>" &> /dev/null && echo "root logged" || echo "root not logged"
egrep = grep -E
egrep [OPTIONS] PATTERN [FILE...]
egrep练习:
1.显示当前系统root、centos或user1用户的默认shell和UID
# egrep "^root|centos|user1\>" /etc/passwd | cut -d: -f3,7
2.找出/etc/rc.d/init.d/functions文件(centos6)中某单词后面跟一个小括号的行
# egrep -o "^[_[:alnum:]]+\(\)" /etc/rc.d/init.d/functions
3.使用echo输出一绝对路径,使用egrep取出其基名
# echo "/etc/rc.d/init.d/functions" | egrep -o "[^\/]+" | tail -1
进一步:使用egrep取出路径的目录名,类似于dirname命令的结果
# echo "/tmp/init.d/function/base.txt" | egrep "(\/).*\1\b"
4.找出ifconfig命令结果中1-255之间的数值
# ifconfig | egrep "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"
linux基础之grep的更多相关文章
- Linux基础:Grep查询&AWK查询
Grep:搜索文件内匹配指定内容的行 常用的Grep搜索方法: 1. grep "普通搜索内容" file( 或者 cat file|grep "普通搜索内容" ...
- Linux基础正则表达式:grep,sed
先说明语系对正则表达式的影响 LANG=C:0,1,2,3,4...A,B,C,D...Z a b c d ... z LANG=zh_CN:0,1,2,3,4...a A b B c C ...
- 【Linux基础】grep命令
1.简介 grep是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. 命令格式:grep [option] pattern file 2.常用参数与举例: -e : 使用P ...
- Linux中通过grep命令检索文件内容和指定内容前后几行
原文链接: https://www.linuxidc.com/Linux/2017-11/148390.htm Linux系统中搜索.查找文件中的内容,一般最常用的是grep命令,另外还有egrep命 ...
- Linux基础命令-Nginx-正则表达式( grep sed awk )-Shell Script--etc
Linux基础使用 学习内容博客 内存 查看swap分区信息 > swapon -s 添加swap分区 > mkswap /dev/sdb2 > 激活 swapon -a /dev/ ...
- linux基础知识汇总(四)--ps grep命令
转:http://www.cnblogs.com/allen8807/archive/2010/11/10/1873843.html http://www.cnblogs.com/end/archiv ...
- 10、Linux基础--find、正则、文本过滤器grep
笔记 1.晨考 1.每个月的3号.5号和15号,而且这天是星期六时执行 00 00 3,5,15 * 6 2.每天的3点到15点,每隔3分钟执行一次 */3 3-15 * * * 3.每周六早上2点半 ...
- Linux基础练习题(二)
Linux基础练习题(二) 1.复制/etc/skel目录为/home/tuer1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限. [root@www ~]# cp -r ...
- Linux课程实践一:Linux基础实践(SSH)
一.SSH服务 1. 安装SSH (1)查看是否已经安装过ssh服务 rpm -qa |grep ssh (2)进行安装 sudo apt-get install openssh-server Ubu ...
随机推荐
- spring和jdbctemplate
1.spring与jdbc整合应用 a.增删改 -获取connection -获取statement -设置sql中?参数 -执行sql操作 -释放connection b.查询 -获取connect ...
- css3奇数偶数的伪属性
<style> /*奇数*/ ul li:nth-child(odd){ background-color: green; } /*偶数*/ ul li:nth-child(even){ ...
- Map 的putAll方法
如下段代码: public static void main(String[] args){ Map<String,String> map1 = new HashMap<>() ...
- 《JavaScript》 程序基本知识 数据类型。 {0912上} {0912下}
JS脚本语言: 这是JaxaScript的全称名 JS是网页里面使用的脚本语言 JS是一个非常强大的语言 JS的基础语法 注释语法: 单行注释 // 多行注释 /**/ 输出语法: 双标 ...
- PHP(表单元素)
表单: 1.收集用户的输入,发送到后台 <form action="后台地址" method="提交方式" enctype="multipart ...
- 异常HTTP Status 500 - Illegal access to constructor, is it public? java.lang.IllegalAccessException: Class com.opensymphony.xwork2.ObjectFactory can not access a member of class action.CoreAction with
Exception report message Illegal access to constructor, is it public? description The server encount ...
- 01day
01 cpu 内存 硬盘 操作系统 CPU:中央处理器,相当于人大脑. (运行速度飞机) 内存:临时存储数据. 8g,16g, (高铁) 1,成本高. 2,断电即消 ...
- [No0000186]治愈系课程教材 第一课
一部分:时态 时态有时间和特点组成 时间:现在.过去.将来 特点:一般.完成.进行.完成进行 所以时态总共有12种(加上过去将来的时间又多出4种时态,总共16种) 一般现在时 一般过去时 一般将来时 ...
- CentOS 7.6 安装 Weblogic 12
http://download.oracle.com/otn/nt/middleware/12c/12213/fmw_12.2.1.3.0_wls_Disk1_1of1.zip java -jar f ...
- 《Mysql 用户管理》
一:数据库用户 ROOT 和 其他用户有什么区别么? - Mysql root 和 linux root 不是一回事,数据库 root 只不过是初始化时候自己建立的一个用户而已,随时可以删除/修改. ...