首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >awk命令行-删除行

awk命令行-删除行
EN

Unix & Linux用户
提问于 2021-11-02 20:54:11
回答 1查看 224关注 0票数 -1

根据请求,投递代码分开。

电流输出

代码语言:javascript
复制
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

我应该实现的输出

代码语言:javascript
复制
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

基本上,它是从输入文件中添加行,而不是删除标头。我的问题是移除它

代码语言:javascript
复制
  #!/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
EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2021-11-02 21:17:16

在shell脚本中,$1引用了调用脚本的第一个位置参数(或参数)--您可能习惯于使用该参数将文件名传递给一次性丢弃awk计划

然而,在可执行的awk程序内部,命令行参数是通过awk自己的ARGV数组在内部处理的,$1是当前记录的第一个字段。在代码块之外,它等价于

代码语言:javascript
复制
$1 != "" {
    print
}

因此,它输出至少包含一个非FS字符的每一行输入。

所以去掉多余的$1

票数 2
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/675907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档