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 ...
随机推荐
- layer loading层 的设置
/* shadeClose 类型:Boolean 默认:true,是否点击遮罩时关闭层 */ var tishi = layer.open({ shadeClose: false ,type: 2 , ...
- swp文件已存在
vim编辑某个文件时,提示.xxx.sh.swp文件已存在是因为异常退出后,linux会生成一个swp文件,无论选择什么,下次进入还是会提示ll 命令无法看到文件使用 rm -rf .xxx.sh.s ...
- Gym 101194A / UVALive 7897 - Number Theory Problem - [找规律水题][2016 EC-Final Problem A]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- English Time And Date
What's the Time in English? Explanation There are two common ways of telling the time. Formal but ea ...
- mongo笔记2
速度和持久性 用户可以选择写入语义,决定是否开启journaling日志记录,通过这种方式来控制速度和持久性的平衡,默认情况下写入都是fire-and-forget,写操作通过tcp套接字发送,不要求 ...
- you-get模块
You-Get是一个基于 Python 3 的下载工具.使用 You-Get 可以很轻松的下载到网络上的视频.图片及音乐. 转载https://www.cnblogs.com/wangchuanyan ...
- Linux-003-Resource temporarily unavailable
Jenkins构建任务向服务器发送war包时提示信息如下所示: 由上述信息可知通过SSH命令连接失败.通过Client连接服务器,提示信息如下: 提示信息说明资源暂时不可用. 原因一般是因为用户或应用 ...
- Redis的数据结构之字符串
五种数据类型: 字符串(String) 字符串列表(list) 有序字符串集合(sorted set) 哈希(hash) 字符串集合(set) Key定义的注意点: 不要过长, 不要过短, 统一的命名 ...
- python笔记-文件读写
文件操作过程一般为:打开.读写.关闭: 打开:open()或file() 读写:read().write(): 关闭:close(): 1.打开:open()或file() file_handler= ...
- 【Tools】-NO.89.Tools.4.Visual Studio 2017.1.001-【Visual Studio 2017 安装与卸载】-
1.0.0 Summary Tittle:[Tools]-NO.89.Tools.4.Visual Studio 2017.1.001-[Visual Studio 2017 安装与卸载]- Styl ...