linux 正则表达式 之 sed 与awk

sed 可以将数据进行替换,删除,新增,选取等操作

sed [参数] [动作]
参数:
-n :使用安静模式 ,只有经过sed特殊处理的那一行才能被列出来
-i : 直接修改读取文件内容,而不是由屏幕输出
-e :直接在命令行模式上进行sed动作编辑
-r :sed的动作支持是拓展正则表达式语法,默认是基础正则表达式
动作说明 :[n1],[n2] function
[n1],[n2] 代表的是 n1行到n2 行

function 说明:
a :新增 a的后面可以接字符串,而字符串会在新的一行出现
c :替换 c的后面可以接字符串。这些字串可以替换n1-n2之间的行
d : 删除
i : 插入 i的后面可以接字符串。而这些字符串会在新的一行出现
p :打印出来,也就是将某个选择的数据打印出来
s :替换 可以直接进行替换的工作 ,通常搭配正则表达式

1.删除操作


[root@localhost tmp]# nl regular_express.txt | sed '2,5d'
     1  "Open Source" is a good mechanism to develop programs.
     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

2.在2行后添加 sxd???


[root@localhost tmp]# cat regular_express.txt  |  sed '2a sxd???'
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
sxd???
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh!     My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird

在第二行之前插入 将a改为i即可

3.将2-5行的内容替换成为 qqq


[root@localhost tmp]# nl regular_express.txt  |  sed '2,5c qqq'
     1  "Open Source" is a good mechanism to develop programs.
qqq
     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

4.打印第5-9行

[root@localhost tmp]# nl regular_express.txt  | sed -n '5,9p'
     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.

部分数据的查找并替换的功能
格式为 sed ‘s/要替换的字符/新的字符串/g’

5.删除注释的数据(以#开头的行)

[root@localhost tmp]# cat regular_express.txt | sed 's/#.*//g'
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.

GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh!     My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.

awk 适合处理小型的数据处理 awk 模式基本是
awk ‘条件1{动作1}条件2{动作2}’ 文件名
输出格式特殊样式
\n 换行
\t 水平的[Tab] 按键
\v 垂直的[tab]按键

BEGIN 第一步执行动作
END 最后结束执行动作
FS 指定分隔符

awk ‘{print 1}’   文件名                  打印文件的第一列  awk  ’ FS=”:”{print 1 "\t" 3}’        以: 为分隔符 打印1,2 列  awk  ’  {sum+= 1 } END{print sum}‘  将第一列数据相加

注意:awk处理数据是一行一行往下走

猜你喜欢

转载自blog.csdn.net/weixin_43142231/article/details/82716535