sed和awk的用法

sed语句

&

用正则表达式匹配的内容进行替换

&(\(?0[0-9]{2,3}\)?-?)?[0-9]{5,8}所匹配到的内容
电话:& 是在所匹配到的内容前面加 电话:
[root@localhost ~]# grep -E '(\(?0[0-9]{2,3}\)?-?)?[0-9]{5,8}' c
010-1334772
027-1225583
1334243
(0714)-8750342
12306
[root@localhost ~]# sed -E 's/(\(?0[0-9]{2,3}\)?-?)?[0-9]{5,8}/电话:&/g' c
电话:010-1334772
电话:027-1225583
电话:1334243
电话:(0714)-8750342
电话:12306
1

.Ah “Major Heading” 转换为@A HEAD = Major Heading

/^\.Ah/{
s/\. Ah*/\
\
@A HEAD = /
s /" //g
s/$/\
/
}

第一个替换命令用两个换行符和“@A HEAD =”取代“.Ah”,在行结尾处有必要用反斜杠转义换行符。

第二个替换删除了引号。

[root@localhost ~]# cat f
.Ah "Major Heading"
[root@localhost ~]# sed '/^\.Ah/{s/\.Ah */@A HEAD = / }' f
@A HEAD = "Major Heading"
[root@localhost ~]# sed '/^\.Ah/{s/\.Ah */@A HEAD = /;s/"//g}' f
@A HEAD = Major Heading
[root@localhost ~]# 
^$

删除空行

[root@localhost ~]# cat f
.Ah "Major Heading"



.Ah "Major Heading"

[root@localhost ~]# sed '/^$/d' f
.Ah "Major Heading"
.Ah "Major Heading"
[root@localhost ~]# 
a

追加

指定内容后面加

[root@localhost ~]# cat c
010-1334772
027-1225583
1334243
027-12345
(0714)-8750342
12306
//指定后面追加jjyy
[root@localhost ~]# sed '/027-1225583/ajjyy' c
010-1334772
027-1225583
jjyy
1334243
027-12345
(0714)-8750342
12306
//多个后面追加
[root@localhost ~]# sed -r '/027-[0-9]{5,7}/ajjyy' c
010-1334772
027-1225583
jjyy
1334243
027-12345
jjyy
(0714)-8750342
12306

[root@localhost ~]# 
i

插入

指定内容前面加

//指定前面插入
[root@localhost ~]# sed '/027-1225583/ijjyy' c
010-1334772
jjyy
027-1225583
1334243
027-12345
(0714)-8750342
12306

[root@localhost ~]# 
//加空格需要转义
[root@localhost ~]# sed '/027-1225583/i\    jjyy' c
010-1334772
    jjyy
027-1225583
1334243
027-12345
(0714)-8750342
12306

[root@localhost ~]# 
c

替换

//把第三行替换为123
[root@localhost ~]# sed '3c123' c
010-1334772
027-1225583
123
027-12345
(0714)-8750342
12306

[root@localhost ~]# 
y

转换

[root@localhost ~]# cat x
a
b
c
xyz
sdfhhjkj
[root@localhost ~]# sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHJIKLMNOPWRSTUVWXYZ/' x
A
B
C
XYZ
SDFHHIKI
[root@localhost ~]# 

[root@localhost ~]# sed 'y/ab/BA/' x
B
A
c
xyz
sdfhhjkj
[root@localhost ~]# 
//指定行号替换
[root@localhost ~]# sed '3y/c/C/' x
a
b
C
xyz
sdfhhjkj
[root@localhost ~]# 

-n

关掉默认打印功能

[root@localhost ~]# sed -n '/xyz/p' x
xyz
[root@localhost ~]# 
//打印行号
[root@localhost ~]# sed '=' x
1
a
2
b
3
c
4
xyz
5
sdfhhjkj
[root@localhost ~]# 
n,d

n:读取指定内容下面一行

d:删除

删除指定内容下面的空行
[root@localhost ~]# cat xx
.H1 "On Egypt"

Napoleon,pointing to the Pyramids,said to his troops:
"Soldiers,forty centuries have their eyes upon you."

123
[root@localhost ~]# 
[root@localhost ~]# sed '/^\.H1/{n;/^$/d}' xx
.H1 "On Egypt"
Napoleon,pointing to the Pyramids,said to his troops:
"Soldiers,forty centuries have their eyes upon you."

123
[root@localhost ~]# 
q

退出

[root@localhost ~]# cat c
010-1334772
027-1225583
1334243
027-12345
(0714)-8750342
12306
//打印两行退出
[root@localhost ~]# sed '2q' c
010-1334772
027-1225583
[root@localhost ~]# 

高级命令

模式空间

N

当前行和下一行放到一个模式空间,可以理解两行为一行

我们假设想要将“Owner and Operator Guide”换成“Installation Guide”,但是我们发现它出现在文件中的两行上,“Operator”和“Guide”被分开了。

[root@localhost ~]# cat yz
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
[root@localhost ~]# 

[root@localhost ~]# sed -r '/Operator$/{N;s/Owner and Operator\nGuide/Installation Guide/g}' yz
Consult Section 3.1 in the Installation Guide for a description of the tape drives
available on your system.
[root@localhost ~]# sed -r '/Operator$/{N;s/Owner and Operator\nGuide/Installation Guide\n/g}' yz
Consult Section 3.1 in the Installation Guide
 for a description of the tape drives
available on your system.
[root@localhost ~]# 


//如果Owner and Operator Guide在多个地方
/Owner$/{N;s/Owner *\n*and *\n*Operator *\n*Guide/Installation Guide/g}‘
或
[root@localhost ~]# cat yyz
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
Look in the Owner and Operator Guide shipped with your system.
Two manuals are provided including the Owner and
Operator Guide and the User Guide.
The Owner and 0perator Guide is shipped with your system.
[root@localhost ~]#
[root@localhost ~]# sed -r 's/Owner and Operator/Installation Guide/;/Owner/{N;s/Owner and Operator\nGuide/Installation Guide\n/;/Owner/{N;s/Owner and Operator\nGuide/Installation Guide\n/}}' yyz
Consult Section 3.1 in the Installation Guide
Guide for a description of the tape drives
available on your system.
Look in the Installation Guide Guide shipped with your system.
Two manuals are provided including the Owner and
Operator Guide and the User Guide.
The Owner and 0perator Guide is shipped with your system.
[root@localhost ~]# 



//取消注释 变成一行
[root@localhost ~]# cat h
# System timezone
timezone Asia/Shanghai --utc
[root@localhost ~]# 
[root@localhost ~]# sed 's/^# //;/System/{N;s/timezone\ntimezone/timezone/}' h
System timezone Asia/Shanghai --utc
[root@localhost ~]# 

D

多行删除
把模式空间的一行删掉(本来两行)

[root@localhost ~]# cat uuu
This line is followed by 1 blank line.

This line is followed by 2 blank line.


This line is followed by 3 blank line.



This line is followed by 4 blank line.




This is the end.
[root@localhost ~]# 

//遇到两行删一行
[root@localhost ~]# sed '/^$/{N;/^\n$/D}' uuu
This line is followed by 1 blank line.

This line is followed by 2 blank line.

This line is followed by 3 blank line.

This line is followed by 4 blank line.

This is the end.
[root@localhost ~]# 
//奇数留1,偶数全删
[root@localhost ~]# sed '/^$/{N;/^\n$/d}' uuu
This line is followed by 1 blank line.

This line is followed by 2 blank line.
This line is followed by 3 blank line.

This line is followed by 4 blank line.
This is the end.
[root@localhost ~]# 
P

多行打印

[root@localhost ~]# sed '/UNIX$/{N;/\nSystem/{s// Operating &/}}' i
Here are examples of the UNIX Operating 
System. Where UNIX
System appears,it should be the UNIX
Operating System.
[root@localhost ~]# 
[root@localhost ~]# sed '/UNIX$/{N;/\nSystem/{s// Operating &/;P;D}}' i
Here are examples of the UNIX Operating 
System. Where UNIX Operating 
System appears,it should be the UNIX
Operating System.
[root@localhost ~]# 


超级命令

保持空间

命令缩写功能
Holdh或H将模式空间的内容复制或追加到保持空间
Getg或G将保持空间的内容复制或追加到模式空间
Exchangex交换保持空间和模式空间的内容
Hold和Get
[root@localhost ~]# cat zyx
1
2
11
22
111
222
[root@localhost ~]# 
[root@localhost ~]# sed '/1/{h;d};/2/{G}' zyx
2
1
22
11
222
111
[root@localhost ~]# 
[root@localhost ~]# sed '/1/{h;d};/2/{g}' zyx
1
11
111
[root@localhost ~]# 
x
[root@localhost ~]# sed -n '/2/{h;x;p}' zyx
2
22
222
[root@localhost ~]# 

awk的高级用法

示例

//测试文件
[root@A ~]# cat test 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
~
~
//打印文件1,3列
[root@A ~]# awk -F: '{print $1":"$3}' test 
root:0
bin:1
daemon:2
adm:3

模式匹配

当 awk 读入一行是时,它试图匹配脚本中的每个模式匹配规则。只有与一个特定的模式相匹配的输入行才能成为操作对象。如果没有指定操作,于模式相匹配的输入行将被打印出来(执行打印语句是一个默认操作)。

//测试文件
[root@A ~]# cat test 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
~
//匹配空行并打印一句话
[root@A ~]# awk '/^$/{print "这是一个空行"}' test 
这是一个空行    #一个空行打印一次,多个空行打印多次

记录和字段

awk 假设它的输入是有结构的,而不只是一串无规则的字符。在最简单的情况下,它将每个输入行作为一条记录,而将由空格或制表符分隔的单词作为字段(用来分隔字段的字符被称为分隔符)。

//示例文本
[root@A ~]# cat test 
John Robinson 666-555-1111

[root@A ~]# awk '{print $2 $1 $3}' test
RobinsonJohn666-555-1111  #交换位置

[root@A ~]# awk '{print $2,$1,$3}' test
Robinson John 666-555-1111

//运算
[root@A ~]# echo | awk 'BEGIN{one=1;two=2}{print (one+two)}'	 #加
3

[root@A ~]# echo | awk 'BEGIN{one=1;two=2}{print (one-two)}'		  #减
-1
[root@A ~]# echo | awk '{print (10*4)}'
40				#乘

[root@A ~]# echo | awk '{print (10/4)}'
2.5				#除

//取出数字
示例文本
[root@A ~]# cat test 
707-724-0000
(707) 724-0000
(707) 724-0000
1-707-724-0000
1 707-724-0000
1(707)724-0000

[root@A ~]# grep -E '1?(-| )?\(?[0-9]{3}\)?[ |-]{1}[0-9]{3}-[0-9]{4}' test
707-724-0000
(707) 724-0000
(707) 724-0000
1-707-724-0000
1 707-724-0000

//取出第6列的第一行
[root@A ~]# cat test 
sdasd qesdf wgsdg qwasd eethf qre707-724-0000
(707) 724-0000
(707) 724-0000
1-707-724-0000
1 707-724-0000
1(707)724-0000

[root@A ~]# awk '$6 ~ /1?(-| )?\(?[0-9]{3}\)?[ -]{1}[0-9]{3}-[0-9]{4}/' test
sdasd qesdf wgsdg qwasd eethf qre707-724-0000

//统计空行数
[root@A ~]# cat test 
root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

[root@A ~]# awk '/^$/ {print x += 1}' test 
1
2
3
# 遇到空行就+1

//计算学生平均成绩
[root@A ~]# cat test 
john 85 92 78 94 88
andrea 89 90 75 90 86
jasper 84 88 80 92 84

[root@A ~]# awk '{sum = ($2+$3+$4+$5+$6);avg=(sum/5);print "姓名:" $1,avg}' test 
姓名:john 87.4
姓名:andrea 86
姓名:jasper 85.6

//取出第二行学生成绩
[root@A ~]# awk NR==2'{sum = ($2+$3+$4+$5+$6);avg=(sum/5);print "姓名:" $1,avg}' test 姓名:andrea 86
# NR 选定行号

[root@A ~]# awk '$1 ~ "andrea" {sum = ($2+$3+$4+$5+$6);avg=(sum/5);print "姓名:" $1,avg}' test 
姓名:andrea 86
# ‘~’ 匹配

[root@A ~]# awk '$1=="andrea" {sum = ($2+$3+$4+$5+$6);avg=(sum/5);print "姓名:" $1,avg}' test 
姓名:andrea 86
# ‘==’选定

系统变量

awk中有许多系统变量或内置变量。awk有两种类型的系统变量。第一种类型定义的变量默认值可以改变,例如默认的字段和记录分隔符。第二种类型定义的变量的值可用于报告或数据处理中。例如当前记录中字段的数量,当前记录的数量等。

有一组默认值会影响对记录和字段的输入和输出的识别。系统变量FS定义字段分隔符。它的默认值为一个空格,这将提示awk可以用若千个空格和/或制表符来分隔字段。FS可以被设置为任何单独的字符或一个正则表达式,前面,我们将分隔符改变为逗号,为的是读取一个名字和地址的列表。

//示例文本
取出第一行名字,和最后一行电话
[root@A ~]# cat test 
John Robinson
Koren lnc.
978 Commonwealth Ave.
Boston
MA 01760
696-0987

[root@A ~]# awk 'BEGIN{FS="\n";RS=""}{print $1,$NF}' test
John Robinson 696-0987


支票薄的结算

//示例文本
[root@A ~]# cat test 
1000
125 Market -125.45
126 Hardware Store -34.95
127 Video Store -7.45
128 Book Store -14.32
129 Gasoline -16.10

[root@A ~]# awk 'NR==1{print "Begin balace: \t" $1;balace=$1;next};{print $0;print balace += $3}' test 
Begin balace: 	1000
125 Market -125.45
874.55
126 Hardware Store -34.95
874.55
127 Video Store -7.45
874.55
128 Book Store -14.32
874.55
129 Gasoline -16.10
858.45

格式化打印

printf的格式说明符

//示例文本
[root@A ~]# cat test 
tom jerry 20
zhangsan lisi 15

[root@A ~]# awk '{printf "hello %s,my name is %s,I am %d years old.\n",$1,$2,$3}' test
hello tom,my name is jerry,I am 20 years old.
hello zhangsan,my name is lisi,I am 15 years old.

//数字左对齐,右对齐
示例文本
[root@A ~]# cat test 
1000
125 Market -125.45
126 Hardware Store -34.95
127 Video Store -7.45
128 Book Store -14.32
129 Gasoline -16.10

[root@A ~]# awk 'NR!=1{printf ("消费:%15.3f\n"),$NF}' test
消费:       -125.450
消费:        -34.950
消费:         -7.450
消费:        -14.320
消费:        -16.100
# 右对齐

[root@A ~]# awk 'NR!=1{printf ("消费:%-15.3f\n"),$NF}' test
消费:-125.450       
消费:-34.950        
消费:-7.450         
消费:-14.320        
消费:-16.100     
# 左对齐
{%.()f}  #括号里数字表示小数点保留多少位
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值