awk使用詳解(三)變量、數字表達式、賦值運算符、BGGIN、END、Built-in 變量

Awk 變量、數字表達式、賦值運算符

1. Awk 變量:以下是定義Awk變量

variable_name=value 

句法中:

1. variable_name: 是一個變量

2. value: 存儲在變量中的值

例如:

computer_name=”tecmint.com”

port_no=”22”

email=”[email protected]

server=”computer_name”

例如變量接收一個域 

first_name=$2

second_name=$3

 first_name is 設置爲第二個域、 second_name is 設置成第三個域.

 names.txt 文件如下:

$ cat names.txt

List File Content Using cat Command

使用變量 first_name 和 second_name 來保存第一個用戶的第一和第二個名字。以下是Awk例子:

$ awk '/Aaron/{ first_name=$2 ; second_name=$3 ; print first_name, second_name ; }' names.txt

Store Variables Using Awk Command

uname -a 打印系統信息.

 hostname, 我們能保存 hostname 在變量 hostname中,使用 Awk 打印出來:

$ uname -a$ uname -a | awk '{hostname=$2 ; print hostname ; }' 

Store Command Output to Variable Using Awk

2. 數字表達式

Awk中, 數字操作符:

1. * : 乘

2. + : 加

3. / : 除

4. - : 減肥

5. % : 餘

6. ^ : 冪

數字表達式:

$ operand1 operator operand2

counter=0

num1=5

num2=10

num3=num2-num1

counter=counter+1

以下爲包含:域名的domains.txt 文件

news.tecmint.com

tecmint.com

linuxsay.com

windows.tecmint.com

tecmint.com

news.tecmint.com

tecmint.com

linuxsay.com

tecmint.com

news.tecmint.com

tecmint.com

linuxsay.com

windows.tecmint.com

tecmint.com

查看文件:

$ cat domains.txt

View Contents of File

以下計算 tecmint.com 在文件中使用次數:

#!/bin/bash
for file in $@; do
if [ -f $file ] ; then
#print out filename
echo "File is: $file"
#print a number incrementally for every line containing tecmint.com
awk  '/^tecmint.com/ { counter=counter+1 ; printf "%s\n", counter ; }'   $file
else
#print error info incase input is not a file
echo "$file is not a file, please specify a file." >&2 && exit 1
fi
done
#terminate script with exit code 0 in case of successful execution
exit 0

Shell-Script-to-Count-a-String-in-File.p

保存腳本,以下爲運行腳本:

$ ./script.sh  ~/domains.txt

Script-To-Count-String.png

3. 賦值操作符

1. *= : multiplication assignment operator

2. += : addition assignment operator

3. /= : division assignment operator

4. -= : subtraction assignment operator

5. %= : modulus assignment operator

6. ^= : exponentiation assignment operator

句法:

$ variable_name=variable_name operator operand

例子:

counter=0

counter=counter+1

num=20

num=num-1

例子:

variable_name operator=operand

counter=0

counter+=1

num=20

num-=1

以下爲使用: += 操作符的shell腳本:

#!/bin/bash
for file in $@; do
if [ -f $file ] ; then
#print out filename
echo "File is: $file"
#print a number incrementally for every line containing tecmint.com
awk  '/^tecmint.com/ { counter+=1 ; printf  "%s\n",  counter ; }'   $file
else
#print error info incase input is not a file
echo "$file is not a file, please specify a file." >&2 && exit 1
fi
done
#terminate script with exit code 0 in case of successful execution
exit 0

Alter-Shell-Script.png


Awk 專有‘BEGIN 和 END’ – 模式

句法:

# awk 'script' filenames  

Awk 腳本

/pattern/ { actions } 

使用 BEGIN 和 END

awk '

BEGIN { actions } 

/pattern/ { actions }

/pattern/ { actions }……….

END { actions } 

' filenames  

1.  BEGIN 模式使用於腳本時, 在開始讀入輸入行之前,所有的動作被執行一次.

2. 輸入行並解析.

3. 執行匹配的輸入行動作.

4. 重複2、3步.

5. 當讀完所有輸入行,  END 模式被執行.

news.tecmint.com

tecmint.com

linuxsay.com

windows.tecmint.com

tecmint.com

news.tecmint.com

tecmint.com

linuxsay.com

tecmint.com

news.tecmint.com

tecmint.com

linuxsay.com

windows.tecmint.com

tecmint.com

$ cat ~/domains.txt

View Contents of File

以下文本例子:

#!/bin/bash
for file in $@; do
if [ -f $file ] ; then
#print out filename
echo "File is: $file"
#print a number incrementally for every line containing tecmint.com 
awk '/^tecmint.com/ { counter+=1 ; printf "%s\n", counter ; }' $file
else
#print error info incase input is not a file
echo "$file is not a file, please specify a file." >&2 && exit 1
fi
done
#terminate script with exit code 0 in case of successful execution 
exit 0

以下是使用 BEGIN 和END 

awk '/^tecmint.com/ { counter+=1 ; printf "%s\n", counter ; }' $file

To:

awk ' BEGIN {  print "The number of times tecmint.com appears in the file is:" ; }
/^tecmint.com/ {  counter+=1  ;  }END {  printf "%s\n",  counter  ; }
'  $file

 shell 腳本如下:

#!/bin/bash
for file in $@; do
if [ -f $file ] ; then
#print out filename
echo "File is: $file"
#print the total number of times tecmint.com appears in the file
awk ' BEGIN {  print "The number of times tecmint.com appears in the file is:" ; }
/^tecmint.com/ {  counter+=1  ;  }END {  printf "%s\n",  counter  ; }
'  $file
else
#print error info incase input is not a file
echo "$file is not a file, please specify a file." >&2 && exit 1
fi
done
#terminate script with exit code 0 in case of successful execution
exit 0

Awk BEGIN and END Patterns

以上首先打印 domains.txt文件, 然後Awk 腳本被執行,  BEGIN 模式輸出“The number of times tecmint.com appears in the file is:” 。然後, /^tecmint.com/ 比較每一個輸入行且動作在 { counter+=1 ; }時,被執行計數 tecmint.com 。最後  END 模式 打印出tecmint.com總的計數。

$ ./script.sh ~/domains.txt 

Script to Count Number of Times String Appears


  使用 Awk Built-in 變量 

1. FILENAME : 輸入文件名( 不改變量名)

2. FR : 輸入行號  (1, 2, 3… 等, 不改變量名)

3. NF : 輸入行域號 (不改變變量名)

4. OFS : 輸出域分隔符

5. FS : 輸入域分隔符

6. ORS : 輸出記錄分隔符

7. RS : 輸入記錄分隔符

例子:

$ awk ' { print FILENAME } ' ~/domains.txt 

Awk FILENAME Variable

 Awk 缺省行爲是使用 FILENAME built-in 變量.

使用 NR :注意空行

$ cat ~/domains.txt

Print Contents of File

$ awk ' END { print "Number of records in file is: ", NR } ' ~/domains.txt 

Awk Count Number of Lines

使用NR built-in 變量計數:

$ cat ~/names.txt

List File Contents

$ awk '{ print "Record:",NR,"has",NF,"fields" ; }' ~/names.txt

Awk Count Number of Fields in File

使用 FS built-in變量,分割輸入行到域.

缺省 FS 是 space 和tab, 有兩個方法:

1. 方法一使用 FS built-in 變量.

2. 第二個方法使用 -F Awk選項

考慮到 /etc/passwd 是系統文件;使用,: 字符, 因此可以說明一個新的輸入域名分隔符,過濾某些域。:

使用 -F 選項:

$ awk -F':' '{ print $1, $4 ;}' /etc/passwd

Awk Filter Fields in Password File

使用 FS built-in 變量優化:

$ awk ' BEGIN {  FS=“:” ; }  { print $1, $4  ; } ' /etc/passwd


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章