十五、gawk命令使用
gawk命令
gawk程序是Unix中原始awk程序的GNU版本。
它可以用來寫腳本的方式處理文本數(shù)據(jù)。
他可以
定義變量保存數(shù)據(jù)
使用算數(shù)運(yùn)算符字符串操作符處理數(shù)據(jù)
使用結(jié)構(gòu)化邏輯語句處理數(shù)據(jù)
提取數(shù)據(jù)重新定義格式(如過濾日志文件找出錯(cuò)誤行便于集中處理閱讀)
語法格式
gawk options program file
gawk選項(xiàng)
| 選項(xiàng) | 描述 |
| -F fs | 指定分隔符 |
| -f file | 指定含有命令的文件 |
| -v var=value | 定義變量 |
| -mf N | 指定處理文件中最大字段數(shù) |
| -mr N | 指定數(shù)據(jù)文件中最大數(shù)據(jù)行數(shù) |
| -W keyword | 指定gawk的兼容模式或警告等級(jí) |
從命令行讀取程序腳本
gawk命令的腳本使用花括號(hào)定義。
gawk命令認(rèn)為腳本是一串字符串所以還需要用單引號(hào)括起來。
gawk程序會(huì)針對(duì)數(shù)據(jù)流中的每行文本執(zhí)行程序腳本。
注意:如下命令,因?yàn)闆]有在命令行上指定文件名,gawk默認(rèn)會(huì)從STDIN也就是鍵盤接收數(shù)據(jù)。
即從鍵盤中中隨便輸入文本,接著都會(huì)執(zhí)行輸出Hello World!然后再等待輸入。
[root@tzPC 19Unit]# gawk '{print "Hello World!"}'
This is a test
Hello World!
Ctrl+D可以終止gawk程序
使用數(shù)據(jù)字段變量
gawk會(huì)自動(dòng)給一行中每個(gè)數(shù)據(jù)字段分配一個(gè)變量
$0代表整個(gè)文本行
$1代表文本行中的第1個(gè)數(shù)據(jù)字段
$2代表文本行中第2個(gè)數(shù)據(jù)字段
...
在文本行中每個(gè)數(shù)據(jù)字段都是通過字段分隔符劃分的。默認(rèn)是空格或制表符
例:使用gawk讀取文本文件,顯示第一個(gè)數(shù)據(jù)字段的值
[root@tzPC 19Unit]# cat data2.txt
One line of test text.
Two lines of test text.
Three lines of test text.
[root@tzPC 19Unit]# gawk '{print $1}' data2.txt
One
Two
Three
-F參數(shù)更改字段分隔符
[root@tzPC 19Unit]# gawk -F: '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
...
在程序腳本中使用多個(gè)命令
使用分號(hào)分隔命令即可。
[root@tzPC 19Unit]# echo "My name is tz" | gawk '{$4="root";print $0}'
My name is root
也可以使用如下寫法
因?yàn)闆]有輸入數(shù)據(jù)流,所以gawk默認(rèn)會(huì)從STDIN中獲取數(shù)據(jù)也就是從鍵盤中獲取,所以要手動(dòng)輸入數(shù)據(jù),然后才會(huì)執(zhí)行g(shù)awk命令輸出數(shù)據(jù),使用Ctrl+D退出程序
[root@tzPC ~]# gawk '{
> $4="root"
> print $0}'
My name is tz
My name is root
從文件中讀取程序命令
使用-f選項(xiàng)指定命令文件
[root@tzPC 19Unit]# cat script2.gawk
{print $1 "'s home directory is " $6}
[root@tzPC 19Unit]# gawk -F: -f script2.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
...
使用含有多行命令的命令文件
注意:這里定義了變量text,再次調(diào)用它的時(shí)候不需要加$
[root@tzPC 19Unit]# cat script3.gawk
{
text="'s home directory is "
print $1 text $6
}
[root@tzPC 19Unit]# gawk -F: -f script3.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
sync's home directory is /sbin
在處理數(shù)據(jù)前運(yùn)行腳本
使用關(guān)鍵字BEGIN
[root@tzPC 19Unit]# cat data3.txt
Line 1
Line 2
Line 3
[root@tzPC 19Unit]# gawk 'BEGIN {print "The data3 File Contents:"}
> {print $0}' data3.txt
The data3 File Contents:
Line 1
Line 2
Line 3
再處理數(shù)據(jù)后運(yùn)行腳本
使用END關(guān)鍵字
[root@tzPC 19Unit]# gawk 'BEGIN {Print "The data3 File Contents:"}
> {print $0}
> END {print "End of File"}' data3.txt
Line 1
Line 2
Line 3
End of File
高大上的格式化輸出報(bào)告用法來了,圈重點(diǎn)!
[root@tzPC 19Unit]# cat script4.gawk
BEGIN{
print "The latest list of users and shells"
print " UserID Shell"
print "------- -------"
FS=":"
}
{
print $1 " " $7
}
END{
print "This concludes the listing"
}
[root@tzPC 19Unit]# gawk -f script4.gawk /etc/passwd
The latest list of users and shells
UserID Shell
------- -------
root /bin/bash
bin /sbin/nologin
postfix /sbin/nologin
sshd /sbin/nologin
tz /bin/bash
This concludes the listing
學(xué)習(xí)來自:《Linux命令行與Shell腳本大全 第3版》第19章
今天的學(xué)習(xí)是為了以后的工作更加的輕松!
總結(jié)
以上是生活随笔為你收集整理的十五、gawk命令使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: KDE常用桌面插件总结
- 下一篇: 设计模式-行为模式(读书笔记)