sed && awk lookup 解答

Posted uxiuxi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sed && awk lookup 解答相关的知识,希望对你有一定的参考价值。

[email protected]:/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 解答的主要内容,如果未能解决你的问题,请参考以下文章

awk&sed 小实例

Sed&awk笔记之awk

sed & awk 概述

sed && awk工具 及一些经常使用的shell脚本

L-1-23 awk&sed

AWK&SED