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

シェルスクリプト内では、$1スクリプトが呼び出される最初の位置パラメータ(または引数)を指します。おそらく、ファイル名を渡すためにそれを使用することに慣れているでしょう。使い捨て awk プログラム

内部実行可能なawkプログラムただし、コマンドライン引数はawk自身の配列を介して内部的に処理されARGV$1現在のレコードの最初のフィールドになります。コードブロックの外では、これは以下と同等です。

$1 != "" {
    print
}

したがって、少なくとも 1 つの非FS文字を含む入力行がすべて出力されます。

したがって、余分なものを削除します$1

関連情報