title: Extract last and second last strings of a file in shell variables
body: <p>Although it is looking similar to my previous post but here purpose is different.</p>
  
  <pre><code>udit@udit-Dabba ~/ah $ cat decrypt.txt
  60 00 00 00 00 17 3a 20  00 00 00 00 00 00 00 00
  00 00 00 00 00 00 00 01  00 00 00 00 00 00 00 00
  00 00 00 00 00 00 00 02  *00 00 e0 f9 6a 61 61 6e
  65 6b 61 68 61 6e 67 61  79 65 77 6f 64 69 6e* 00
  00 00 03 29 
  </code></pre>
  
  <p>I want to extract last string of the file (here it is <code>29</code>) in a shell varaible</p>
  
  <p>I tried this ...</p>
  
  <pre><code>size=`wc -w encrypt.txt`
  
  awk -v size=$size 'BEGIN {RS=" ";ORS=" ";}' {if (NR&gt;size-1 &amp;&amp;
                                           NR &lt; size+1)print $0}' decrypt.txt
  </code></pre>
  
  <p><strong>Output :</strong>
       29</p>
  
  <p>But when I changed the file slightly ..</p>
  
  <pre><code>  udit@udit-Dabba ~/ah $ cat decrypt.txt
    60 00 00 00 00 17 3a 20  00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 01  00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 02  *00 00 e0 f9 6a 61 61 6e
    65 6b 61 68 61 6e 67 61  79 65 77 6f 64 69 6e* 00
    65 6b 61 68 61 6e 67 61  00 00 03 29 
  </code></pre>
  
  <p><strong>Output :</strong>
        03</p>
  
  <p>Why there is discrepency between the results ??</p>
  
  <p>I am new to awk and shell features so I am not sure whether it is a right way to do so or not ???</p>
  
  <p>I think there should be some variation of <code>grep,sed,awk</code> or any other linux command which may solve my problem but I am not aware of it.</p>
  
  <p>Please guide me for this.</p>
  
  <p>Thanx in advance.</p>
  
  <p><strong>Purpose :</strong> </p>
  
  <p>Make two variables in a shell script which should store last and second last strings of an input file.</p>
  
  <p><strong>Limitation :</strong></p>
  
  <p>Every input file contains a blank line at the end of file.</p>
  
  <p>(Like in above mentioned file , after the file contents there would be one more blank line just like hitting <code>ENTER</code> key and that can not be changed because it is being generated through a C program at run time.)</p>