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

相关内容