Shell-04
- grep程序
Linux下文本处理三剑客-----grep sed awk
sed:文本行编辑器(流编辑器)
awk:报告生成器(文本输出格式化)
grep:文本行过滤工具 每一行进行过滤
pattern一般通过通配符和正则表达式两种方式进行匹配
包含三个命令:grep egrep fgrep,它们是用来进行 行模式(pattern)匹配的
Egrep = grep -E //使用扩展的正则表达式
Fgrep = fast grep //只使用文件通配符进行匹配 快速文件匹配 不调用正则表达式引擎
grep的用法:
grep [option] … PATTERN [filename]
A* 以A开头的任意文件 *---代表任意字符
*grep默认使用正则表达式进行文本匹配*
grep常见选项:
-E 支持使用扩展的正则表达式(ERE)regexp
-P 使用perl语言的正则表达式引擎进行搜索(每一种语言的正则表达式引擎都不相同,甚至sed、grep、awk使用的正则表达式引擎也不相同)
-i 忽略大小写
-v 进行反选
-o 仅仅输出匹配的内容(默认输出的是匹配到的行)
--color=auto 语法着色
-n 显示行号
-w 匹配固定的单词
- 正则表达式----正则表达式PATTERN
作用:通过一些特殊字符,来表示一类字符内容,然后交给前面的命令来执行;如果使用特殊字符本身的含义,就需要进行转义(\)
回顾文件通配符:* ? [] [^ ]
a) 字符匹配
. 代表任意一个字符==?
[] 范围内的任意一个字符
[^ ] 范围外的任意一个字符
字符类:[:digit:] [:alnum:] [:alpha:] [:lower:] [:upper:] [:space:] [:punct:]
b) 次数匹配
* 匹配前面相邻的一个字符0次到n次 n—无数次
\? 匹配前面相邻的一个字符0次到1次
\+ 匹配前面相邻的一个字符1次到n次
\{m\} 匹配前面相邻的一个字符m次
\{m,n\} 匹配前面相邻的一个字符m到n次
\{0,n\} 匹配前面相邻的一个字符0次到n次
\{m,\} 匹配前面相邻的一个字符至少m次
c) 位置锚定
^ 锚定行首
$ 锚定行尾
\b 锚定单词词首和锚定词尾
\> 锚定词尾
\< 锚定词尾
<\root\> --- 匹配root这个单词
d) 分组
abc*----c出现0-n次
abc看作整体,就要分组
\(\) 实例:\(abc\)* abcabcabc…
**分组特性:默认情况下,linux系统会为分组指定变量,变量的表示形式\1 \2 \3…..



1.grep -i “^s” /pro
2.grep -v “/b$” /e
3.sort -n -t: -k3 /etc/passwd | tail -1 | cut -d: -f1
4.alias grep =”grep “^root\>” --color=auto”
grep “^root\>” /etc/passwd | cut -d: -f7
grep “^root\>” /etc/passwd &> /dev/null && grep “^root\>” /etc/passwd &> /dev/null | cut -d: -f7
id root &> /dev/null && grep “^root\>” /etc/passwd &> /dev/null | cut -d: -f7
5.grep “[0-9]\{2,3\}” /etc/passwd
grep -w “[0-9]\{2,3\}\>” /etc/passwd
grep “\<[0-9]\{2,3\}\>” /etc/passwd

/etc/rc.d/rc.sysinit-------centos7没有文件
6. grep “^[[:space:]]\+ .*[^[:space:]]$” /etc/rc.d/rc.sysinit
7.netstat -tan ----- -t tcp -n
netstat -tan | grep “LISTEN[[:space:]]*$”
8.

-s指定用户使用的

grep “^\(bash\)\b.*\1$” /etc/passwd
grep “^\(bash\)\>.*\1$” /etc/passwd

grep “\(^[[:alnum:]]\+\>\).*\1$” /etc/passwd
9.IP地址:
0-255.0-255.0-255.0-255
0-255
2 0-4 0-9 2[0-4][0-9]
2 5 0-5 25[0-5]
1 0-9 0-9 1[0-9][0-9]
0 0-9 0-9 [0-9][0-9]
0 0 0-9 [0-9]
2[0-4][0-9] | 25[0-5] | 1[0-9][0-9] | [0-9][0-9] | [0-9] \. 2[0-4][0-9] | 25[0-5] | 1[0-9][0-9] | [0-9][0-9] | [0-9] \. 2[0-4][0-9] | 25[0-5] | 1[0-9][0-9] | [0-9][0-9] | [0-9] \. 2[0-4][0-9] | 25[0-5] | 1[0-9][0-9] | [0-9][0-9] | [0-9]

Shell-04的更多相关文章
- shell编程中用户输入处理(shell 04)
shell编程中用户输入处理1.命令行参数2.脚本运行时获取输入 命令行参数 通过空格来进行分割的位置参数 :$+position $0,$1,$2 ....$0 :程序名$1,$2,$3 ... $ ...
- Linux Shell 04 数字/字符串/文件测试
一. 数字测试 格式:n1 -op n2 测试操作op: eq/ne/le/ge/lt/gt --> 等于/不等于/小于等于/大于等于/小于/大于 1. 数字比较可以使用特殊的( ...
- Shell 04 字符串处理、正则表达式
一.字符串的处理 1.字符串截取 1.1 s{}表达式 ${变量名:起始位置:长度} (从0开始) n=number (n="number") echo ${#n} -- ...
- centos6安装部署git服务器(gitlab6.4)
环境准备 python版本2.6git版本 1.8.4.1ruby版本ruby-2.0.0-p353gitlab-shell版本 v1.8.0gitlab版本6.4.3 因centos6系列的pyth ...
- python练习六十二:文件处理,往文件中所有添加指定的前缀
往文件中所有添加指定的前缀 方法一:open方法 f_r = open('text.txt') f_w = open('text_new.txt','w+') i = 0 while True: i ...
- 【Linux】一步一步学Linux——初识Linux命令解析器(10)
目录 00. 目录 01. Shell简介 02. Shell分类 03. 交互式shell和非交互式shell 04. 登录shell和非登录shell 05. Shell类型 06. 参考 00. ...
- ubuntu16.04 安装power shell
ubuntu16.04 安装power shell # Download the Microsoft repository GPG keys wget -q https://packages.micr ...
- Ubuntu 16.04下在Shell终端下使用nautilus快速打开窗口文件夹
Ubunut 16.04默认使用nautilus进行管理资源文件夹,nautilus默认是支持参数传递的. 使用: nautilus /dirurl 打开当前文件夹(可以使用$PWD代替): naut ...
- ubuntu12.04中shell脚本无法使用source的原因及解决方法
现象: shell脚本中source aaa.sh时提示 source: not found 原因: ls -l `which sh` 提示/bin/sh -> dash 这说明是用dash来进 ...
- Ubuntu 14.04 在桌面上双击运行shell 脚本文件
http://askubuntu.com/questions/465531/how-to-make-a-shell-file-execute-by-double-click up vote7down ...
随机推荐
- 2018.03.29 python-pandas 数据透视pivot table / 交叉表crosstab
#透视表 pivot table #pd.pivot_table(data,values=None,index=None,columns=None, import numpy as np import ...
- DJ Java Decompiler
With DJ Java Decompiler you can decompile java class-files and save it in text or other format. It's ...
- 学习kettle遇到的问题
一. 解决mysql连接缺少驱动问题:http://www.mamicode.com/info-detail-1724584.html 1.下载驱动 https://dev.mysql.com/dow ...
- 【linux开发】ubuntu执行sudo apt-get update提示缺少公钥
ubuntu执行sudo apt-get update提示缺少公钥 提示信息如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 获取:1 http://arch ...
- Pyinstaller-封装python
1. 当程序中没有调用matplotlib模块 ① pip intall pyinstaller ② 在cmd环境下,pyinstaller -F xxx.py 2.当程序中调用matplotlib ...
- ipad已停用 连接itunes怎么办
问题描述: ipad 开机密码多次输入出错后,提示 ipad已停用 连接itunes 解决方法: 参考: https://jingyan.baidu.com/article/fb48e8bee9ef4 ...
- cdh平台问题
问题背景:内容的不懂之处,可以私信博主.友好交流使用.主要针对的问题种类有:网络桥接报错.网卡文件问题(该问题主要看你的安装脚本文件里面写的是否和主机对应,也是运行环境的问题).scm表中没有节点信息 ...
- 期货、股指期权、ETF期权
期货与期权: 期权是指一种合约,该合约赋予持有人在某一特定日期或该日之前的任何时间以固定价格购进或售出某种资产的权利. 期货是标准化的合约,赋予参与者在未来的某个时间点以约定好的一个价格去买入或者卖出 ...
- centos7 无法启动网络(service network restart)错误解决办法(转)
centos7 无法启动网络(service network restart)错误解决办法:(以下方法均为网上COPY,同时感谢原博主分享) systemctl status network.serv ...
- XLS导出的服务器端配置方式
IIS支持excel导出: 1.开始—运行,然后键入DCOMCNFG; 2.组件服务—计算机—我的电脑—DCOM配置,这时弹出一个问注册的窗口,确定注册. 这时如果一切恢复正常了,不用往下操作了. 关 ...