Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 12th, 2012  |  syntax: None  |  size: 2.19 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Unix Awk array not printing values
  2. processbody() {
  3. nawk '
  4. NR == FNR {
  5. split($0, x, "@")
  6. country_code[x[2]] = x[1]
  7. next
  8. system(" echo " I am here ">>/tmp/abc.txt")
  9. }
  10. {
  11. CITIZEN_COUNTRY_NAME = "INDIA"
  12. system(" echo " I am here 1">>/tmp/abc.txt")
  13. if (CITIZEN_COUNTRY_NAME in country_code) {
  14. value = country_code[CITIZEN_COUNTRY_NAME]
  15. system(" echo " I am here 2">>/tmp/abc.txt")
  16. } else {
  17. value = "null"
  18. system(" echo " I am here 3">>/tmp/abc.txt")
  19. }
  20. system(" echo " I am here 4">>/tmp/abc.txt")
  21. print "found " value " for country name " CITIZEN_COUNTRY_NAME  >> "/tmp/standalone.txt"
  22. } ' /tmp/country_codes.config
  23. echo "I am here 5" >> /tmp/abc.txt
  24. }
  25.  
  26. # Main program starts here
  27. echo "I am here 0" >> /tmp/abc.txt
  28. processbody
  29.        
  30. $ cat country_codes.config
  31. IND@INDIA
  32. IND@INDIB
  33. USA@USA
  34. CAN@CANADA
  35.        
  36. processbody()
  37. {  
  38.     awk '
  39.         {
  40.         split($0, x, "@")
  41.         country_code[x[2]] = x[1]
  42.         #next
  43.         }
  44.     END {
  45.         CITIZEN_COUNTRY_NAME = "INDIA"
  46.         if (CITIZEN_COUNTRY_NAME in country_code) {
  47.             value = country_code[CITIZEN_COUNTRY_NAME]
  48.         } else {
  49.             value = "null"
  50.         }
  51.         print "found " value " for country name " CITIZEN_COUNTRY_NAME
  52.     } ' /tmp/country_codes.config
  53. }  
  54.  
  55. # Main program starts here
  56. processbody
  57.        
  58. found IND for country name INDIA
  59.        
  60. processbody()
  61. {
  62.     awk -F@ '
  63.     { country_code[$2] = $1 }
  64.     END {
  65.         CITIZEN_COUNTRY_NAME = "INDIA"
  66.         if (CITIZEN_COUNTRY_NAME in country_code) {
  67.             value = country_code[CITIZEN_COUNTRY_NAME]
  68.         } else {
  69.             value = "null"
  70.         }
  71.         print "found " value " for country name " CITIZEN_COUNTRY_NAME
  72.     } ' /tmp/country_codes.config
  73. }
  74.  
  75. # Main program starts here
  76. processbody
  77.        
  78. found IND for country name INDIA
  79.        
  80. awk -F@ '/INDIA/ {print "found " $1 " for country name " $2 }' /tmp/country_codes.config
  81.        
  82. IND@INDIA
  83. IND@INDIB
  84. USA@USA
  85. CAN@CANADA
  86.        
  87. I am from INDIA
  88. I am from INDIB
  89. I am from CANADA
  90.        
  91. nawk '
  92.  
  93. BEGIN {
  94.         while (getline < "/tmp/country_codes.config")
  95.         {
  96.           split($0,x,"@")
  97.           country_code[x[2]] = x[1]
  98.         }
  99.       }
  100.  
  101.   { print $1,$2,$3,country_code[$4]}
  102.  
  103. ' /tmp/input_file
  104.        
  105. I am from IND
  106. I am from IND
  107. I am from CAN