數據流處理命令awk

  • awk '{print $2}' [file]: 輸出文件[file]中每行以空格分割的第二項, $2表示第二項
➜  linux_commands cat hello_sorted.txt
HELLO everyone
hello boys
hi boys
hi world
she is saying hi
➜  linux_commands awk '{print $2}' hello_sorted.txt
everyone
boys
boys
world
is
  • awk '/str/ {print $1}' [file]:輸出文件[file]中包含字符串str的行以空格分割的第一項
➜  linux_commands awk '/boys/ {print $1}' hello_sorted.txt (包含boys行的第1項)
hello
hi
  • awk 'NR%3==2' [file]: 輸出文件[file]中的每三行的第2行,NR表示行數
➜  linux_commands awk 'NR%3==2' hello_sorted.txt
hello boys
she is saying hi
  • awk -F ',' '{print $NF}' [file]:輸出文件[file]中以逗號爲分割符的最後一列,-F表示分隔符,$NF表示列數  
​
➜  linux_commands cat test.txt
yes,no
white,black
boy,girl
➜  linux_commands awk -F ',' '{print $NF}' test.txt
no
black
girl

​
  •  awk '{for (i=6; i <= NF; i++) printf $i""FS; print""}' : 輸出所有行中從第6行以後的內容
➜  linux_commands ls -l
total 392
-rw-r--r--  1 qiushye  staff  172214 May 15 12:11 commodity.txt
-rw-r--r--  1 qiushye  staff     161 Apr  4 22:43 diff.txt
-rw-r--r--  1 qiushye  staff      92 May 19 12:30 hello.txt
-rw-r--r--  1 qiushye  staff      60 May 15 12:16 hello_sorted.txt
-rw-r--r--  1 qiushye  staff      68 May 15 12:20 hh.txt
-rw-r--r--  1 qiushye  staff       9 May 22 12:36 input.txt
-rw-r--r--  1 qiushye  staff      23 Apr 30 12:21 regex.txt
drwxr-xr-x  8 qiushye  staff     256 May 22 12:37 temp
dr-xr-xrwx  5 eric     staff     160 May 22 12:24 tt
drwxr-xr-x  2 qiushye  staff      64 May 22 12:24 ut
➜  linux_commands ls -l | awk '{for (i=6; i <= NF; i++) printf $i""FS; print""}' (只保留日期和文件名信息)

May 15 12:11 commodity.txt
Apr 4 22:43 diff.txt
May 19 12:30 hello.txt
May 15 12:16 hello_sorted.txt
May 15 12:20 hh.txt
May 22 12:36 input.txt
Apr 30 12:21 regex.txt
May 22 12:37 temp
May 22 12:24 tt
May 22 12:24 ut
  • awk '{s+=$2} END {print s}' [file]:對文件[file]的第二項求和並打印
➜  linux_commands cat salary.txt
Bob 1000
Mary 2000
Tom 4000
➜  linux_commands awk '{s+=$2} END {print s}' salary.txt
7000
  • awk 'NR>3 {print}'  [file] |  awk '{print $NF}'  : 打印出文件[file]第3行以下的最後一列
➜  linux_commands awk '{print $NF}' hello.txt| awk 'NR>3'
boys
everyone
world
boys
boys
everyone
➜  linux_commands ls -lt | awk 'NR>1 {print}' | awk '{print $NF}' | head -3 (找出最新的前三個文件,NR>1是爲了去除total這行,print $NF輸出文件名)
salary.txt
test.txt
temp
  • awk  '{print toupper()}' [file]: 將文件[file]的每行大寫後打印,toupper($1)表示將第一項大寫後打印
➜  linux_commands awk '{print toupper() }' hello.txt
HI WORLD
HI BOYS
HI WORLD
HELLO BOYS
HELLO EVERYONE
HI WORLD
HI BOYS
OH BOYS
HELLO EVERYONE
  • awk '{if ($1 > "h") print $1; else print "----"}' [file]:將所有行首字大於h的行正常輸出,否則輸出----
➜  linux_commands awk '{if ($1 > "h") print $1; else print "----"}' hello.txt
hi
hi
hi
hello
----
hi
hi
oh
----

最後列舉下awk的內建變量:

  1. NR:當前的行號,從1開始計數
  2. NF:當前行的字段個數,也就是有多少列
  3. $0:整行內容
  4. FS:字段分隔符,默認是空格和製表符。
  5. RS:行分隔符,用於分割每一行,默認是換行符。
  6. OFS:輸出字段的分隔符,用於打印時分隔字段,默認爲空格。
  7. ORS:輸出記錄的分隔符,用於打印時分隔記錄,默認爲換行符。
  8. OFMT:數字輸出的格式,默認爲%.6g。

以及awk的函數:

  1. tolower():字符轉爲小寫。
  2. length():返回字符串長度。
  3. substr():返回子字符串。
  4. sin():正弦。
  5. cos():餘弦。
  6. sqrt():平方根。
  7. rand():隨機數。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章