Linux 课堂笔记

第十一课(2018-06-04)

2018-06-06  本文已影响0人  chocolee911

目录

  1. 正则表达式
  2. 扩展正则表达式
  3. grep
  4. sed
  5. awk

1. 正则表达式

Regular Expression, 通过一些特殊字符的排列, 配合上部分工具, 达到"搜索/取代/删除"具有某些特征的字符串的目的.
说白了, RE 就是用于标记字符串的一种表达式

符号 含义 示例
[list] 代表一个字符, 方括号内为该字符的穷举 [0-9] 代表数字; [a-z] 代表全部小写;[akw] 代表 a/k/w 中选一个
[^] 代表一个字符, 表示取反, 即不取方括号内的任何值 [^0-9] 代表非数字;[^a-z] 代表非小写; [akw] 代表不是 a/k/w 中的任何一个
^word 代表行首, 后接特征, 代表以该特征作为行首 ^9 代表以9作为行首
word$ 代表行尾, 前接特征, 代表以该特征作为行尾 9$ 代表以9作为行尾
. 代表有且只有任意一个字符 a.g 代表 a 和 g 之间有且只有任意一个字符 如 abg akg a/g a%g 等
* 代表0~无穷个前面那一个, 前接一个字符 .* 代表任意个任意字符; a* 代表0~无穷个 a; aa* 代表1~无穷个 a
{n,m} 代表某个字符连续重复的次数, 前接字符, 中间为次数范围, 但花括号在 RE 中属于特殊字符, 需要转义 o\{2\} 代表2个 o; o\{2,5\} 代表2至5个连续的 o;

2. 扩展正则表达式

RE Extended 字符 含义 示例
char+ >=1 个char a+ 代表1个或更多的a
char? <=1 个char a? 代表0个或1个的a
PATTERN1|PATTERN2 用或的方式进行匹配 gd | good :代表查找关键字 gd 或 good
( ) 查找字符,常与 | 配合使用 g(la|oo)d 代表查找关键字 glad 或 good
( )+ 同一字符多次重复的查找 (xyz)+ 代表查找关键字 xyz 或 xyzxyz 或 xyzxyzxyz...

3. grep

查找、筛选。根据特征进行文本的查找。以行为单位进行查找。查找完成后打印到屏幕。

3.1 用法

grep -[options] 'pattern' <file_name>

<command> | grep -[option] 'pattern'

3.2 选项

3.3 选项示例

其实示例中应该包含RE相关的知识,但其实RE仅仅是个通用的表达式,跟工具无关。所以在本示例中不再加入过多关于RE使用技巧的介绍

[root@choco-01 grep]# pwd
/root/grep
[root@choco-01 grep]# ls     
regular_express.txt
[root@choco-01 grep]# nl regular_express.txt 
     1  "Open Source" is a good mechanism to develop programs.
     2  apple is my favorite food.
     3  Football game is not use feet only.
     4  this dress doesn't fit me.
     5  However, this dress is about $ 3183 dollars.
     6  GNU is free air not free beer.
     7  Her hair is very beauty.
     8  I can't finish the test.
     9  Oh! The soup taste good.
    10  motorcycle is cheap than car.
    11  This window is clear.
    12  the symbol '*' is represented as start.
    13  Oh! My god!
    14  The gd software is a library for drafting programs.
    15  You are the best is mean you are the no. 1.
    16  The world <Happy> is the same with "glad".
    17  I like dog.
    18  google is the best tools for search keyword.
    19  goooooogle yes!
    20  go! go! Let's go.
    21  # I am VBird
[root@choco-01 grep]# grep 'GNU' regular_express.txt 
GNU is free air not free beer.
[root@choco-01 grep]# grep -n 'GNU' regular_express.txt   
6:GNU is free air not free beer.
[root@choco-01 grep]# grep -v 'o' regular_express.txt 
Her hair is very beauty.
I can't finish the test.
\# I am VBird
[root@choco-01 grep]# grep 'happy' regular_express.txt 


[root@choco-01 grep]# grep -i 'happy' regular_express.txt 
The world <Happy> is the same with "glad".
[root@choco-01 grep]# grep 'Happy' regular_express.txt 
The world <Happy> is the same with "glad".


[root@choco-01 grep]# grep -o 'Happy' regular_express.txt 
Happy
[root@choco-01 grep]# grep 'god' regular_express.txt 
Oh! My god!


[root@choco-01 grep]# grep -c 'god' regular_express.txt 
1
[root@choco-01 grep]# grep -n 'go' regular_express.txt  
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
13:Oh!  My god!
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.


[root@choco-01 grep]# grep -n -e 'go' -e 'GNU' regular_express.txt  
1:"Open Source" is a good mechanism to develop programs.
6:GNU is free air not free beer.
9:Oh! The soup taste good.
13:Oh!  My god!
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
[root@choco-01 grep]# grep -n 'GNU' regular_express.txt 
6:GNU is free air not free beer.


[root@choco-01 grep]# grep -n -A2 'GNU' regular_express.txt 
6:GNU is free air not free beer.
7-Her hair is very beauty.
8-I can't finish the test.
[root@choco-01 grep]# grep -n 'GNU' regular_express.txt 
6:GNU is free air not free beer.


[root@choco-01 grep]# grep -n -B2 'GNU' regular_express.txt 
4-this dress doesn't fit me.
5-However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
[root@choco-01 grep]# grep -n 'GNU' regular_express.txt 
6:GNU is free air not free beer.
[root@choco-01 grep]# grep -n -C2 'GNU' regular_express.txt 
4-this dress doesn't fit me.
5-However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
7-Her hair is very beauty.
8-I can't finish the test.

4. sed

查找、截取、替换、新增、删除。

4.1 用法

sed [options] line1,line2[action] [file_name]

[command] | sed [options] line1,line2[action]

4.2 Option

4.3 Action

4.3 示例

[root@choco-01 sed]# nl test.txt 
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@choco-01 sed]# nl test.txt | sed '3,4p'
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin


[root@choco-01 sed]# nl test.txt | sed -n '3,4p'
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@choco-01 sed]# nl test.txt | sed '3,4d'
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@choco-01 sed]# nl test.txt  | sed '2a this is a new line'
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
this is a new line
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@choco-01 sed]# nl test.txt  | sed '2i this is a new line'
     1  root:x:0:0:root:/root:/bin/bash
this is a new line
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@choco-01 sed]# nl test.txt  | sed '2i 1st added line\
> 2nd added line\
> 3rd added line'
     1  root:x:0:0:root:/root:/bin/bash
1st added line
2nd added line
3rd added line
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@choco-01 sed]# nl test.txt | sed '2,3c this line replaces the old 2&3 line'
     1  root:x:0:0:root:/root:/bin/bash
this line replaces the old 2&3 line
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sed 's/PATTERN/new/g'

[root@choco-01 sed]# nl test.txt 
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@choco-01 sed]# nl test.txt | sed 's/sbin/SBIN/'
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/SBIN/nologin
     3  daemon:x:2:2:daemon:/SBIN:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/SBIN/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/SBIN/nologin
[root@choco-01 sed]# nl test.txt | grep 'root' | sed 's/root/ROOT/'
     1  ROOT:x:0:0:root:/root:/bin/bash
[root@choco-01 sed]# nl test.txt | grep 'root' | sed 's/root/ROOT/g'
     1  ROOT:x:0:0:ROOT:/ROOT:/bin/bash
[root@choco-01 sed]# nl test.txt 
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@choco-01 sed]# sed -i 's/sbin/SBIN/' test.txt 
[root@choco-01 sed]# nl test.txt 
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/SBIN/nologin
     3  daemon:x:2:2:daemon:/SBIN:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/SBIN/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/SBIN/nologin

5. awk

(The End)

上一篇 下一篇

猜你喜欢

热点阅读