Unix的awk的数组不打印值 [英] Unix Awk array not printing values

查看:188
本文介绍了Unix的awk的数组不打印值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是确切的code,我在我的系统运行与 SH lookup.sh 。我看不出在 NAWK 的任何细节块打印或写入文件的abc.txt 。只有我在这里0 我在这里1 打印。即使是的printf NAWK 不工作。请帮助。

  processbody(){
NAWK
NR == FNR {
拆分($ 0,X,@)
country_ code [X [2] = X [1]
下一个
系统(回声我在这里>> /tmp/abc.txt)
}
{
CITIZEN_COUNTRY_NAME =印
系统(回声我在这里1>> /tmp/abc.txt)
如果(在country_ code CITIZEN_COUNTRY_NAME){
值= country_ code [CITIZEN_COUNTRY_NAME]
系统(回声我在这里2>> /tmp/abc.txt)
}其他{
值=空
系统(回声我在这里3>> /tmp/abc.txt)
}
系统(回声我在这里4>> /tmp/abc.txt)
打印发现价值为国名CITIZEN_COUNTRY_NAME>> /tmp/standalone.txt
}'/tmp/country_$c$cs.config
回声我在这里5>> /tmp/abc.txt
}#主程序从这里开始
回声我在这里0>> /tmp/abc.txt
processbody

和我的 country_ codes.config 文件:

  $猫country_ codes.config
IND印度@
IND @ INDIB
美国美国@
CAN @加拿大


解决方案

这是一些pretty有趣 AWK code。问题是,你的首要条件,在 NR == FNR 之一,是从第一个文件中读取每个记录有效时 - country_ codes.config文件,但在处理动作包含接下来打完读取记录,并分割它,保存它,它会并读取下一个记录 - 不执行<$ C $的第二块C> AWK 脚本。最后,它完成 - 无事可做,所以它永远不会打印任何东西。

这工作三立:

  processbody()
{
    AWK
        {
        拆分($ 0,X,@)
        country_ code [X [2] = X [1]
        #下一个
        }
    结束 {
        CITIZEN_COUNTRY_NAME =印
        如果(在country_ code CITIZEN_COUNTRY_NAME){
            值= country_ code [CITIZEN_COUNTRY_NAME]
        }其他{
            值=空
        }
        打印发现价值为国名CITIZEN_COUNTRY_NAME
    }'/tmp/country_$c$cs.config
}#主程序从这里开始
processbody

它产生的输出:

 为国名印度发现IND

由于海武笔记,你可以使用 AWK 的内在纪录分割设施,以简化生活:

  processbody()
{
    awk的-F @
    {country_ code [$ 2] = $ 1}
    结束 {
        CITIZEN_COUNTRY_NAME =印
        如果(在country_ code CITIZEN_COUNTRY_NAME){
            值= country_ code [CITIZEN_COUNTRY_NAME]
        }其他{
            值=空
        }
        打印发现价值为国名CITIZEN_COUNTRY_NAME
    }'/tmp/country_$c$cs.config
}#主程序从这里开始
processbody

This is the exact code I am running in my system with sh lookup.sh. I don't see any details within nawk block printed or written to the file abc.txt. Only I am here 0 and I am here 1 are printed. Even the printf in nawk is not working. Please help.

processbody() {
nawk '
NR == FNR {
split($0, x, "@")
country_code[x[2]] = x[1]
next
system(" echo " I am here ">>/tmp/abc.txt") 
}
{
CITIZEN_COUNTRY_NAME = "INDIA"
system(" echo " I am here 1">>/tmp/abc.txt") 
if (CITIZEN_COUNTRY_NAME in country_code) {
value = country_code[CITIZEN_COUNTRY_NAME]
system(" echo " I am here 2">>/tmp/abc.txt") 
} else {
value = "null"
system(" echo " I am here 3">>/tmp/abc.txt") 
}
system(" echo " I am here 4">>/tmp/abc.txt") 
print "found " value " for country name " CITIZEN_COUNTRY_NAME  >> "/tmp/standalone.txt"
} ' /tmp/country_codes.config
echo "I am here 5" >> /tmp/abc.txt
}

# Main program starts here
echo "I am here 0" >> /tmp/abc.txt
processbody 

And my country_codes.config file:

$ cat country_codes.config
IND@INDIA
IND@INDIB
USA@USA
CAN@CANADA

解决方案

That's some pretty interesting awk code. The problem is that your first condition, the NR == FNR one, is active for each record read from the first file - the country_codes.config file, but the processing action contains next so after it reads a record and splits it and saves it, it goes and reads the next record - not executing the second block of the awk script. At the end, it is done - nothing more to do, so it never prints anything.

This works sanely:

processbody() 
{   
    awk '
        {
        split($0, x, "@")
        country_code[x[2]] = x[1]
        #next
        }
    END {
        CITIZEN_COUNTRY_NAME = "INDIA"
        if (CITIZEN_COUNTRY_NAME in country_code) {
            value = country_code[CITIZEN_COUNTRY_NAME]
        } else {
            value = "null"
        }
        print "found " value " for country name " CITIZEN_COUNTRY_NAME
    } ' /tmp/country_codes.config
}   

# Main program starts here
processbody

It produces the output:

found IND for country name INDIA

As Hai Vu notes, you can use awk's intrinsic record splitting facilities to simplify life:

processbody()
{
    awk -F@ '
    { country_code[$2] = $1 }
    END {
        CITIZEN_COUNTRY_NAME = "INDIA"
        if (CITIZEN_COUNTRY_NAME in country_code) {
            value = country_code[CITIZEN_COUNTRY_NAME]
        } else {
            value = "null"
        }
        print "found " value " for country name " CITIZEN_COUNTRY_NAME
    } ' /tmp/country_codes.config
}

# Main program starts here
processbody

这篇关于Unix的awk的数组不打印值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆