跳过输入参数的第一行

问题描述

read                          #reads input 
awk 'BEGIN {FS=";"}           #file seperator=;
{
    c=0;
    if($2=="programmer"||$2=="Programmer"||$2="PROGRAMMER")   #checks if the person is a programmer
    {
        print $1","$2","$3","$3*12;
        c=c+1;
    }
}
END {
    if(c==0)
    {
        print "no programmer";
    }
}'

当我执行此脚本时,它总是跳过读取输入参数的第一行并继续读取第二行。

解决方法

您没有提到任何包含用户在读取命令中输入的值的变量,提及一个变量并将其传递给 awk 命令,然后它应该会飞。

read var                         #reads input

echo "$var" |
awk 'BEGIN {FS=";"}              #file separator=;
{
    c=0;
    if($2=="programmer"||$2=="Programmer"||$2=="PROGRAMMER")   #checks if the person is a programmer
    {
        print $1","$2","$3","$3*12;
        c=c+1;
    }
}
END {
    if(c==0)
    {
        print "no programmer";
    }
}'

此外(Shawn 在问题部分的评论中提到的内容),您需要将 = 更改为 == 以使其在您的 $2=="PROGRAMMER" 部分正确。

如果用户可以输入超过提到的这 3 种类型(程序员的值),您实际上可以这样做,在上面的代码中将您的 if 条件更改为 if(tolower($2)=="programmer")

,

第一组语句对每一行输入执行,这意味着在每一输入行中,变量c将被重置为零。计算 END 子句时,c 包含最后一行的值。

我会将初始化 c=0 移到 BEGIN 子句中。