首页 > 代码库 > sed && awk lookup 解答
sed && awk lookup 解答
root@ubuntu:/home/koyaku# vim lookup 2 3 #0 4 BEGIN { FS = " "; OFS = " " 5 #prompt user 6 printf("Enter a glossary term:") 7 } 8 9 #1 read local file named glossary 2 3 #0 4 BEGIN { FS = " "; OFS = " " 5 #prompt user 6 printf("Enter a glossary term:") 7 } 8 9 #1 read local file named glossary 10 FILENAME == "glossary" { 11 #load each glossary entry into an array 12 entry[$1]=$2 13 next 14 } 15 16 #2 scan for command to exit program 17 $0 ~ /^(quit|[qQ]|exit|[xX])$/{ exit } 18 19 #3 process any non-empty line 20 $0 != ""{ 21 printf "in this roud" 22 print entry[1] 23 if ( $0 in entry ){ 24 # it is there,print definition 25 printf "in the entry" 26 print entry[$0] 27 }else 28 print $0 "not found" 29 } 30 31 #4 prompt user again for another term 32 { 33 printf("Enter another glossary term (q to quit):") 34 }‘ glossary -
手动敲上面代码遇到几个问题和疑惑
1.按照书上给的是“\t” 实际敲的是“ ”,所有导致得不到正确的结果。第十行的意思是在当前目录中找到文件名为glossary的文件,并且由“ ”(空格分隔),将后面的赋值给前面比如entry[BASIC] =Beginners entry[CICS]= Customers 等等
2.第二十到二十三行。就是你输入的值为$0。检查这个值是否在entry数组中,即BASIC\ COBOL\CICS等。如果在就打印entry对应的值,没有就输出notfound
3.glossary - 是什么意思?
sed && awk lookup 解答
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。