初识sed和gawk

sed编辑器被称作流编辑器(stream editor),和普通的交互式文本编辑器恰好相反。在交互式
文本编辑器中(比如vim),你可以用键盘命令来交互式地插入、删除或替换数据中的文本。流编
辑器则会在编辑器处理数据之前基于预先提供的一组规则来编辑数据流。
sed命令选项
-e script 在处理输入时,将script中指定的命令添加到已有的命令中
-f file 在处理输入时,将file中指定的命令添加到已有的命令中
-n 不产生命令输出,使用print命令来完成输出
#将test替换成hhhh
echo "This is a test" | sed 's/test/hhhh/' 
#多个条件替换
[root@localhost ~]# cat data1.txt 
The quick brown fox jumps over the lazy dog. 
The quick brown fox jumps over the lazy dog. 
The quick brown fox jumps over the lazy dog. 
The quick brown fox jumps over the lazy dog.

#同时将brown和dog都替换,多个条件之间用;风格
sed -e 's/brown/green/; s/dog/cat/' data1.txt

#第二个多条件的替换的方法,从文件中读取编辑器的内容
[root@localhost ~]# cat script1.sed 
s/The/this/
s/dog/cat/
s/fox/elephant/

[root@localhost ~]# sed -f script1.sed data1.txt 
this quick brown elephant jumps over the lazy cat. 
this quick brown elephant jumps over the lazy cat. 
this quick brown elephant jumps over the lazy cat. 
this quick brown elephant jumps over the lazy cat.
 
gawk程序是Unix中的原始awk程序的GNU版本。gawk程序让流编辑迈上了一个新的台阶,它
提供了一种编程语言而不只是编辑器命令。在gawk编程语言中,你可以做下面的事情:
 定义变量来保存数据;
 使用算术和字符串操作符来处理数据;
 使用结构化编程概念(比如if-then语句和循环)来为数据处理增加处理逻辑;
 通过提取数据文件中的数据元素,将其重新排列或格式化,生成格式化报告。
1. gawk命令格式
gawk程序的基本格式如下:
gawk options program file
gawk选项
-f file 从指定的文件中读取程序
-v var=value 定义gawk程序中的一个变量及其默认值
-mf N 指定要处理的数据文件中的最大字段数
-mr N 指定数据文件中的最大数据行数
-W keyword 指定gawk的兼容模式或警告等级
gawk
$0代表整个文本行
$1代表文本行的第1个数据段
$2代表文本行的第2个数据段
$n代表文本行的第n个数据段
 1 [root@localhost ~]# cat data2.txt 
 2 one line of test text
 3 two lines of test text
 4 three lines of test text
 5 
 6 #只显示第一个数据字段的值
 7 [root@localhost ~]# gawk '{print $1}' data2.txt 
 8 one
 9 two
10 three
11 
12 -F以字段分隔符划分
13 #以:分隔,读取第一个数据段
14 [root@localhost ~]# gawk -F: '{print $1}' /etc/passwd
15 root
16 bin
17 daemon
18 adm
19 lp
20 sync
21 shutdown
22 halt
23 mail
24 #多条件执行,例:将rich修改为jack,并将文本行打印出来
25 [root@localhost ~]# echo "My name is rich" | gawk '{$4="jack"; print $0}'
26 My name is jack
27 
28 #-f从文件中读取程序
29 
30 [root@localhost ~]# cat script2.gawk 
31 {print $1 " homedirctory is " $6}
32 
33 [root@localhost ~]# gawk -F: -f script2.gawk /etc/passwd
34 root homedirctory is /root
35 bin homedirctory is /bin
36 daemon homedirctory is /sbin
37 adm homedirctory is /var/adm
38 lp homedirctory is /var/spool/lpd
39 sync homedirctory is /sbin
40 shutdown homedirctory is /sbin
41 halt homedirctory is /sbin
42 mail homedirctory is /var/spool/mail
43 operator homedirctory is /root
44 
45 如果在这个程序文件中需要指导多条命令,只需要一条命令放一行即可,不需要用分号
46 [root@localhost ~]# cat script3.gawk
47 {
48 text="home directory is "
49 print $1 text $6
50 }
51 
52 在处理前运行脚本BEGIN
53 [root@localhost ~]# cat data3.txt 
54 #!/bin/bash
55 Line 1
56 line 2
57 line 3
58 
59 root@localhost ~]# gawk 'BEGIN {print "the data file contents:"}
60 > {print $0}' data3.txt
61 the data file contents:
62 #!/bin/bash
63 Line 1
64 line 2
65 line 3
66 
67 
68 在处理数据后运行脚本END
69 [root@localhost ~]# gawk 'BEGIN {print "the data file contents:"}
70 > {print $0}
71 > END {print "End fo file"}' data3.txt
72 the data file contents:
73 #!/bin/bash
74 Line 1
75 line 2
76 line 3
77 End fo file

 

posted @ 2020-01-02 17:08  LaniLai  阅读(230)  评论(0编辑  收藏  举报