AWK Programming

Introduction

Data Model

How Does Awk work?

Invoke

Program Structure

Records and Files

Built-In Variables

Print Statement

Begin and End

Examples of Begin

Specify Ranges by Pattern

Examples: Counting

Example: Sums and Averages

Example: Handling Text

Example: String Concatenation

Example: Print Last Input Line

Example: Built-in Functions

Example: Counting Lines, Words, and Characters

Flow Control Flow

Flow Control Flow - While

Flow Control Flow - For

String Concatenation

Arrays

Arrays

Some Useful Simple Scripts

Some Useful Simple Scripts

Conditional Operations

Pattern Expression

String Matching

Metacharacters

Escape Sequence

Basic regular expression

Combined regular expression

Regular Expression: Examples

Exp Regular Expression: Examples

Exp Regular Expression: Examples

Output

Output into Files

Output to Pipes

Getline Function

Variable

Expression

The System Function

Built-in String Functions

Scripts for NS2 Trace Data

Scripts for NS2 Trace Data - Avg. Packet Delay

Scripts for NS2 Trace Data - Avg. Throughput


Untitled Document
Introduction
Basic data unit - word
Good for processing formatted input
Pattern match capability
C-like data manipulation capability
 
AWK是一種處理文字檔案的語言。它將檔案內容視為具有固定欄位的一系列記錄, 每一行都是一個記錄。AWK逐行讀取輸入檔案,並根據使用者的指令進行處理。 每行內容都會被分割成一系列的欄位存入預設變數($1, $2, $3, ...等)。 此外還有一些常用的預設變數,例如NR(讀到第幾行)及 NF(本行欄位數目)等,節省 不少程式設計的麻煩。 只要在輸入資料中有模式匹配,就執行一系列指令。

AWK直譯器對每個輸入行先判斷它是否符合使用者指定的模式(pattern),再根據結果觸發後續 的處理動作。而處理動作的語法非常類似C程式語言,對於已經熟悉C程式語言的使用者而言, 學習AWK程式語言是輕而易舉的事。讀者可從網路上找到海量的教材自行學習。

Thu Jan 11 14:33:42 CST 2024 Untitled Document
Data Model
   
AWK 讀進一份資料時,會將檔案內容逐行的剖析成 一欄欄的資料,例如下面的一個含有四個欄位 (name, age, wagerate, hometown)的資料,及其 在AWK內的資料結構:
原始資料 AWK 內部資料結構
   
David 30 25.2 Chicago
Chris 20 20.4 Indy
Joe 55 40.2 Columbus
Jane 42 35.7 Holmdel
Bob 60 52.2 Naperville
$1 $2 $3 $4
David 30 25.2 Chicago
Chris 20 20.4 Indy
Joe 55 40.2 Columbus
Jane 42 35.7 Holmdel
Bob 60 52.2 Naperville
而AWK 語法提供程式設計師以這種資料結構,進行程式設計。
結果
# -------------------------------------
# Script: awk-1
# ------------------------------------- 
$2 == 55 { print $4, $3 * 10 } # ------------------------------------- # 第二欄的資料如果是55, 則印出第四欄及第三欄的十倍
Columbus 402
Thu Jan 11 14:33:42 CST 2024 Untitled Document
How Does Awk work?
   
scan set of input files in order
   
searching for lines which match the specified patterns,
   
and take actions
Thu Jan 11 14:33:42 CST 2024 Untitled Document
Invoke
Invoke from Shell
awk -f awfscript input-file
Thu Jan 11 14:33:42 CST 2024 Untitled Document
Program Structure
Pattern { action }
   
if no pattern, do action for every line
   
if no action, print the input line
Thu Jan 11 14:33:42 CST 2024 Untitled Document
Records and Files
   
The input stream is divided into "records" terminated by a record separator
   
$1 $2 $3 ...
   
$0 represents the entire line
Thu Jan 11 14:33:43 CST 2024 Untitled Document
Built-In Variables
FILENAME
file name
NR
number of records read
NF
number of fields in current line read
FS
field separator
RS
record separator
OFS
output field separator
ORS
output record separator
Thu Jan 11 14:33:43 CST 2024 Untitled Document
Print Statement
Script ID    AWK Script 意義
awk-2-1
{ print }
copy input to output
awk-2-2
{ print NR, $2, $1 }
print line-number, 2nd, and 1st fields
awk-2-3
{ print $2 $1 }
print 2nd and 1st fields
awk-2-4
{ printf "%8.2f %10d\n", $2 , $1 }
print 2nd and 1st fields in designated format
awk-2-5
{ print > "outfile"}
print to "outfile"
awk-2-6
{ print | "mail bbs" }
pipe to mail
Thu Jan 11 14:33:43 CST 2024 Untitled Document
Begin and End
BEGIN { FS = ":" }
set the field separator as ":"
BEGIN { FS = "#|%" }
set the field separator as "#" or "%"
may not work on some systems
END { print NR }
count the number of lines in the input file
Thu Jan 11 14:33:43 CST 2024 Untitled Document
Examples of Begin

 

 

§ 功能   
華氏溫度轉攝氏溫度
用法
f2c <temperature>
Script
3.5-X
#   f2c - $(()) version  
t=$1 echo $(((t-32)*5/9))
Script
3.5-X
#   f2c - awk version
awk "BEGIN {print ($1-32)*5/9;}" /dev/null
解釋
/dev/null 是一個有名無實的虛擬檔案 (dummy file),放在這個位置讓 awk 取用,否則 awk 會等著使用者從 STDIN 輸入資料,而不會自動停止。

 

§ 功能   
攝氏溫度轉華氏溫度
用法
c2f <temperature>
Script
3.5-X
#   c2f - $(( ))  version 
t=$1 echo $((t*9/5+32))
Script
3.5-X
#   c2f -awk version 
awk "BEGIN {print ($1*9/5)+32;}" /dev/null
Thu Jan 11 14:33:43 CST 2024 Untitled Document
Specify Ranges by Pattern

  /start/,/end/
  NR==1, NR ==200
Thu Jan 11 14:33:44 CST 2024 Untitled Document
Examples: Counting
# -----------------------------------------------
# Script: awk-3  計算第三欄的值大於15的record 數量
# ----------------------------------------------- 
$3 > 15 { count++ ;} END {print count;}
Thu Jan 11 14:33:44 CST 2024 Untitled Document
Example: Sums and Averages
   
# -----------------------------------------------
# Script: awk-4  計算第二欄的平均值
# ----------------------------------------------- 
{ sum += $2; } END {print sum / NR; }
Thu Jan 11 14:33:44 CST 2024 Untitled Document
Example: Handling Text

   
# -----------------------------------------------
# Script: awk-5  找出薪資最高的員工
# ----------------------------------------------- 
$2 > maxrate { maxrate = $2; maxemp = $1; } END { print "highest rate:", maxrate, "for", maxemp ;}
Thu Jan 11 14:33:44 CST 2024 Untitled Document
Example: String Concatenation
   
# -----------------------------------------------
# Script: awk-6  將第一欄(名字 )印出成一行
# ----------------------------------------------- 
{ names = names $1 " " ;} END { print names ;}
 
結果 (使用 awk-1 的輸入檔)

David Chris Joe Jane Bob
Thu Jan 11 14:33:45 CST 2024 Untitled Document
Example: Print Last Input Line
   
NR retains its value in an END action, $0 does not.
   
# -----------------------------------------------
# Script: awk-7 印出最後一行
# ----------------------------------------------- 
{last = $0 ;} END { print last; }
Thu Jan 11 14:33:45 CST 2024 Untitled Document
Example: Built-in Functions
Script: awk-8 結果
(使用 awk-1 的輸入檔)
   
# -----------------------------------------------
# Script: awk-8 印出第一欄及其字串長度
# ----------------------------------------------- 
{ print $1, length($1); }
   
David 5
Chris 5
Joe 3
Jane 4
Bob 3
Thu Jan 11 14:33:45 CST 2024 Untitled Document
Example: Counting Lines, Words, and Characters
 
# ---------------------------------------------------------
# Script: awk-9 計算行數(line),詞數(word)及字元數 (character)
# --------------------------------------------------------- 
{ nc = nc + length ($0) + 1 ; nw += NF; } END { print NR, "lines,", nw, "words,", nc, "chars" ;}
Thu Jan 11 14:33:45 CST 2024 Untitled Document
Flow Control Flow
 
Same as C
If-Else
 
# ---------------------------------------------------------
# Script: awk-10 計算時薪超過200元的員工平均時薪
# 輸入資料格式: (Name, Rate, WorkingHours)
# --------------------------------------------------------- 
$2 > 200 { n++ ; pay += $2 * $3 ;} END { if (n > 0) print n, "employee, pay =", pay, "avg. =", pay/n); else print "No employee are paid more than $200/hour"; }
Thu Jan 11 14:33:45 CST 2024 Untitled Document
Flow Control Flow - While
While 迴圈

Script awk-11 結果
   
# ---------------------------------------------------------
# Script: awk-11 計算複利率,並印出各年的總額
# 輸入資料格式: (本金, 利率, 期限)
# 輸入資料    :  1000 0.06 5
# --------------------------------------------------------- 
$2 > 200 { n++ ; pay += $2 * $3 ;} {i = 1; while ( i <= $3 ) { printf("\t%.2f\n", $1*(1+$2)^i); i++; } }
1060.00
1123.60
1191.02
1262.48
1338.23
Thu Jan 11 14:33:46 CST 2024 Untitled Document
Flow Control Flow - For
For 迴圈

   
# ---------------------------------------------------------
# Script: awk-12 與P awk-11 相同
# ---------------------------------------------------------  
 { for ( i = 1; i <= $3; i ++)
    printf ("\t%.2f\n", $1*(1+$2)^i);
 }
Thu Jan 11 14:33:46 CST 2024 Untitled Document
String Concatenation
 
字串的連接 (concatenation) 只需將各字串放在一起以空白隔開即可:
   length($1 $2 $3)
   print $1 " is " $2
Thu Jan 11 14:33:46 CST 2024 Untitled Document
Arrays
 
AWK 支援 Array
1. 不須事先宣告即可使用
2. 支援 Associative Array (其索引是一個字串)
 
例: Conventional Array (索引是整數)
   
# -------------------------------------------------------
# Script: awk-13 將輸入資料以相反順序(由下至上)印出
# ------------------------------------------------------- 
{ line[NR] = $0; } END { for ( i = NR; i > 0; i-- ) print line[i]; }
Thu Jan 11 14:33:46 CST 2024 Untitled Document
Arrays
 
例: Associative Array (索引是字串)
   
# -------------------------------------------------------------
# Script: awk-14  統計含有 apple 及 orange 字串的  record 數量
# ------------------------------------------------------------- 
/apple/ { x["apple"]++;} /orange/ { x["orange"]++;} END { print x["apple"], x["ornage"];}
Thu Jan 11 14:33:46 CST 2024 Untitled Document
Some Useful Simple Scripts
 
一些簡單的 AWK script

Script ID   Script   說明
 awk-15-1
END {print NR;}
印出總行數 (相當於 wc -l)
 awk-15-2
NR == 10
印出第10行
 awk-15-3
NF > 4
印出超過4個欄位的record
 awk-15-4
NF > 0
印出至少有一個欄位的record
 awk-15-5
{ print NF;}
印出每一個record 的欄位數
 awk-15-6
{ print NF, $0;}
印出每一個record 的欄位數,及該行
awk-15-7
length ($0) > 80
印出長度大於80 的record
awk-15-8
{ print $2, $1; }
印出第二欄及第一欄
awk-15-9
{ $1 = NR; print; }
如果第一欄的值等於該行的行數,印出該行
awk-15-10
{ $2 = ""; print; }
如果第二欄是空字串,印出該行
Thu Jan 11 14:33:47 CST 2024 Untitled Document
Some Useful Simple Scripts
Script ID   Script   說明
awk-15-11
{ tmp = $1; $1 = $2; $2 = tmp; print; }
印出每一個record 的欄位數,及該行
 awk-15-12
{ nf += NF;}
END {print nf;}
印出每一個record 的欄位數,及該行 統計輸入資料中共有多少欄
(可用來計算一個檔案內共有多少英文字)
 awk-15-13
/Beth/ { nlines ++; }
END { print nlines; }
印出每一個record 的欄位數,及該行 統計輸入資料中有多少行含有字串 'Beth'
 awk-15-14
BEGIN {max=-999999;}
$1 > max { max = $1; maxline = $0; }
END { print max, maxline; }
印出第一欄的最大值及行數
 awk-15-15
{ for ( i = NF; i > 0; i--)
printf("%s ", $i);
printf ("\n");
}
將每一個record 內個各欄依照相反的順序印出來
 15-16
{ sum = 0;
for ( i=1; i <= NF; i++) sum += $i;
print sum;
}
將每一個record 內個各欄相加,印出來,每一個record 一個小計
 15-17
{ for ( i=1; i <= NF; i++) sum += $i;
END{ print sum; } }
將每一個record 內個各欄相加,最後印出總計
 15-18
{ for (i=1;i <= NF;i++)
if ($i<0) $i = -$i;
print;
}
將每一個record 內個各欄的負值轉正。
註:如果一個檔案內除了負值之外,沒有'-'號, 可以用 ex 直接刪掉負號即可,更為簡單。
Thu Jan 11 14:33:47 CST 2024 Untitled Document
Conditional Operations
BEGIN { statement }
END { statement }
expression { statement }
/regular expression/ { statement }
compound pattern { statement }
pattern,pattern { statement }
Thu Jan 11 14:33:47 CST 2024 Untitled Document
Pattern Expression
比對運算元
 
數值比對
<
>
<=
>=
==
!=
 
字串比對
~
(Match)
!~
(Not Match)
Thu Jan 11 14:33:47 CST 2024 Untitled Document
String Matching
字串比對
格式 說明
/regexpr/
match to current line
/Asia/
expr ~ /regexpr/
match to expr
$4 ~ /Asia/
expr !~ /regexpr/
not match to expr
$2 !~ /Asia/
Thu Jan 11 14:33:47 CST 2024 Untitled Document
Metacharacters
字串中的特殊符號 (Meta Characters)
  \  
  ^  
  $  
  [  
  ]  
  |  
  (  
  )  
  .  
  *  
  +  
  ? 
Thu Jan 11 14:33:48 CST 2024 Untitled Document
Escape Sequence
字串中的特殊符號 (Escape Sequecnce)
符號 意義
\t
tab
\b
backspace
\f
formfeed
\n
newline
\r
carriage return
\ddd
八進位數字 (octal value) ddd
\c
其他特殊符號(消除特殊意義)
例: \\ for \, \* for *, \" for "
Thu Jan 11 14:33:48 CST 2024 Untitled Document
Basic regular expression
基本 Regular Expression
符號 意義
^
the beginning of a string
$
the end of a string
  • any single character
    [ABC]
    any of the characters, A, B, or C.
    [A-Za-z]
    any single letter
    [^0-9]
    any character but digit
    Thu Jan 11 14:33:48 CST 2024 Untitled Document
    Combined regular expression
    合併的Regular Expression
    功能 符號 意義
    alternation
    A|B
    A or B
    concatenation
    AB
    to string AB
    closure
    A*
    zero or more A's
    positive closure
    A+
    one or more A's
    zero or one
    A?
    zero or one A's
    parentheses
    (r)
    the same string as r does.
    Thu Jan 11 14:33:48 CST 2024 Untitled Document
    Regular Expression: Examples
    Regular Expression 之例
    意義
    ^C
    以'C'開頭的字串
    C$
    以'C'結尾的字串
    ^C$
    字串只含一個'C'
    [AEIOU]
    字串含有 AEIOU 中之任一字元
    ^.$
    字串只含有一個字元
    ^...$
    字串只含有三個字元
     ...
    任意三個連在一起的字元
    \.$
    字串的結尾是一個'.'
    ^[ABC]
    以A或B或C開頭之字串
    ^[^ABC]
    不是以A或B或C開頭之字串
    [^ABC]
    A或B或C以外之字元
    ^[^a-z]$
    字串只含有一個a-z以外之字元
    (Asian|European)
    Asian 或 European
    (male|female)
    male 或 female
    (black|blue) bird
    black bird 或 blue bird
    Thu Jan 11 14:33:49 CST 2024 Untitled Document
    Exp Regular Expression: Examples
    意義
    B*
    空字串 或 B 或 BB, etc.
    AB*C
    AC 或 ABC 或 ABBC, etc.
    AB+C
    ABC 或 ABBC, etc.
    ABB*C
    ABC 或 ABBC, etc.
    AB?C
    AC 或 ABC
    [A-Z]+
    含有一個或更多的大寫英文字母字串
    (AB)+C
    ABC, ABABC, ABABABC, etc.
    Thu Jan 11 14:33:49 CST 2024 Untitled Document
    Exp Regular Expression: Examples
    Example (matches any input line)
    意義
    /^[0-9]+$/
    consists of only digits
    /^[0-9][0-9][0-9]$/
    exactly 3 digits
    /^(\+|-)?[0-9]+\.?[0-9]*$/
    decimal number with an optional sign and optional fraction
    /^[+-]?[0-9]+[.]?[0-9]*$/
    decimal number same as above
    /^[A-Za-z][A-Za-z0-9]*$/
    a letter followed by any letters or digits
    /^[A-Za-z]$|^[A-Za-z0-9]$/
    a letter or a letter followed by a digit
    /^[A-Za-z][0-9]?$/
    same as above

    /^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eE][+-]?[0-9]+)?$/
    floating point decimal number
    Thu Jan 11 14:33:49 CST 2024 Untitled Document
    Output
    print, printf
    Output Separators (OFS, ORS)
    Thu Jan 11 14:33:49 CST 2024 Untitled Document
    Output into Files
    Script ID    Script 意義
    awk-16-1
    $3 > 1{ print $1, $3 > "bigpop"}
    將第三欄之值大於1的record 之第一及第三欄印出於 bigpop 檔案中
    awk-16-2
    $3 <= 1{ print $1, $3 > "smallpop"}
    將第三欄之值不大於1的record 之第一及第三欄印出於 smallpop 檔案中
    awk-16-3
    {print($1,$3) > ($3>1? "bigpop" : "smallpop" )}
    awk-16-1 及 awk-16-2 之合併版本
    awk-16-4
    { print > $1 }
    將第一欄之值當作輸出之檔名,將整個record 印到該檔案
    awk-16-5
    { print $1, $2 > $3 }
    將第三欄之值當作輸出之檔名,將第一及第二欄印到該檔案
    awk-16-6
    { print $1, ( $2 > $3 ) }
    Thu Jan 11 14:33:49 CST 2024 Untitled Document
    Output to Pipes
     
    "print |" command
    # -----------------------------------------------
    # Script: awk-17:
    # print continents and population, sorted by population
    # ----------------------------------------------- 
    BEGIN { FS = "\t"; } { pop[$4] += $3; } END { for ( c in pop ) printf ("%15s %6d\n", c, pop[c]) | "sort +1rn"; close ( "sort +1rn"); }
    Thu Jan 11 14:33:50 CST 2024 Untitled Document
    Getline Function
       
    Fetch the next input record and performs the normal field-spliting operations on it.
       
    It sets NF, NR, and FNR.
       
    return 1 if there was a record presented, 0 otherwise.
       
    The expression 'getline x ' reads the next record into the variable x and increments NR and FNR. No spliting is done; NF is not set.
    --------------------
    getline < "file"
    --------------------
    getline x < "file"
    --------------------
       # include - replace #include "file" by its contents
       /^#include/{
          gsub(/"/,"",$2);
          while (getline x < $2 > 0 )
    	  print x;
    	  next;
    	  }
        {print}
    
    Pipe from other command
    while ("who" | getline )
    n++;
    "date" | getline d;
    while (getline < "file" > 0)
    Thu Jan 11 14:33:50 CST 2024 Untitled Document
    Variable
       
    Awk variables take on numeric of string values according to context.
    Command-Line Variables
       awk 'program' f1 f2 ...
       awk -f progfile f1 f2 ...
       awk -Fsep 'program' f1 f2 ...
       awk -Fsep -f progfile f1 f2 ...
    
       
    ARGV[0] is awk
       
    f1 f2 ... are considered ARGV[1], ARGV[2],....
       
    f1, f2, .. are file names except when it is in the form of var=text,
       
    var=text is considered variable initialization
    Thu Jan 11 14:33:50 CST 2024 Untitled Document
    Expression
       
    Same as C.
     
    Examples
       
     
       x = 1
       x = "string" 
       x = "3" + "4"   #(x = 7) 
    Thu Jan 11 14:33:50 CST 2024 Untitled Document
    The System Function
       
     
    $1 == "#include" 
          gsub(/"/,"",$2); system("cat " $2);next; }
          {print; }
    
    Thu Jan 11 14:33:51 CST 2024 Untitled Document
    Built-in String Functions
    Script ID    Script 意義
    awk-X-1
    {print length, $0;}
    印出各行(record)的長度及內容
    awk-X-2
    {print length($0), $0; }
    同上
    awk-X-3
    length < 10 || length > 20
    字串長度小於 10 或 大於20
    awk-X-4
    substr(s,m,n)
    從字串 s 的第m字元開始取n 個字元
    awk-X-5
    sprintf
    輸出指令
    awk-X-6
    {index("banana", "an")}
    return 2
    awk-X-7
    {$0 = "banana"; sub(/an/, "AN");}
    replace banana by bANana
    awk-X-8
    {$0 = "banana"; sub(/(an)+/, "AN");}
    replace banana by bANa
    awk-X-9
    {$0 = "banana"; gsub(/an/, "AN");}
    replace banana by bANANa
    Thu Jan 11 14:33:51 CST 2024 Untitled Document
    Scripts for NS2 Trace Data
     
    Trace output
    s -t 0.100000000 -Hs 0 -Hd -2 -Ni 0 -Nx 287.00 -Ny 471.00 -Nz 0.00 -Ne -1.000000 -Nl AGT -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -Is 0.0 -Id 5.0 -It tcp -Il 40 -If 0 -Ii 0 -Iv 32 -Pn tcp -Ps 0 -Pa 0 -Pf 0 -Po 0
    r -t 0.100000000 -Hs 0 -Hd -2 -Ni 0 -Nx 287.00 -Ny 471.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -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 0 -Pe 0->0 -Pw 0 -Pm 0 -Pc 0 -Pb 0->0
    f -t 0.160553892 -Hs 1 -Hd -1 -Ni 1 -Nx 487.00 -Ny 471.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md ffffffff -Ms 0 -Mt 800 -Is 0.255 -Id 5.255 -It DSR -Il 48 -If 0 -Ii 3 -Iv 32 -P dsr -Ph 2 -Pq 1 -Ps 2 -Pp 0 -Pn 2 -Pl 0 -Pe 0->0 -Pw 0 -Pm 0 -Pc 0 -Pb 0->0
    r -t 0.161654559 -Hs 0 -Hd -1 -Ni 0 -Nx 287.00 -Ny 471.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md ffffffff -Ms 1 -Mt 800 -Is 0.255 -Id 5.255 -It DSR -Il 48 -If 0 -Ii 3 -Iv 32 -P dsr -Ph 2 -Pq 1 -Ps 2 -Pp 0 -Pn 2 -Pl 0 -Pe 0->0 -Pw 0 -Pm 0 -Pc 0 -Pb 0->0
    r -t 0.161654559 -Hs 2 -Hd -1 -Ni 2 -Nx 687.00 -Ny 471.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md ffffffff -Ms 1 -Mt 800 -Is 0.255 -Id 5.255 -It DSR -Il 48 -If 0 -Ii 3 -Iv 32 -P dsr -Ph 2 -Pq 1 -Ps 2 -Pp 0 -Pn 2 -Pl 0 -Pe 0->0 -Pw 0 -Pm 0 -Pc 0 -Pb 0->0
    f -t 0.162774872 -Hs 2 -Hd -1 -Ni 2 -Nx 687.00 -Ny 471.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md ffffffff -Ms 1 -Mt 800 -Is 0.255 -Id 5.255 -It DSR -Il 68 -If 0 -Ii 3 -Iv 32 -P dsr -Ph 3 -Pq 1 -Ps 2 -Pp 0 -Pn 2 -Pl 0 -Pe 0->0 -Pw 0 -Pm 0 -Pc 0 -Pb 0->0
    r -t 0.163955538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ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md ffffffff -Ms 3 -Mt 800 -Is 0.255 -Id 5.255 -It DSR -Il 92 -If 0 -Ii 3 -Iv 32 -P dsr -Ph 4 -Pq 1 -Ps 2 -Pp 0 -Pn 2 -Pl 0 -Pe 0->0 -Pw 0 -Pm 0 -Pc 0 -Pb 0->0
    f -t 0.175468107 -Hs 4 -Hd -1 -Ni 4 -Nx 1087.00 -Ny 471.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md ffffffff -Ms 3 -Mt 800 -Is 0.255 -Id 5.255 -It DSR -Il 120 -If 0 -Ii 3 -Iv 32 -P dsr -Ph 5 -Pq 1 -Ps 2 -Pp 0 -Pn 2 -Pl 0 -Pe 0->0 -Pw 0 -Pm 0 -Pc 0 -Pb 0->0
    r -t 0.176944774 -Hs 3 -Hd -1 -Ni 3 -Nx 887.00 -Ny 471.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md ffffffff -Ms 4 -Mt 800 -Is 0.255 -Id 5.255 -It DSR -Il 120 -If 0 -Ii 3 -Iv 32 -P dsr -Ph 5 -Pq 1 -Ps 2 -Pp 0 -Pn 2 -Pl 0 -Pe 0->0 -Pw 0 -Pm 0 -Pc 0 -Pb 0->0
    r -t 0.176944774 -Hs 5 -Hd -1 -Ni 5 -Nx 1287.00 -Ny 471.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md ffffffff -Ms 4 -Mt 800 -Is 0.255 -Id 5.255 -It DSR -Il 120 -If 0 -Ii 3 -Iv 32 -P dsr -Ph 5 -Pq 1 -Ps 2 -Pp 0 -Pn 2 -Pl 0 -Pe 0->0 -Pw 0 -Pm 0 -Pc 0 -Pb 0->0
    s -t 0.183894147 -Hs 5 -Hd 4 -Ni 5 -Nx 1287.00 -Ny 471.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -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Pq 0 -Ps 2 -Pp 1 -Pn 2 -Pl 6 -Pe 0->5 -Pw 0 -Pm 0 -Pc 0 -Pb 0->0
    r -t 0.199261147 -Hs 2 -Hd 2 -Ni 2 -Nx 687.00 -Ny 471.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 13a -Md 2 -Ms 3 -Mt 800 -Is 5.255 -Id 0.255 -It DSR -Il 76 -If 0 -Ii 4 -Iv 252 -P dsr -Ph 6 -Pq 0 -Ps 2 -Pp 1 -Pn 2 -Pl 6 -Pe 0->5 -Pw 0 -Pm 0 -Pc 0 -Pb 0->0
    f -t 0.199261147 -Hs 2 -Hd 1 -Ni 2 -Nx 687.00 -Ny 471.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 13a -Md 2 -Ms 3 -Mt 800 -Is 5.255 -Id 0.255 -It DSR -Il 76 -If 0 -Ii 4 -Iv 251 -P dsr -Ph 6 -Pq 0 -Ps 2 -Pp 1 -Pn 2 -Pl 6 -Pe 0->5 -Pw 0 -Pm 0 -Pc 0 -Pb 0->0
    Thu Jan 11 14:33:51 CST 2024 Untitled Document
    Scripts for NS2 Trace Data - Avg. Packet Delay
     
    delay.awk
    BEGIN {
    	 highest_packet_id = -1;
      
             n=0;
              for (i in send) {
                     send[i] = 0;
                     printf("%d\n",send[i]);
             }
             for (i in recv) {
                     recv[i] = 0;
             }
             delay = avg_delay = 0;
     }
     
     {
             # Trace line format: normal
             if ($2 != "-t") {
                     event = $1;
                     time = $2;
                     if (event == "+" || event == "-") node_id = $3;
                     if (event == "r" || event == "d") node_id = $4;
                     flow_id = $8;
                     pkt_id = $12;
                     pkt_size = $6;
             }
             # Trace line format: new
             if ($2 == "-t") {
                     event = $1;
                     time = $3;
                     node_id = $5;
                     trace_name=$19;
                     source= $31;
                     destination= $33;
                     pkt_type=$35;
                     seq_num=$47;                    
                     flow_id = $39;
                     pkt_id = $41;
                     pkt_size = $37;
             }
     
     #記錄目前最高的packet ID
       if ( (trace_name == "AGT") && pkt_type=="tcp"&& flow_id == 0 && node_id == 0 && (event == "s") ){
       	if ((pkt_id > highest_packet_id)  ) {
       		
                    	highest_packet_id = pkt_id;
    		
              # Store packets send time
                     send[pkt_id] = time;
               
         	}
         }
             # Store packets arrival time
             if ((trace_name="AGT") && pkt_type=="tcp" && flow_id == 0  && event == "r") {
                     recv[pkt_id] = time;
             }
     }
     
     END {
             # Compute average delay
             for (i in recv) {
                     if (send[i] == 0) {
                             printf("\nError %g\n",i);
                     }
                     delay += recv[i] - send[i];
                     num ++;
             }
     
             printf("%10g ",flow);
             if (num != 0) {
                     avg_delay = delay / num;
             } else {
                     avg_delay = 0;
             }
             printf("Average delay=%10g\n",avg_delay*1000);
     }
    
    Thu Jan 11 14:33:51 CST 2024 Untitled Document
    Scripts for NS2 Trace Data - Avg. Throughput
     
    throughput.awk
    BEGIN {
    	n=0;
             recv = 0;
     }
     
     {
             # Trace line format: normal
             if ($2 != "-t") {
                     event = $1;
                     time = $2;
                     if (event == "+" || event == "-") node_id = $3;
                     if (event == "r" || event == "d") node_id = $4;
                     flow_id = $8;
                     pkt_id = $12;
                     pkt_size = $6;
             }
             # Trace line format: new
             if ($2 == "-t") {
                     event = $1;
                     time = $3;
                     node_id = $5;
                     trace_name=$19;
                     source= $31;
                     destination= $33;             
                     pkt_type=$35;
                     seq_num=$47;    
                     flow_id = $39;
                     pkt_id = $41;
                     pkt_size = $37;
             }
     
             # Calculate total received packets' size
             if ((flow_id == 0) && (event == "r")  &&($19 =="AGT") && pkt_type=="tcp") { 
             	
             	if (n == 0) start = $3;
       		recv += pkt_size * 8;
       		n +=1;
       		end = $3;
     	}
     }
     
     END {
     	throughput = recv / (end-start) / 1024;
     	print start, "\t", end, "\t",throughput, "Kbps";
     }
    
    Thu Jan 11 14:33:52 CST 2024