歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> AWK原理及命令和文件輸入

AWK原理及命令和文件輸入

日期:2017/2/28 14:31:11   编辑:Linux教程

一,awk簡介
1,
awk是3個姓氏的首字母,代表該語言的3個作者,awk的版本有很多,包括:舊版awk,新版awk(nawk),GNU awk(gawk)等。
awk程序有awk命令,括在引號或寫在文件中的指令以及輸入文件這幾個部分組成。
2,
[root@rhel helinbash]# which awk
/bin/awk
[root@rhel helinbash]# which gawk
/bin/gawk
[root@rhel helinbash]# ls -l /bin/awk /bin/gawk
lrwxrwxrwx 1 root root 4 Oct 10 2013 /bin/awk -> gawk
-rwxr-xr-x 1 root root 320416 Jan 15 2007 /bin/gawk
[root@rhel helinbash]#
注:以後的例子都是采用gawk命令

AWK簡介及使用實例 http://www.linuxidc.com/Linux/2013-12/93519.htm

AWK 簡介和例子 http://www.linuxidc.com/Linux/2012-12/75441.htm

Shell腳本之AWK文本編輯器語法 http://www.linuxidc.com/Linux/2013-11/92787.htm

正則表達式中AWK的學習和使用 http://www.linuxidc.com/Linux/2013-10/91892.htm

文本數據處理之AWK 圖解 http://www.linuxidc.com/Linux/2013-09/89589.htm

二,awk工作原理
1,
以下內容的names文件名舉例按步驟解析awk的處理過程
(1)
vim names
Tom Savage 100
Molly Lee 200
John Doe 300
(2)
[root@rhel helinbash]# cat names.txt | cut -d ' ' -f 2

Savage
Lee

[root@rhel helinbash]# cat names.txt | cut -d '\t' -f 2
cut: the delimiter must be a single character
Try `cut --help' for more information.
[root@rhel helinbash]#
(3)
[root@rhel helinbash]# gawk '{ print $1,$3 }' names.txt


Tom 100
Molly 200
John 300

[root@rhel helinbash]#

[root@rhel helinbash]# gawk '{ print $1 $3 }' names.txt

Tom100
Molly200
John300

[root@rhel helinbash]#

2,
原理圖
FS:Field separator(分隔符)
OFS:Output Field Separator

第三步:awk中print命令打印字段;{print $1,$3} 只取有用的第一段和第三段;在打印時$1和$3之間由空格間隔。“,”逗號是一個映射到內部的輸出字段分隔符(OFS),OFS變量
缺省為空格,逗號在輸出時被空格替換。接下來,awk處理下一行數據,直到所有的行處理完。

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2014-05/102520p2.htm

Copyright © Linux教程網 All Rights Reserved