亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747


Linux技巧:awk 命令簡(jiǎn)單入門(mén)介紹

 

在 linux 命令中,awk 命令常用于處理文本內(nèi)容。下面基于實(shí)例介紹 awk 命令的常見(jiàn)用法。

GNU gawk

awk 既是一個(gè)命令,也是一種程序語(yǔ)言,它可以有不同的實(shí)現(xiàn)版本。

在 Linux 系統(tǒng)中,awk 的實(shí)現(xiàn)版本是 GNU gawk。

在 shell 中執(zhí)行 awk 命令,實(shí)際執(zhí)行的是 gawk 命令。如下所示:

$ ls -l /usr/bin/awk
lrwxrwxrwx 1 root root 21  2月  1  2019 /usr/bin/awk -> /etc/alternatives/awk
$ ls -l /etc/alternatives/awk
lrwxrwxrwx 1 root root 13  3月  8  2019 /etc/alternatives/awk -> /usr/bin/gawk
$ ls -l /usr/bin/gawk
-rwxr-xr-x 1 root root 441512  7月  3  2013 /usr/bin/gawk

可以看到,/usr/bin/awk 文件最終鏈接到 /usr/bin/gawk 文件,/usr/bin/gawk 文件沒(méi)有再鏈接到其他文件。

在下面的描述中,如無(wú)特別說(shuō)明,所說(shuō)的 awk 是指 GNU gawk。

awk 命令格式

查看 man awk 的說(shuō)明,也是鏈接到 man gawk 的內(nèi)容,這個(gè)說(shuō)明比較難懂,不夠清晰,可以再參考 GNU gawk 在線幫助手冊(cè) https://www.gnu.org/software/gawk/manual/gawk.html 的說(shuō)明。

下面引用的內(nèi)容就出自這個(gè)在線幫助手冊(cè),其中對(duì) awk 的基本介紹如下:

The basic function of awk is to search files for lines (or other units of text) that contain certain patterns.

When a line matches one of the patterns, awk performs specified actions on that line.

awk continues to process input lines in this way until it reaches the end of the input files.

awk 命令的基本用法說(shuō)明如下:

There are several ways to run an awk program.

If the program is short, it is easiest to include it in the command that runs awk, like this:awk 'program' input-file1 input-file2 …

where program consists of a series of patterns and actions, an awk program looks like this:

pattern { action }

 

When the program is long, it is usually more convenient to put it in a file and run it with a command like this:

awk -f program-file input-file1 input-file2 …

 

There are single quotes around program so the shell won’t interpret any awk characters as special shell characters.

The quotes also cause the shell to treat all of program as a single argument for awk, and allow program to be more than one line long.

 

You can also run awk without any input files. If you type the following command line:

awk 'program'

awk Applies the program to the standard input, which usually means whatever you type on the keyboard.

即,awk 命令在所給文件中查找包含特定模式的行,并對(duì)找到的行進(jìn)行特定處理。這些特定處理由 program 參數(shù)指定。

上面提到,所給的 program 參數(shù)要用單引號(hào)括起來(lái),避免 shell 對(duì)一些特殊字符進(jìn)行擴(kuò)展。

如果沒(méi)有提供文件名,awk 命令默認(rèn)會(huì)讀取標(biāo)準(zhǔn)輸入。

如果沒(méi)有提供特定模式,默認(rèn)處理所有行。

注意:跟在 awk 命令的 program 參數(shù)后面的參數(shù)會(huì)被認(rèn)為是文件名,即使用引號(hào)把參數(shù)值括起來(lái)也還是當(dāng)成文件名,不會(huì)當(dāng)成字符串。

這個(gè)命令不能處理命令行參數(shù)提供的字符串值,具體舉例說(shuō)明如下:

$ cat testawk
This is a test string.
This is another TEST string.
$ awk '{print $3}' testawk
a
another
$ awk '{print $3}' "testawk"
a
another
$ awk '{print $3}' "This is a test string."
awk: fatal: cannot open file `This is a test string.' for reading (No such file or directory)

可以看到,awk '{print $3}' testawk 命令打印出 testawk 文件的第三列內(nèi)容。

awk '{print $3}' "testawk" 命令也是打印出 testawk 文件的第三列內(nèi)容。即使用雙引號(hào)把 testawk 括起來(lái),也不代表是打印 "testawk" 字符串的第三列。

而 awk '{print $3}' "This is a test string." 命令會(huì)執(zhí)行報(bào)錯(cuò),提示找不到名為 This is a test string. 的文件,它不會(huì)處理 "This is a test string." 這個(gè)字符串自身的內(nèi)容,而是把該字符串當(dāng)成文件名,要處理對(duì)應(yīng)文件的內(nèi)容。

如果確實(shí)需要用 awk 命令來(lái)處理字符串,可以用管道操作符 | 來(lái)連接標(biāo)準(zhǔn)輸入。

例如用 echo 命令打印字符串的值,然后通過(guò)管道操作符把這個(gè)值傳遞到 awk 命令的標(biāo)準(zhǔn)輸入。

具體舉例如下:

$ echo "This is a test string." | awk '{print $4}'
test
$ value="This is a new test string."
$ echo "$value" | awk '{print $4}'
new

可以看到,echo "This is a test string." | awk '{print $4}' 命令通過(guò) echo 先輸出字符串的值,再通過(guò)管道操作符 | 把這個(gè)輸出連接到 awk 命令的標(biāo)準(zhǔn)輸入,就能對(duì)這個(gè)字符串進(jìn)行處理,不會(huì)執(zhí)行報(bào)錯(cuò)。

echo "$value" | awk '{print $4}' 命令打印出 value 變量值的第四列內(nèi)容,可以用這個(gè)方式來(lái)對(duì)變量值進(jìn)行處理。

注意:這里使用管道操作符 | 來(lái)連接標(biāo)準(zhǔn)輸入,讓 awk 命令能夠處理傳入到標(biāo)準(zhǔn)輸入的字符串,但是使用重定向標(biāo)準(zhǔn)輸入操作符 < 并不能讓 awk 命令處理字符串。

重定向是基于文件的操作,所給的字符串會(huì)被當(dāng)成文件名,舉例如下:

$ awk '{print $4}' < "This is a test string."
-bash: This is a test string.: No such file or directory

可以看到,在重定向標(biāo)準(zhǔn)輸入操作符 < 右邊的 "This is a test string." 字符串被當(dāng)成文件名,bash 提示找不到文件。

這里不是 awk 命令報(bào)錯(cuò),而是 bash 在處理重定向的時(shí)候報(bào)錯(cuò)。

awk program

使用 awk 命令的關(guān)鍵在于,program 參數(shù)要怎么寫(xiě)。

查看 GNU gawk 在線幫助手冊(cè)的說(shuō)明,列舉部分內(nèi)容如下:

  • Programs in awk consist of pattern–action pairs.
  • An action without a pattern always runs.
  • An awk program generally looks like this: [pattern] { action }
  • Patterns in awk control the execution of rules -- a rule is executed when its pattern matches the current input record.
  • The purpose of the action is to tell awk what to do once a match for the pattern is found.
  • An action consists of one or more awk statements, enclosed in braces (‘{…}’).

即,awk 命令的 program 參數(shù)由 pattern 和 action 組成。Pattern 用于指定匹配模式,并對(duì)匹配的行執(zhí)行后面的 action 操作,不匹配的行不做處理。Action 用于指定要對(duì)匹配到的行進(jìn)行什么樣的操作,這些操作語(yǔ)句要包含在大括號(hào) {} 里面。如果沒(méi)有提供 pattern 參數(shù),默認(rèn)處理所有行。

部分 pattern 參數(shù)的寫(xiě)法說(shuō)明如下:

  • /regular expression/
    A regular expression. It matches when the text of the input record fits the regular expression.
  • expression
    A single expression. It matches when its value is nonzero (if a number) or non-null (if a string).

具體舉例說(shuō)明如下:

$ awk '/a.*/ {print $0}' testawk
This is a test string.
This is another TEST string.
$ awk '/test/ {print $0}' testawk
This is a test string.
$ awk 'test {print $0}' testawk
$ awk '"NONE" {print $0}' testawk
This is a test string.
This is another TEST string.
$ awk '$3 == "another" {print $0}' testawk
This is another TEST string.

可以看到,awk '/a.*/ {print $0}' testawk 命令使用 a.* 正則表達(dá)式來(lái)匹配包含字符 ‘a’ 的行,然后打印出整行內(nèi)容。

awk '/test/ {print $0}' testawk 命令則是打印包含 "test" 字符串的行。

awk 'test {print $0}' testawk 命令什么都沒(méi)有打印,這種寫(xiě)法并不表示打印包含 "test" 字符串的行。

awk '"NONE" {print $0}' testawk 命令打印出 testawk 文件的所有行,雖然這個(gè)文件并沒(méi)有包含 "NONE" 字符串。基于上面說(shuō)明,所給的 pattern 參數(shù)是一個(gè)用雙引號(hào)括起來(lái)的非空字符串,表示總是匹配,不管這個(gè)字符串的內(nèi)容是什么。

awk '$3 == "another" {print $0}' testawk 命令匹配第三列內(nèi)容為 "another" 字符串的行,并打印出整行內(nèi)容。

即,如果要指定匹配某個(gè)字符串,pattern 參數(shù)寫(xiě)為 “/regular expression/” 的形式會(huì)比較簡(jiǎn)單,要寫(xiě)為 “expression” 形式,需要了解 awk 的表達(dá)式寫(xiě)法。

獲取所給行的內(nèi)容

awk 在讀取每行內(nèi)容時(shí),會(huì)基于分割字符把行內(nèi)容拆分成多個(gè)單詞,可以用 $number 來(lái)獲取第 number 列的單詞,number 值從 1 開(kāi)始。例如,$1 對(duì)應(yīng)第一列的單詞,$2 對(duì)應(yīng)第二列的單詞,$3 對(duì)應(yīng)第三列的單詞,依此類推。可以用 $NF 來(lái)獲取拆分后的最后一列內(nèi)容。

特別的,$0 獲取到整行的內(nèi)容,包括行首、或者行末的任意空白字符。

以 "This is a test string." 這一行進(jìn)行舉例,有如下的對(duì)應(yīng)關(guān)系:

Linux技巧:awk 命令簡(jiǎn)單入門(mén)介紹

awk行字段獲取方法

使用 -F 選項(xiàng)指定分割字符

前面提到,awk 默認(rèn)使用空格來(lái)拆分行內(nèi)容為多個(gè)單詞。如果想要基于其他字符來(lái)進(jìn)行拆分,可以使用 -F 選項(xiàng)來(lái)指定分割字符。

GNU gawk 在線幫助手冊(cè)對(duì) -F 選項(xiàng)說(shuō)明如下:

-F fs

--field-separator fs

Set the FS variable to fs.

例如對(duì) "clang/utils/analyzer/" 這樣的目錄路徑來(lái)說(shuō),如果想要基于 / 進(jìn)行拆分,以便獲取各個(gè)目錄名,就可以使用 -F 選項(xiàng)來(lái)指定分割字符為 /

具體舉例如下:

$ echo "clang/utils/analyzer/" | awk -F '/' '{print $1, $2}'
clang utils
$ echo "clang/utils/analyzer/" | awk -F '/' '{print "Last word is: " $NF}'
Last word is: 

可以看到,使用 -F '/' 指定分割字符后,所給內(nèi)容會(huì)以 / 來(lái)進(jìn)行拆分,拆分后的單詞不包含 ‘/’ 這個(gè)字符。

由于所給內(nèi)容的最后一個(gè)字符是 ‘/’,最后一列拆分后的內(nèi)容為空,所以 $NF 的內(nèi)容為空。

當(dāng)需要基于特定字符分割行內(nèi)容時(shí),使用 awk 命令特別實(shí)用,-F 選項(xiàng)可以指定分割字符,然后用 $number 就能獲取到第 number 列的內(nèi)容,方便處理。

print 語(yǔ)句

前面的例子都用了 print 語(yǔ)句來(lái)打印內(nèi)容。

GNU gawk 在線幫助手冊(cè)對(duì) print 語(yǔ)句的說(shuō)明如下:

Use the print statement to produce output with simple, standardized formatting.

You specify only the strings or numbers to print, in a list separated by commas.

They are output, separated by single spaces, followed by a newline.

The statement looks like this:

print item1, item2, …

 

The entire list of items may be optionally enclosed in parentheses.

The simple statement ‘print’ with no items is equivalent to ‘print $0’: it prints the entire current record.

即,print 語(yǔ)句打印所給字符串、或者數(shù)字的內(nèi)容,不同內(nèi)容之間要用逗號(hào) ‘,' 隔開(kāi),但是打印出來(lái)的效果是用空格隔開(kāi)。

經(jīng)過(guò)測(cè)試,如果用其他字符隔開(kāi)會(huì)不生效,打印的內(nèi)容會(huì)連在一起。具體舉例說(shuō)明如下:

$ awk '/test/ {print $3, $5}' testawk
a string.
$ awk '/test/ {print $3 $5}' testawk
astring.
$ awk '/test/ {print $3_$5}' testawk
astring.

可以看到,在 print 后面寫(xiě)為 $3, $5 時(shí),打印的兩個(gè)字符串用空格隔開(kāi),而寫(xiě)為 $3 $5、或者 $3_$5,打印的兩個(gè)字符串直接連在一起,沒(méi)有打印所給的空格、或者下劃線 ‘_’。即,只能用逗號(hào)來(lái)隔開(kāi)。

如果在 print 后面沒(méi)有提供參數(shù),默認(rèn)相當(dāng)于 print $0,會(huì)打印整行內(nèi)容。如果沒(méi)有提供任何 action 參數(shù),連大括號(hào) {} 都不提供,默認(rèn)相當(dāng)于 { print $0 }。如果只提供大括號(hào) {},大括號(hào)里面沒(méi)有內(nèi)容,則是空操作,什么都不做。

具體舉例如下:

$ awk '/test/ {print}' testawk
This is a test string.
$ awk '/test/' testawk
This is a test string.
$ awk '/test/ {}' testawk

可以看到,awk '/test/ {print}' testawk 命令在 print 后面提供參數(shù),打印出整行內(nèi)容。

awk '/test/' testawk 命令沒(méi)有提供 action 參數(shù),也是打印出整行內(nèi)容。

awk '/test/ {}' testawk 命令提供了 action 參數(shù),只是沒(méi)有指定要做的操作,什么都沒(méi)有打印。

分享到:
標(biāo)簽:命令 awk
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定