当前位置: 首页 > news >正文

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
2.次数匹配
 * (贪婪模式) 做次数匹配,匹配*前面的字符0,1或多次,只表示次数,不表示字符

只做次数匹配,01次,写法是:  \?

{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
3.锚定符<>^$
 \<                      锚定词首       ####找到以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结尾的行
4.大括号的使用:在复制较长路径的文件并重命名时可使用大括号
 
 [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
 
5.wc命令:统计作用。(word count)
 
 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
6.tr命令:转换显示的结果。
 
 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
7.cut:剪切
 -c :按照字符数量切 (characters)

-d :按指定分隔符切(delimiter 定界符)

-f:指定要显示的字段  (file)

单个数字-->一个字段

逗号分隔的多个数字-->指定多个离散字段

-  -->连续字段,3-5表示35字段

例子:
 [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
8.sort排序: sort [options] file ...
   -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
 

 

转载于:https://www.cnblogs.com/lbg-database/p/10109956.html

相关文章:

  • ajax跨域问题
  • 菜根谭#89
  • Kubernetes上的十大应用程序
  • 开发技巧:高效的使用 Response.Redirect
  • 正则表达式-基础知识Review
  • Andrew Ng机器学习公开课笔记 -- 线性回归和梯度下降
  • 四则运算1
  • Windows API 第15篇 GetVolumeInformation 获取磁盘卷(驱动器)信息
  • 看完这篇文章,你还觉得Python难吗?
  • 使用AIR进行移动APP开发常见功能和问题(上)
  • 应用el-tabs模拟nav menu组件
  • “ an error occurred during ssl communication”--VisualSVN
  • mybatis 动态SQL .1
  • 从零开始编写自己的C#框架(2)——开发前准备工作
  • 下列关于异常处理的描述中,错误的是()。
  • 【Linux系统编程】快速查找errno错误码信息
  • Create React App 使用
  • CSS魔法堂:Absolute Positioning就这个样
  • HTML5新特性总结
  • Javascript弹出层-初探
  • js递归,无限分级树形折叠菜单
  • leetcode98. Validate Binary Search Tree
  • Linux快速复制或删除大量小文件
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • Redux系列x:源码分析
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • sublime配置文件
  • vue+element后台管理系统,从后端获取路由表,并正常渲染
  • 聊聊hikari连接池的leakDetectionThreshold
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 浅谈Golang中select的用法
  • 手写双向链表LinkedList的几个常用功能
  • 小程序 setData 学问多
  • 你对linux中grep命令知道多少?
  • JavaScript 新语法详解:Class 的私有属性与私有方法 ...
  • 阿里云IoT边缘计算助力企业零改造实现远程运维 ...
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • ​软考-高级-信息系统项目管理师教程 第四版【第19章-配置与变更管理-思维导图】​
  • $(function(){})与(function($){....})(jQuery)的区别
  • (Redis使用系列) Springboot 整合Redisson 实现分布式锁 七
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (十八)SpringBoot之发送QQ邮件
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容
  • .NET 读取 JSON格式的数据
  • .NET 依赖注入和配置系统
  • .NET开源的一个小而快并且功能强大的 Windows 动态桌面软件 - DreamScene2
  • .net图片验证码生成、点击刷新及验证输入是否正确
  • .NET中 MVC 工厂模式浅析
  • .project文件
  • :“Failed to access IIS metabase”解决方法
  • @Import注解详解
  • @RequestParam详解
  • [ 隧道技术 ] cpolar 工具详解之将内网端口映射到公网
  • []T 还是 []*T, 这是一个问题
  • [100天算法】-目标和(day 79)