I am trying to get two input from a file .

!/bin/bash

echo "enter clientname"
read clientname

echo print|nsradmin -s $clientname -p390113|egrep 'NetWorker version|IP address'>> output_$clientname

the content of the file would be :

$ cat test_new1

             NetWorker version: 7.6.3.2.Build.860;


              IP address: xxx.xxx.xxx.xx;

I want to grab "IP address:" to provide input to my rest of the script

reasult2=$(nslookup $ipaddress)

This post is really old, but here is my answer in case someone else comes looking. There are several ways you can trim it down further. I can show you one using bash/sh, grep, and sed.

# This says remove all spaces from the input
sed "s/[ ]//g" 

# This says remove 'ipaddress:' and ';'.
sed "s/ipaddress:\|;//gi"

So now all you have to do is get it down to just the IP Address line.

iprawline="$(grep "IP" test_new1)"

And then you can filter that single line:

echo $iprawline | sed "s/[ ]//g" | sed "s/ipaddress:\|;//gi"

But you want it stored in a variable so:

ipaddress="$(echo $iprawline | sed "s/[ ]//g" | sed "s/ipaddress:\|;//gi")"

And to do the whole process all in one line:

ipaddress="$(grep "IP" test_new1 | sed "s/[ ]//g" | sed "s/ipaddress:\|;//gi")"

And now echo $ipadress says: xxx.xxx.xxx.xx

These are Regular Exressions, in case you didn't know. They can be very handy when not abused (and they are often abused). The problem with RegEx is that it can be very unfriendly for humans (unreadable). But still, a good skill to have when processing text. sed accepts RegEx as input and will do substitutions for you. The script will work provided the format of the test_new1 file doesn't change.

Honestly, while bash is totally capable of handling this I tend to lean towards Python these days. Especially if the process gets any more complicated than this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.