shell文本处理——gawk和sed简介

gawk程序

gawk程序时Unix中的原始awk程序的GNU版本。在gawk编程语言中,可以做如下的事情:

  • 定义变量来保存数据
  • 使用算术和字符串操作符来处理数据
  • 使用结构化编程概念(比如if-then语句和循环)来为数据处理增加处理逻辑。
  • 通过提取数据文件中的数据元素,将其重新排列或格式化,生成格式化报告
    1.gawk命令格式
    gawk程序的基本格式如下:
    gawk options program file
    gawk有如下选项:
    |选项|描述|
    |–|--|
    |-F fs|指定行中划分数据字段的字段分隔符|
    |-f file | 从指定的文件中读取程序|
    |-v var=value|定义gawk程序中的一个变量及其默认值|
    |-mf N|指定要处理的数据文件中的最大字段数|
    |-mr N|指定数据文件中的最大数据行数|
    |-W keyword|指定gawk的兼容模式或警告等级|

2.使用数据字段变量
默认情况下,gawk会将如下变量分配给它在文本行中发现的数据字段:

  • $0代表整个文本行;
  • $1代表文本行中的第一个数据字段
  • $2代表文本行中的第二个数据字段
  • $n代表文本行中的第n个数据字段
    下面例子表示从文本文件中读取,并显示第1个数据字段的值
[root@ommleft zd]# cat data2.txt
One line of test text.
Two line of test text.
Three line of test text.
[root@ommleft zd]# gawk '{print $1}' data2.txt
One
Two
Three

使用字段分隔符来读取文件,可以使用-F选项指定

[root@ommleft zd]# gawk -F: '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown

3.从程序脚本中使用多个命令
要在命令行上的程序脚本中使用多条命令,可以在命令之间放个分号即可。

[root@ommleft zd]# echo "My name is Rich"|gawk '{$4="Christine";print $0}'
My name is Christine

4.从文件中读取程序
gawk编辑器允许将程序存储到文件中,然后在命令行中引用。

[root@ommleft zd]# more script.gawk
{print $1 "'s home directory is " $6}
[root@ommleft zd]# gawk -F: -f script.gawk /etc/passwd
root's home directory is /root
bin's home directory is /bin
daemon's home directory is /sbin
adm's home directory is /var/adm
lp's home directory is /var/spool/lpd

5.gawk BEGIN
gawk中的BEGIN关键字会强制gawk在读取数据前执行BEGIN关键字后指定的程序脚本。

[root@ommleft zd]# gawk 'BEGIN{print "Hello World!"}'
Hello World!

读取文本并显示

[root@ommleft zd]# cat data3.txt
Line 1
Line 2
Line 3
root@ommleft zd]# gawk 'BEGIN {print "The data3 file contents:"}{print $0}' data3.txt
The data3 file contents:
Line 1
Line 2
Line 3

6.gawk END
与BEGIN关键字相似,END关键字允许指定一个程序脚本,gawk会在读完数据之后执行。

[root@ommleft zd]# gawk 'BEGIN{print "The data3 file contents:"}
{print $0}
END{print "End of File"}' data3.txt
The data3 file contents:
Line 1
Line 2
Line 3
End of File

sed编辑器

sed编辑器被称作流编辑器(stream editor),sed编辑器会执行如下操作:
1)一次从输入中读取一行数据。
2)根据所提供的编辑器命令匹配数据
3)按照命令修改流中的数据
4)将新的数据输出到STDOUT
sed命令的格式如下:
sed options script file
sed命令选项如下:
|选项|描述|
|-e command|将指定的命令添加到已有命令中|
|-f file|将file中指定的命令添加到已有命令中|
|-n |不产生命令输出,使用print命令完成输出|

1.在命令行定义编辑器命令

[root@ommleft zd]# echo "This is a test"|sed 's/test/trial/' 
This is a trial

还可以对文件数据进行处理,输出STDOUT,但并不会修改文件中的数据

[root@ommleft zd]# 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.
The quick brown fox jumps over the lazy dog.
[root@ommleft zd]# sed 's/dog/cat/' data1.txt
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.

2.在命令行使用多个编辑器命令
要在sed命令行上执行多个命令时,只要用-e选项就可以了。

[root@ommleft zd]# sed -e 's/brown/green/;s/dog/cat/' data1.txt
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.

3.从文件中读取编辑器命令

[root@ommleft zd]# cat script.sed
s/brown/green/
s/fox/elephant/
s/dog/cat/
[root@ommleft zd]# sed -f script.sed data1.txt
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
发布了75 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zhengdong12345/article/details/101266083