天天看點

21、awk用法簡介

awk   -F ":" 'BEGIN{}; { };END{} ' files

awk [options] 'script' var=value file(s)

awk [options] -f scriptfile var=value file(s)

使用awk可以很友善處理結構化文本中每一行中的每一列。

section=$(sed '1,$p' ${everyname} | sed 's/,/ /g' | sed 's/: /:/g' | head -n 100 | awk '{if (($1 ~ ”hello”) && ($11 !~ ”worl”)) {section6=$6;print section6;}}')

1、簡介

awk : providing a programming language instead of just editor commands。在awk中,

    根據指定的分隔符(可以通過FS變量指定或者-F選項), 将檔案的内容讀出,并且根據條件可以指定要顯示的字段。

我們可以:

■ Define variables to store data.(定義變量,存儲資料)

■ Use arithmetic and string operators to operate on data.(通過算術或字元操作來操作資料)

■ Use structured programming concepts, such as if-then statements and loops, to add logic to your data processing.(有結構化程式設計概念,像C語言一樣進行資料處理)

■ Generate formatted reports by extracting data elements within the data file and repositioning them in another order or format.(導出資料到檔案,也可以重定向另一種模式)

awk選項有好多,常用的有:

F fs

Specify a file separator for delineating data fields in a line.

f file

Specify a filename to read the program from.

2、幾個變量

    ARGC   指令行變元個數

    ARGV   指令行變元數組

    FILENAME   目前輸入檔案名

    FNR   目前檔案中的記錄号   

    FS   輸入域分隔符,預設為一個空格

    NF   目前記錄裡域個數

NR   到目前為止記錄數(行數)

RS   記錄分隔符(預設是一個換行符)。

~   表示比對

!~   不比對

OFS   輸出域分隔符,預設空格

ORS   輸出記錄分隔符,預設回車

3、說明

1)模式 pattern可以是/ /包含的比對形式

2)BEGIN(處理檔案前的action,常包含FS、OFS等)、END(處理檔案後的action)

3)條件與循環:if else(next,exit),for do while(continue,break)

4)數學運算符 + - * / % ^;數學函數sin int;字元串函數length index gsub substr等

5)數組與關聯數組:a[1]; a[$1]; a[$0]; a[b[i]]

6)BEGIN、END塊可有可無,分别隻在開始和結束時執行一次。多個指令用分号相隔。

7)the gawk program defines data as records and data fields. A record is a line of data (delineated by the newline characters by default), and a data field is a separate data element within the line (delineated by a white space character, such as a space or tab, by default).

8)-v var=value Define a variable and default value used in the gawk program.

-mf N Specify the maximum number of fields to process in the data file.

-mr N Specify the maximum record size in the data file.

4、使用示例

1)awk '/101/' file 顯示檔案file中包含101的比對行。

2)cat header.h | tail  -n 5 | awk '{print NR,NF,$0}'

1 3 #define CONFIG_FILE "./config"

2 0

3 3 using namespace std;

4 0  

5 1 #endif

3)awk -F '[ :\t|]' '{print $1}' file

按照正規表達式的值做為分隔符,這裡代表空格、:、TAB、|同時做為分隔符。

4)awk '$1 ~ /101/ {print $1}' file 顯示檔案中第一個域比對101的行(記錄)。

5)awk 'BEGIN {system("echo \"Input your name:\\c\""); getline d;print "\nYour name is",d,"\b!\n"}'   //實作互動

awk '{ i=1;while(i<NF) {print NF,$i;i++}}' file //實作循環

6) 通過從檔案中讀入腳本執行

$ cat script2

{ print $5 "’s userid is " $1 }

$ gawk -F":" -f script2 /etc/passwd

7)在指令行中指派

$ cat script1

BEGIN{FS=","}

{print $n}

$ gawk -f script1 n=2 data1

$ gawk -f script1 n=3 data1

8)使用正則表示式

    the regular expression must appear before the left brace of the program script that it controls:

gawk ’BEGIN{FS=","} /test/{print $1}’ data1 //從檔案data1中一行一行的讀入資料,把資料域的分隔符設為",",查找比對test的行,并輸入第一項資料域。

9)比對操作符(The matching operator)

$1 ~ /^data/

This expression filters records where the first data field starts with the text data.

10)數學表達式

$ gawk -F: ’$4 == 0{print $1}’ /etc/passwd

    displays the first data field value for all lines that contain the value 0 in the fourth data field.

11)要吧有if,for,while等結構。

12)在腳本中進行指派

awk 'BEGIN{test="hello";print test}'

hello

13)模式

/正規表達式/  //   /test/

關系表達式 

模式比對表達式 ~

14)$ awk '/root/,/mysql/' test将顯示root第一次出現到mysql第一次出現之間的所有行。如果有一個模闆沒出現,則比對到開頭或末尾。

15)$ awk '/^(no|so)/' test  //列印所有以模式no或so開頭的行。

$ awk '/^[ns]/{print $1}' test—–如果記錄以n或s開頭,就列印這個記錄。

$ awk '$1 ~/[0-9][0-9]$/(print $1}' test—–如果第一個域以兩個數字結束就列印這個記錄。

$ awk '$1 == 100 || $2 < 50' test—–如果第一個或等于100或者第二個域小于50,則列印該行。

$ awk '$1 != 10' test—–如果第一個域不等于10就列印該行。

$ awk '/test/{print $1 + 10}' test—–如果記錄包含正規表達式test,則第一個域加10并列印出來。

參考

【4】 講的相當詳細

<a href="http://blog.microsuncn.com/?p=1232">http://blog.microsuncn.com/?p=1232</a>