Linux三剑客(上)

简介: Linux三剑客(上)

一、正则表达式

由特殊字符和文本字符组成编写模式。主要分类:

  • 基本正则表达式BRE
  • 元字符有:^$.[]*
  • 作用:1、匹配字符;2、匹配字符次数;3、位置锚定
  • 扩展正则表达式ERE
  • 元字符有:(){}?+| (包括BRE)
  • 必须使用 grep -E 才能生效

1.2正则表达意义

  1. 处理大量的字符串 (提取关键字信息、文件信息、网站信息)
  2. 处理文本(替换功能)
  3. 正则表达式必须使用Linux三剑客去操作它。

二、Linux三剑客grep

  • grep:文本搜索、过滤工具

    2.1、grep练习
##首先连接文件passwd并打印输出到 pwd.txt文件中
[root@1-VM00013 ~]# cat /etc/passwd > ./pwd.txt
##开始搜索过滤,不区分大小写,查找pwd.txt文件中含有root字样的字符
[root@1-VM00013 ~]# grep -i "root" pwd.txt
root:x:0:0:root:/root
operator:x:11:0:operator:/root:/sbin/nologin
dockerroot:x:988:98
##把行号显示出来,查找pwd.txt文件中含有root字样的字符
[root@1-VM00013 ~]# grep -i -n "root" pwd.txt
1:root:x:0:0:root:
10:operator:x:11:0:operator:/root:/sbin/nologin
42:dockerroot:x:
##统计pwd.txt文本中,有多少行root有关的字符
[root@1-VM00013 ~]# grep -i "root" ./pwd.txt -c
3
[root@1-VM0001
##找出所有的非空行
 思路是先找出所有的空行,然后结果取反
 [root@1-VM00013 data]# cat luffy.txt
I am linghu
I teach linux
i like python
my name is linghu
[root@1-VM00013data]# grep '^$' luffy.txt
##查看空行的行号:
[root@1-VM00013 data]# grep '^$' luffy.txt -n
2:
4:
6:
8:
##把结果取反
[root@1-VM00013 data]# grep '^$' luffy.txt -n -v
1:I am linghu
3:I teach linux
5:i like python
7:my name is linghu

现在在文本中加入注释行,我们要排除注释行:

[root@1-VM00013 data]# grep '^$' luffy.txt -v | grep '^#' -v
I am linghu
I teach linux
i like python
my name is linghu

Linux三剑客(下)+https://developer.aliyun.com/article/1623587

目录
相关文章
|
12月前
|
Linux
linux下的GetTickCount
linux下的GetTickCount
|
1天前
|
Linux
Linux
linux关机重启 登录注销 shutdown -h now 立刻关机 shutdown -h 1 一分钟后重启 shutdown -r now 重启计算机 halt 关机 reboot 重启计算机 sync 内存数据同步到磁盘 linux关机重启 登录注销
|
2月前
|
网络协议 Unix Linux
linux
inux是一种免费使用和自由传播的类UNIX操作系统,其内核由林纳斯·本纳第克特·托瓦兹于1991年10月5日首次发布。它主要受到Minix和Unix思想的启发,是一个基于POSIX的多用户、多任务、支持多线程和多CPU的操作系统。Linux支持32位和64位硬件,能运行主要的Unix工具软件、应用程序和网络协议。由于Linux是开源的,因此全世界的Linux爱好者都可以对Linux进行修改和再发布。‌
|
5月前
|
存储 缓存 安全
Linux小记
Linux小记
36 0
|
Linux 开发工具
Linux wired unmanageed
这是一个有线连接无法时行图形化界面管理的问题 在状态栏上有线连接的图标显示为 wired unmanageed image.png 插上网线也不知道是什么情况,这时要对它的配置文件进行设置一下 打开网络管理器的配置文件 huanyu@ubuntu:/etc/NetworkManager$ sudo vim NetworkManager.
1327 0
|
Shell Linux 数据安全/隐私保护
|
Shell 网络协议 Perl
|
Linux Perl 关系型数据库