grep/字符/次数匹配/锚定符/小大括号/wc/tr/cut/sort/uniq
grep:正则表达式,文本过滤工具,能够实现以指定的"模式(Pattern)"逐行搜索文件中的内容,并将匹配到的行显示出来.
模式:是由正则表达式的元字符,其他字符组合起来的匹配字符。
每一类正则表达式本身的表达式是需要用户自己去写的,但表达式的元字符都有着固定的或者特定的意义,我们可以根据自己的需要去理解或者组合字符,生成我们需要的模式
| -v:显示不被模式匹配到的行,invert-match
-i:在做模式匹配的时候不区分大小写ignore-case -o:只显示匹配到的串,而非默认显示匹配到的行,only-matching -A 数字:A=after,显示到匹配到的行后,还显示每一个匹配项下面*行的内容 -B 数字:before,前*行 -C 数字:context,上下文*行 -E:扩展的正则表达式 |
例子:
| [root@lbg test]# grep -o 'root' /etc/passwd --只显示字段,不显示整行。
[root@lbg test]# grep -A 1 'root' /etc/passwd --显示该行和下一行内容 [root@lbg test]# grep -B 2 'root' /etc/passwd --显示该行和其前两行内容。 |
1.字符匹配( . [] [^] [:space:] [:punct:])
| . 匹配任意单个字符(制表符,空格,标点,字母,数 字,其他符号)
[] 匹配指定范围内的任意单个字符 [^] 指定范围外的单个字符 [:space:] 匹配空白字符,包括空格,tab, [:punct:] 标点符号(用法同空白字符) |
例子:
| [root@lbg test]# grep 'r[oa]t' /etc/passwd operator:x:11:0:operator:/root:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin |
| * (贪婪模式) 做次数匹配,匹配*前面的字符0,1或多次,只表示次数,不表示字符
? 只做次数匹配,0或1次,写法是: \? {m,n} 最少m次,最多n次 写法为: \{m,n\} 用{m,n}时可以没有上限,但下限一定要有. 且 ?和{}都要加\. |
例子:
| [root@lbg test]# cat 1 ab a b a b a b [root@lbg test]# grep 'a b' 1 --匹配1个空格 a b [root@lbg test]# grep 'a *b' 1 --匹配0及以上的空格 ab a b a b a b a b [root@lbg test]# grep 'a \?b' 1 --匹配0或1个空格 ab a b [root@lbg test]# grep 'a \{2,3\}b' 1 ---匹配2或3个空格 a b a b [root@lbg test]# grep 'a \{2,\}b' 1 --匹配2个及以上空格 a b a b a b |
| \< 锚定词首 ####找到以r..t开头的内容 \
\> 锚定词尾 ### r..t\> ^ 脱字符 行首锚定 $ 行尾锚定 ##### root$ -->必须以root结尾 |
例子:
| [root@lbg test]# grep '\
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@lbg test]# grep 'ot\>' /etc/passwd ---ot结尾的单词
root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin setroubleshoot:x:993:988::/var/lib/setroubleshoot:/sbin/nologin [root@lbg test]# grep '^ro' /etc/passwd --以ro开头的行 root:x:0:0:root:/root:/bin/bash [root@lbg test]# grep 'bash$' /etc/passwd --以bash结尾的行 |
| [root@lbg test]# ls ---无文件 [root@lbg test]# touch a --创建文件a [root@lbg test]# cp /test/a{,1} --将a文件复制,并在其原路径下重命名为a1. [root@lbg test]# ls a a1 |
| wc [options]... file_name
-l 仅显示行数(line) -w 仅显示单词数(word) -c 仅显示字节数(char) 单独的wc显示的信息依次是:行数 单词数 字节数 文件名 |
例子:
| [root@lbg test]# cat a how do you do how do you do [root@lbg test]# wc a ---显示4种信息 2 8 28 a [root@lbg test]# wc -l a --显示行数 2 a [root@lbg test]# ls /test a b c d [root@lbg test]# ls /test | wc -l ----使用wc统计目录下文件的个数 4 |
| tr:转换字符或者删除字符(tr -d '字符集合' --delete )
tr '集合1' '集合2' --将集合1里的内容按集合2对应位置的内容替换显示出来。 |
例子:
| [root@lbg test]# echo 'abcdabcd' |tr 'ac' '13' --两边集合一一对应的情况 1b3d1b3d [root@lbg test]# echo 'abcdabcd' |tr 'acd' '13' --集合1多于集合2的情况 1b331b33 [root@lbg test]# echo 'abcdabcd' |tr 'acd' '1234' --集合1少于集合2的情况 1b231b23 [root@lbg test]# echo 'abcdabcd' |tr 'a-z' 'A-Z' --大小写转换 ABCDABCD [root@lbg test]# echo 'abcdabcd' |tr -d 'ab' ---删除字符 cdcd |
| -c :按照字符数量切 (characters)
-d :按指定分隔符切(delimiter 定界符) -f:指定要显示的字段 (file) 单个数字-->一个字段 逗号分隔的多个数字-->指定多个离散字段 - -->连续字段,3-5表示3到5字段 |
例子:
| [root@lbg test]# echo 'there are many dogs'|cut -d ' ' -f 2 --按空格切 are [root@lbg test]# echo 'there are many dogs'|cut -d ' ' -f 2,4 --第2个和第4个 are dogs [root@lbg test]# echo 'there are many dogs'|cut -d ' ' -f 2-4 --2到4个 are many dogs [root@lbg test]# echo 'there are many dogs'|cut -d m -f 1 --以m切割 there are [root@lbg test]# echo 'there are many dogs'|cut -d m -f 2 --以m切割 any dogs [root@lbg test]# echo 'how do you do'|cut -c 2-5 ---取第2到第5个字符 ow d |
| -f:忽略字符大小写(ignore-case)
-n:对数值进行排序 //Sort 默认字符排序 加,-n 数字排序. -r : 逆序输出 -t:指定分隔符 -k:基于哪个字段进行排序 (key) -u:uniq,重复的行只显示一行 (unique) |
例子:
| [root@lbg test]# cut -d : -f 3 /etc/passwd |sort -n [root@lbg test]# cat a 234 123 234 [root@lbg test]# sort -u a 123 234 [root@lbg test]# sort -n a 123 234 234 [root@lbg test]# sort -n -u a 123 234 |
9.uniq:去重复行。
| -c:统计每一行出现的次数(count)
-d:仅显示重复过的行 -u:仅显示未重复行 注意uniq去重,认定是连续的才是重复的.不是连续的不会去重. |
例子:
| [root@lbg test]# cat a 234 234 123 234 [root@lbg test]# uniq a 234 123 234 [root@lbg test]# sort a | uniq -c 1 123 3 234 [root@lbg test]# sort -u -n a 123 234 |
10.小括号向前引用功能
\1--引用从左到右的第一个括号内范围.
\2--引用从左到右的第二个括号内范围.
例子:
| [root@lbg test]# cat a he love his lover she like her liker [root@lbg test]# grep 'l..e.*e' a he love his lover she like her liker [root@lbg test]# grep '\(l..e\).*\1' a ---与命令grep 'l..e.*e' a相同 he love his lover she like her liker [root@lbg test]# grep '\(l\(..\)e\).*\1' a ---与命令grep 'l..e.*e' a相同 he love his lover she like her liker [root@lbg test]# grep 'l..e.*l..' a he love his lover she like her liker [root@lbg test]# grep '\(l\(..\)e\).*\2' a --与grep 'l..e.*l..' a 相同 he love his lover she like her liker |
grep/字符/次数匹配/锚定符/小大括号/wc/tr/cut/sort/uniq的更多相关文章
- 线上问题debug过程(cat,grep,tr,awk,sort,uniq,comm等工具的综合使用)
问题:发现线上到货单的数量,小于实际到货的数量. 怀疑一些隐藏的条件,将部分唯一码进行了过滤,导致数量变少. 开展了如下的跟踪流程: 1.找到其中一个明细的唯一码 grep 6180e-4b09f p ...
- php正则:匹配(),{},[]小括号,大括号,中括号里面的内容
php正则:匹配(),{},[]小括号,大括号,中括号里面的内容 比如有一段字符: $s='60c8 {"code":"200","message&q ...
- grep中正则匹配的使用
如要匹配Computer或computer两个单词,可做如下操作: [Cc]mputer “.”允许匹配ASCII集中任意字符,或为字母,或为数字. 使用\{\}匹配模式结果出现的次数 匹配字母A出现 ...
- grep 常用正则匹配
1.或操作 grep -E '123|abc' filename // 找出文件(filename)中包含123或者包含abc的行 egrep '123|abc' filename // 用egrep ...
- Linux篇---Grep和正则匹配
一.前述 Linux中正则匹配查找比较常用,所以分享一篇关于正则匹配和Grep结合的文章. 二.匹配规则 匹配操作符: \ 转义字符. ...
- 第11.11节 Python正则表达式的指定重复次数匹配模式及元字符”{}”功能介绍
在<第11.8节 Pytho正则表达式的重复匹配模式及元字符"?". "". "+"功能介绍>和<第11.10节 Pyth ...
- Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助
Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助 目录 Pychar ...
- grep是模糊匹配
1. 我:我用的ps -nat|grep -i "80"|wc -l命令 我:解释详细点,,龙哥,对于我这种菜鸟:也是模糊匹配 :你用 grep "80" 会匹 ...
- 《c程序设计语言》读书笔记--每行一个单词打印输入的字符,除去空符
#include <stdio.h> int main() { int c; while((c = getchar()) != EOF) { if(c != '\n' && ...
随机推荐
- 推荐算法之: DeepFM及使用DeepCTR测试
算法介绍 左边deep network,右边FM,所以叫deepFM 包含两个部分: Part1: FM(Factorization machines),因子分解机部分 在传统的一阶线性回归之上,加了 ...
- 扫描仪扫描文件处理-Photoshop批处理无响应问题
问题描述:Photoshop批处理时候卡死.卡住.无响应问题(出现在处理60M及以上TIFF文件的时候) 解决办法: 调整系统虚拟内存见<扫描-Photoshop批处理内存不足问题解决> ...
- go panic
panic 抛出异常 通过recover捕获 类似 php python等语言的try catch package mainimport ( "fmt" "errors& ...
- java List<T>和List<Object>的区别
// List<T> 的T表示的是某一类型可以用人一类型来替代,一般在定义的时候使用 // List<Object> 就是具体的了表示这个List里只能放置Object pub ...
- js实现无缝连接轮播图(二)实现自定义属性,根据banner图片的数量动态生成小圆点
<!-- 这个animate.js 必须写到 index.js的上面引入 --><script src="js/animate.js"></scrip ...
- java抓取东方财富股票数据(附源码)
背景 前段时间给朋友写了一个自动抓取同花顺股票数据的程序,不少人觉得不错. 这几天后台有粉丝给我留言让我也抓一下东方财富的数据,说东方财富的数据特别难抓,我还真不一定能搞得定. 本来我是一个德艺双磬且 ...
- xUtils简介和使用方法
xUtils简介 xUtils 包含了很多实用的android工具. xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓词) ...
- for循环结构中的3个表达式缺一不可?
do-while循环结构结束条件是while后的判断语句不成立for循环结构中的3个表达式都可以为空的.
- pyqy5 程序实例1
import sys from PyQt5.QtWidgets import QMainWindow,QApplication from PyQt5.QtGui import QIcon class ...
- non-local denoising methods
NL-Means算法 在噪声先验为高斯噪声的基础上, 进行non-local的平均,在2005年由Baudes提出,该算法使用自然图像中普遍存在的冗余信息来去噪声.与常用的双线性滤波.中值滤波等利用图 ...