<p>I'm trying to get the address from the below result of my <code>nslookup</code></p>
<p>Here I want to get is the 2nd address which is <code>10.0.45.45</code></p>
<pre class="lang-sh prettyprint-override"><code>Server:         10.152.183.10
Address:        10.152.183.10#53

Name:   pg-master-0.pg-master-headless.postgres.svc.cluster.local
Address: 10.1.45.45
</code></pre>
<p>Here is my code</p>
<pre class="lang-sh prettyprint-override"><code>MASTER_HOST=$(nslookup pg-master-0.pg-master-headlesss | awk '/^Address:/ {A=$2}; END {print A}');

echo $MASTER_HOST
</code></pre>
<p>Unfortunately, my output is:</p>
<pre class="lang-sh prettyprint-override"><code>10.152.183.10#53
</code></pre>
<p>Here I'm logged into the pod
<a href="https://i.stack.imgur.com/CSGsk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CSGsk.png" alt="enter image description here" /></a>
then ran the nslookup that way.</p>

Jayson Gonzaga
<p>If you want the 2nd &quot;Address:&quot; from the <code>nslookup</code> output, you can simply do:</p>
<pre><code>awk '/^Address/{n++; if (n==2){print $2; exit}}'
</code></pre>
<p>Which checks if the line begins with <code>Address</code>, then increments a counter <code>n++</code> and when <code>n == 2</code> it outputs the second field and exits.</p>
<p><strong>Example Use/Output</strong></p>
<p>With your data in the file called <em>nslookup.txt</em>, you would receive the following:</p>
<pre><code>$ awk '/^Address/{n++; if (n==2){print $2; exit}}' nslookup.txt
10.1.45.45
</code></pre>
<p>Of course, using <code>nslookup</code> you would just pipe the output to <code>awk</code>. For example, if I wanted the IP of the machine <em>valkyrie</em> on my local subnet, I would use:</p>
<pre><code>$ nslookup valkyrie | awk '/^Address/{n++; if (n==2){print $2; exit}}'
192.168.6.135
</code></pre>
<p>Look things over and let me know if you have further questions.</p>
