首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >利用数学成绩使用awk命令查找记录

利用数学成绩使用awk命令查找记录
EN

Stack Overflow用户
提问于 2020-06-18 22:38:57
回答 2查看 152关注 0票数 1

编写unix命令,显示数学成绩超过80分的学生的所有字段,数学成绩在所有科目中得分最高,并且输出应按学生的std(标准)的升序进行。

投入:

代码语言:javascript
运行
AI代码解释
复制
roll,name,std,science_marks,math_marks,college
1,A,9,60,86,SM
2,B,10,85,80,DAV
3,C,10,95,92,DAV
4,D,9,75,92,DAV

产出:

代码语言:javascript
运行
AI代码解释
复制
1|A|9|60|86|SM
4|D|9|75|92|DAV

myCode:

代码语言:javascript
运行
AI代码解释
复制
awk 'BEGIN{FS=',' ; OFS="|"} {if($4<$5 && $5>80){print $1,$2,$3,$4,$5,$6}}'

但是我收到了意想不到的象征性错误,请帮帮我。

代码语言:javascript
运行
AI代码解释
复制
    Error Message on my Mac System Terminal:
awk: syntax error at source line 1
     context is
        BEGIN >>>  {FS=, <<< 
    awk: illegal statement at source line 1
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-18 22:53:15

请您试着用GNU awk中显示的示例进行以下、编写和测试。这个答案并不硬编码它收集的字段号,列中有数学,然后相应地检查其余的行。

代码语言:javascript
运行
AI代码解释
复制
awk '
BEGIN{
  FS=","
  OFS="|"
}
FNR==1{
  for(i=1;i<=NF;i++){
    if($i=="math_marks"){ field=i }
  }
  next
}
{
  for(i=3;i<=(NF-1);i++){
    max=(max>$i?(max?max:$i):$i)
  }
  if(max==$field && $field>80){ $1=$1; print }
  max=""
}
'  Input_file

解释:添加了上面的详细说明。

代码语言:javascript
运行
AI代码解释
复制
awk '                                               ##Starting awk program from here.
BEGIN{                                              ##Starting BEGIN section of code here.
  FS=","                                            ##Setting field separator as comma here.
  OFS="|"                                           ##Setting output field separator as | here for all lines.
}
FNR==1{                                             ##Checking condition if its first line then do following.
  for(i=1;i<=NF;i++){                               ##Going through all fields here.
    if($i=="math_marks"){ field=i }                 ##Checking if a field value is math_marks then set field to tht field numner here.
  }
  next                                              ##next will skip all further statements from here.
}
{
  for(i=3;i<=(NF-1);i++){                           ##Going through from 3rd field to 2nd last field here.
    max=(max>$i?(max?max:$i):$i)                    ##Creating max variable which checks its value with current field and sets maximum value by comparison here.
  }
  if(max==$field && $field>80){ $1=$1; print }      ##After processing of all fields checking if maximum and field value is equal AND math number field is greater than 80 then print the line.
  max=""                                            ##Nullifying max var here.
}
'  Input_file                                       ##Mentioning Input_file name here.
票数 2
EN

Stack Overflow用户

发布于 2020-06-19 00:00:26

您的代码有错误编码的双引号:

代码语言:javascript
运行
AI代码解释
复制
                        here
                        | |
                        v v
$ busybox awk 'BEGIN{FS=”,” ; OFS="|"} {if($4<$5 && $5>80){print $1,$2,$3,$4,$5,$6}}'
awk: cmd. line:1: Unexpected token

替换这些代码,您的代码就能正常工作。

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

https://stackoverflow.com/questions/62464640

复制
相关文章

相似问题

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