Ubuntu中的Awk编程

Ubuntu中的Awk编程,ubuntu,awk,Ubuntu,Awk,如何在同一个awk文件中打印整个记录并请求输入(getline) 这是awk文件的代码。它不打印输入文件中的症状列表,只在我运行它时询问问题 #!/bin/awk -f BEGIN{ print $0 printf"Enter the symptoms you are experiencing according to the numbering given in the list\n getline symptom<"-" } $1==symptom{ print $2 } 假设您

如何在同一个awk文件中打印整个记录并请求输入(getline)

这是awk文件的代码。它不打印输入文件中的症状列表,只在我运行它时询问问题

#!/bin/awk -f
BEGIN{
print $0
printf"Enter the symptoms you are experiencing according to the numbering given in the list\n  
getline symptom<"-"
}
$1==symptom{
print $2
}

假设您的数据文件名为
data
,如下所示:

1 Chestpainortightne
2 HeadacheandFacialpain
3 Chillswithsweating
4 Painondeepbreathing
5 Coughwithsputum
6 Triggeredbyallergens
7 Drycough
8 Triggeredbysmoking
9 Fever
10 Wheeze
然后您应该能够使用它,它将
-
指定为两个输入文件之一,以便脚本在处理
数据
文件后从标准输入读取

awk 'FNR == NR { symptom[$1] = $2; print $0; next }
    #FNR == 1  { print "Enter your symptoms by number, one per line" }
               { print $0 " ==> " symptom[$1] }' data -
如果未注释,则提示出现在用户输入的第一行之后,而不是之前,这是一个麻烦。使用GNU Awk,您可以使用:

gawk 'FNR == NR { symptom[$1] = $2; print $0; next }
      BEGINFILE { print "Enter your symptoms by number, one per line" }
                { print $0 " ==> " symptom[$1] }' data -
BEGINFILE
模式仅在第二个文件的开头调用,
-

$cat文件
$ cat file
1 Chestpainortightne    6 Triggeredbyallergens
2 HeadacheandFacialpain 7 Drycough
3 Chillswithsweating    8 Triggeredbysmoking
4 Painondeepbreathing   9 Fever
5 Coughwithsputum       10 Wheeze

$ cat tst.awk
BEGIN {
    db = ARGV[1]
    delete ARGV[1]
    ARGC--
    while ( (getline line < db) > 0 ) {
        print line
        split(line,flds)
        for (i=1;i in flds;i+=2) {
            n2v[flds[i]] = flds[i+1]
        }
    }
    close(db)
    printf "Pick a number: "
}
{
    printf "You selected %d (%s)\n", $0, n2v[$0]
    exit
}

$ awk -f tst.awk file
1 Chestpainortightne    6 Triggeredbyallergens
2 HeadacheandFacialpain 7 Drycough
3 Chillswithsweating    8 Triggeredbysmoking
4 Painondeepbreathing   9 Fever
5 Coughwithsputum       10 Wheeze
Pick a number: 2
You selected 2 (HeadacheandFacialpain)
1切斯帕诺提尼6触发亚勒根斯 2头痛和面部疼痛7干咳 3寒战出汗8触发电击 4呼吸痛9发烧 咳嗽5次,咳嗽10次 $cat tst.awk 开始{ db=ARGV[1] 删除ARGV[1] ARGC-- 而((getline0){ 打印行 拆分(直线,FLD) 对于(i=1;在FLD中为i;i+=2){ n2v[flds[i]]=flds[i+1] } } 关闭(db) printf“选择一个数字:” } { printf“您选择了%d(%s)\n”,$0,n2v[$0] 出口 } $awk-f tst.awk文件 1切斯帕诺提尼6触发亚勒根斯 2头痛和面部疼痛7干咳 3寒战出汗8触发电击 4呼吸痛9发烧 咳嗽5次,咳嗽10次 选择一个数字:2 您选择了2(头痛和面部疼痛)

在开始处理之前,从BEGIN部分的数据库中读取数据是非常罕见的情况之一,在这种情况下,可以适当地使用getline,但请确保在使用getline之前阅读并完全理解所有的注意事项。

AFAICR,BEGIN块中没有
$0
;它在读取第一行之前执行。您在
printf
行末尾也缺少一个结束引号。这将有助于查看命令行调用以及您希望提供的输入。您确定需要
getline
FNR==NR
技术允许您处理输入文件,然后处理其他数据-如果您将文件名
-
指定为
awk
,则这些数据可能来自标准输入aka键入。输入是否如图所示,或者输入是否转换为2组2列?Getline不重要。我尝试按您的方式运行它,但它只打印症状列表。它不会向用户询问问题。我该怎么办?请用更简单的术语解释一下,因为我使用ubuntu才4个月,我们学习了非常基本的AWK编程才一个月,所以我不能理解太多?n我们还没有在awk编程中使用数组。请阅读Arnold Robbins的《有效的awk编程》第四版。如果你有任何其他的“awk书”,扔掉它,因为它已经过时了。
$ cat file
1 Chestpainortightne    6 Triggeredbyallergens
2 HeadacheandFacialpain 7 Drycough
3 Chillswithsweating    8 Triggeredbysmoking
4 Painondeepbreathing   9 Fever
5 Coughwithsputum       10 Wheeze

$ cat tst.awk
BEGIN {
    db = ARGV[1]
    delete ARGV[1]
    ARGC--
    while ( (getline line < db) > 0 ) {
        print line
        split(line,flds)
        for (i=1;i in flds;i+=2) {
            n2v[flds[i]] = flds[i+1]
        }
    }
    close(db)
    printf "Pick a number: "
}
{
    printf "You selected %d (%s)\n", $0, n2v[$0]
    exit
}

$ awk -f tst.awk file
1 Chestpainortightne    6 Triggeredbyallergens
2 HeadacheandFacialpain 7 Drycough
3 Chillswithsweating    8 Triggeredbysmoking
4 Painondeepbreathing   9 Fever
5 Coughwithsputum       10 Wheeze
Pick a number: 2
You selected 2 (HeadacheandFacialpain)