awk用法:
awk 'pattern '
argc 命令列變元個數示例:argv 命令列變元陣列
filename 當前輸入檔名
fnr 當前檔案中的記錄號
fs 輸入域分隔符,預設為乙個空格
rs 輸入記錄分隔符
nf 當前記錄裡域個數
nr 到目前為止記錄數
ofs 輸出域分隔符
ors 輸出記錄分隔符
test.txt
test zqh
ttttttt
good
i adm student!
3242test 444
zhauiqhui,zqh
bboy zqh or
die!
$ awk '' test.txt # 每行按空格或tab分割,輸出文字中的1、4項
$ awk -f, '' test.txt # 使用","分割,輸出文字中的1、2項
$ awk 'begin ' test.txt #使用內建變數
$ awk -f
'[ ,]'
'' test.txt #多個分隔符,先使用空格分割,然後對分割結果再使用","分割
$ awk -f
',''begin end ' test.txt # 所有行開始輸出新增'start--',在最後一行新增'end--'
輸出:start--
test zqh
ttttttt
good
i adm student!
3242test 444
zhauiqhui
bboy zqh or die!
end--
$ awk -f, '/zqh/' test.txt # 查詢關鍵字為'zqh'的內容
輸出:test zqh
zhauiqhui,zqh
bboy zqh or die!
$ awk '/zqh/' test.txt # 輸出包含關鍵字為'zqh'的行第乙個值(以空格或tab分割)
輸出:test
zhauiqhui,zqh
bboy
$ awk '' test.txt # 統計test.txt中檔名,每行的行號,每行的列數,對應的完整行內容
輸出:filename:test.txt,linenumber:
1,columns:
2,linecontent:test zqh
filename:test.txt,linenumber:
2,columns:
1,linecontent:ttttttt
filename:test.txt,linenumber:
3,columns:
1,linecontent:good
filename:test.txt,linenumber:
4,columns:
3,linecontent:
i adm student!
filename:test.txt,linenumber:
5,columns:
2,linecontent:
3242test 444
filename:test.txt,linenumber:
6,columns:
1,linecontent:zhauiqhui,zqh
filename:test.txt,linenumber:
7,columns:
4,linecontent:bboy zqh or die!
注:上面命令可用:
$ awk -f
'' test.txt 來代替
$ awk '$1 ~ /zqh/ ' test.txt #找出第一列包含 "zqh",並列印該行的第一列與第三列,其中~是模式的開始
輸出:zhauiqhui,zqh
$ awk 'begin /zqh/' test.txt # 忽略大小寫
$ awk '$2 !~ /zqh/ ' log.txt #找出不包含"zqh",並列印該行的第二列與第四列
awk程式設計
$ awk '$1>2 && $2=="are" ' test.txt # 輸出第一列大於2並且第二列等於'are'的行
$ awk ' end' test.txt
輸出:test zqh
ttttttt
good
i adm student!
3242test 444
zhauiqhui,zqh
bboy zqh or die!
countis7
$ awk 'begin ; end' test.txt
輸出:0 test
1 ttttttt
2 good
3 i4
3242test
5 zhauiqhui,zqh
6 bboy
對比
linux awk列資料處理工具使用示例
1表示第一列 0表示所有列 11表示第十一列 第一種寫法 awk f etc passwd more bin bash usr sbin nologin usr sbin nologin usr sbin nologin bin sync usr sbin nologin usr sbin nolo...
Linux awk基本使用
linux三劍客之awk,以字段為單位進行處理資料的處理,統計。作為開發人員也應熟練掌握 awk 引數 模式 檔案 1 f 以指定分隔符號分隔,也就是多個字段 2 print列印輸出 3 n第n列 4 0表示一整行 5 nf列的數量 6 nf最後1列名稱 nf當前列總數 列印第一行列總數 awk n...
Linux awk 使用用例
環境 centos 鑑於語句描述蒼白無力,用例子直接說明。檔案內容 zilzhang 19881110 jiangxi 18 film zhagnsan 21321 sichuan 100 card 1.列印整行 awk mytxt zilzhang 19881110 jiangxi 18 film...