国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP開發(fā) Linux awk command

Linux awk command

Dec 12, 2016 pm 03:55 PM

AWK是一種處理文本文件的語言,是一個強大的文本分析工具。

之所以叫AWK是因為其取了三位創(chuàng)始人 Alfred Aho,Peter Weinberger, 和 Brian Kernighan 的Family Name的首字符。

語法

awk [選項參數(shù)] 'script' var=value file(s)或awk [選項參數(shù)] -f scriptfile var=value file(s)

選項參數(shù)說明:

-F fs or --field-separator fs


指定輸入文件折分隔符,fs是一個字符串或者是一個正則表達式,如-F:。

-v var=value or --asign var=value


賦值一個用戶定義變量。

-f scripfile or --file scriptfile


從腳本文件中讀取awk命令。

-mf nnn and -mr nnn


對nnn值設置內(nèi)在限制,-mf選項限制分配給nnn的最大塊數(shù)目;-mr選項限制記錄的最大數(shù)目。這兩個功能是Bell實驗室版awk的擴展功能,在標準awk中不適用。

-W compact or --compat, -W traditional or --traditional


在兼容模式下運行awk。所以gawk的行為和標準的awk完全一樣,所有的awk擴展都被忽略。

-W copyleft or --copyleft, -W copyright or --copyright


打印簡短的版權信息。

-W help or --help, -W usage or --usage


打印全部awk選項和每個選項的簡短說明。

-W lint or --lint


打印不能向傳統(tǒng)unix平臺移植的結構的警告。

-W lint-old or --lint-old


打印關于不能向傳統(tǒng)unix平臺移植的結構的警告。

-W posix


打開兼容模式。但有以下限制,不識別:/x、函數(shù)關鍵字、func、換碼序列以及當fs是一個空格時,將新行作為一個域分隔符;操作符**和**=不能代替^和^=;fflush無效。

-W re-interval or --re-inerval


允許間隔正則表達式的使用,參考(grep中的Posix字符類),如括號表達式[[:alpha:]]。

-W source program-text or --source program-text


使用program-text作為源代碼,可與-f命令混用。

-W version or --version


打印bug報告信息的版本。

基本用法

log.txt文本內(nèi)容如下:

2 this is a test3 Are you like awkThis's a test
10 There are orange,apple,mongo

用法一:

awk '{[pattern] action}' {filenames} # 行匹配語句 awk '' 只能用單引號

實例:

# 每行按空格或TAB分割,輸出文本中的1、4項
 $ awk '{print $1,$4}' log.txt ---------------------------------------------
 2 a 3 like This's
 10 orange,apple,mongo
 # 格式化輸出
 $ awk '{printf "%-8s %-10s\n",$1,$4}' log.txt
 ---------------------------------------------
 2        a
 3        like
 This's 10       orange,apple,mongo

用法二:

awk -F #-F相當于內(nèi)置變量FS, 指定分割字符

實例:

# 使用","分割
 $  awk -F, '{print $1,$2}'   log.txt ---------------------------------------------
 2 this is a test 3 Are you like awk This's a test
 10 There are orange apple
 # 或者使用內(nèi)建變量
 $ awk 'BEGIN{FS=","} {print $1,$2}'     log.txt
 ---------------------------------------------
 2 this is a test
 3 Are you like awk
 This's a test 10 There are orange apple # 使用多個分隔符.先使用空格分割,然后對分割結果再使用","分割
 $ awk -F '[ ,]'  '{print $1,$2,$5}'   log.txt ---------------------------------------------
 2 this test 3 Are awk This's a
 10 There apple

用法三:

awk -v # 設置變量

實例:

$ awk -va=1 '{print $1,$1+a}' log.txt ---------------------------------------------
 2 3
 3 4
 This's 1
 10 11
 $ awk -va=1 -vb=s '{print $1,$1+a,$1b}' log.txt
 ---------------------------------------------
 2 3 2s
 3 4 3s
 This's 1 This'ss
 10 11 10s

用法四:

awk -f {awk腳本} {文件名}

實例:

$ awk -f cal.awk log.txt

Linux awk command


Linux awk command

過濾第一列大于2的行

$ awk '$1>2' log.txt   
#命令#輸出
3 Are you like awkThis's a test
10 There are orange,apple,mongo


過濾第一列等于2的行

$ awk '$1==2 {print $1,$3}' log.txt    #命令
#輸出
2 is

過濾第一列大于2并且第二列等于'Are'的行

$ awk '$1>2 && $2=="Are" {print $1,$2,$3}' log.txt    #命令
#輸出
3 Are you

內(nèi)建變量

$ awk 'BEGIN{printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n","FILENAME","ARGC","FNR","FS","NF","NR","OFS","ORS","RS";printf "---------------------------------------------\n"} {printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n",FILENAME,ARGC,FNR,FS,NF,NR,OFS,ORS,RS}'  log.txt
FILENAME ARGC  FNR   FS   NF   NR  OFS  ORS   RS
---------------------------------------------
log.txt    2    1         5    1
log.txt    2    2         5    2
log.txt    2    3         3    3
log.txt    2    4         4    4
$ awk -F\' 'BEGIN{printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n","FILENAME","ARGC","FNR","FS","NF","NR","OFS","ORS","RS";printf "---------------------------------------------\n"} {printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n",FILENAME,ARGC,FNR,FS,NF,NR,OFS,ORS,RS}'  log.txt
FILENAME ARGC  FNR   FS   NF   NR  OFS  ORS   RS
---------------------------------------------
log.txt    2    1    '    1    1
log.txt    2    2    '    1    2
log.txt    2    3    '    2    3
log.txt    2    4    '    1    4
# 輸出順序號 NR, 匹配文本行號
$ awk '{print NR,FNR,$1,$2,$3}' log.txt
---------------------------------------------
1 1 2 this is
2 2 3 Are you
3 3 This's a test
4 4 10 There are
# 指定輸出分割符
$  awk '{print $1,$2,$5}' OFS=" $ "  log.txt
---------------------------------------------
2 $ this $ test
3 $ Are $ awk
This's $ a $
10 $ There $

使用正則,字符串匹配

# 輸出第二列包含 "th",并打印第二列與第四列
$ awk '$2 ~ /th/ {print $2,$4}' log.txt
---------------------------------------------
this a

~ 表示模式開始。// 中是模式。

$ awk 'BEGIN{IGNORECASE=1} /this/' log.txt
---------------------------------------------
2 this is a test
This's a test

模式取反

$ awk '$2 !~ /th/ {print $2,$4}' log.txt
---------------------------------------------
Are like
a
There orange,apple,mongo
$ awk '!/th/ {print $2,$4}' log.txt
---------------------------------------------
Are like
a
There orange,apple,mongo

awk腳本

關于awk腳本,我們需要注意兩個關鍵詞BEGIN和END。

BEGIN{ 這里面放的是執(zhí)行前的語句 }

END {這里面放的是處理完所有的行后要執(zhí)行的語句 }

{這里面放的是處理每一行時要執(zhí)行的語句}

假設有這么一個文件(學生成績表):

$ cat score.txt
Marry   2143 78 84 77
Jack    2321 66 78 45
Tom     2122 48 77 71
Mike    2537 87 97 95
Bob     2415 40 57 62

我們的awk腳本如下:

$ cat cal.awk
#!/bin/awk -f
#運行前
BEGIN {
    math = 0
    english = 0
    computer = 0
 
    printf "NAME    NO.   MATH  ENGLISH  COMPUTER   TOTAL\n"
    printf "---------------------------------------------\n"
}
#運行中
{
    math+=$3
    english+=$4
    computer+=$5
    printf "%-6s %-6s %4d %8d %8d %8d\n", $1, $2, $3,$4,$5, $3+$4+$5
}
#運行后
END {
    printf "---------------------------------------------\n"
    printf "  TOTAL:%10d %8d %8d \n", math, english, computer
    printf "AVERAGE:%10.2f %8.2f %8.2f\n", math/NR, english/NR, computer/NR
}

我們來看一下執(zhí)行結果:

$ awk -f cal.awk score.txt
NAME    NO.   MATH  ENGLISH  COMPUTER   TOTAL
---------------------------------------------
Marry  2143     78       84       77      239
Jack   2321     66       78       45      189
Tom    2122     48       77       71      196
Mike   2537     87       97       95      279
Bob    2415     40       57       62      159
---------------------------------------------
  TOTAL:       319      393      350
AVERAGE:     63.80    78.60    70.00

另外一些實例

AWK的hello world程序為:

BEGIN { print "Hello, world!" }

計算文件大小

$ ls -l *.txt | awk '{sum+=$6} END {print sum}'
--------------------------------------------------
666581

從文件中找出長度大于80的行

awk 'lenght>80' log.txt

打印九九乘法表

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276