awk 命令列 - 刪除行

awk 命令列 - 刪除行

根據請求單獨發布代碼。

電流輸出

Name,Final Grade,Section
Andrew is an online student
Andrew,95,online
Brandon is an online student
Brandon,100,online
Chelsey is an onsite student
Chelsey,100,onsite
Deborah is an online student
Deborah,72,online
Erik is an online student
Erik,65,online
Arielle is an onsite student
Arielle,88,onsite
Shaun is an onsite student
Shaun,91,onsite
Ninette is an online student
Ninette,82,online
Nguyen is an onsite student
Nguyen,80,onsite

我應該達到的輸出

Andrew is an online student
Brandon is an online student
Chelsey is an onsite student
Deborah is an online student
Erik is an online student
Arielle is an onsite student
Shaun is an onsite student
Ninette is an online student
Nguyen is an onsite student

基本上,它是從輸入檔案中添加行而不是刪除標題。我的問題是刪除它

  #!/usr/bin/awk -f
    ##comment create awk script that will output the given data in the format given in word document
    ##comment specify the delimiter as ","
    BEGIN { FS = "," }
    
    /./ {
    ##comment check if the third field is online, if print online
    if ($3 == "online")
    printf("%s is an online student\n", $1)
    
    ##commentcheck if the third field is onsite, if print onsite
    if ($3 == "onsite")
    printf("%s is an onsite student\n", $1)
    } $1

答案1

在 shell 腳本中,$1指的是呼叫腳本時使用的第一個位置參數(或自變數)—您可能習慣使用它來將檔案名稱傳遞給一次性 awk 程序

裡面一個可執行 awk 程序然而,命令列參數是透過 awk 自己的ARGV數組在內部處理的,並且$1是當前記錄的第一個欄位。在程式碼區塊之外它相當於

$1 != "" {
    print
}

因此,它輸出包含至少一個非FS字元的每一行輸入。

所以去掉多餘的$1

相關內容