LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   File Parsing using a Shell Script (https://www.linuxquestions.org/questions/programming-9/file-parsing-using-a-shell-script-84225/)

yasir15 08-21-2003 12:23 PM

File Parsing using a Shell Script
 
Hello:

I am very new to shell programming and in need of some help. I have a file sc.cfg that contains some ip addresses as ASCIP.

sc.cfg
-------------------------------------------
ASCIP=3.87.87.3/255.255.252.0
ASCIP=3.87.86.41/255.255.252.0
ASCIP=3.87.87.45/255.255.252.0

-------------------------------------------

All I need is a shell script that opens this file (sc.cfg) takes out the 3 ip addresses and stores it in etc/hosts...

Please help.

Thank You & Best Regards

david_ross 08-21-2003 12:39 PM

How about:
grep "ASCIP" sc.cfg | cut -d"=" -f2 | cut -d"/" -f1 >> /etc/hosts

The only problem is that you don't have any hostnames so you will need to add them manually.

abd_bela 08-21-2003 12:41 PM

hi,
you can use for example awk like this:

awk -F"=" {'print $2'} sc.cfg | awk -F"/" {'print $1'} > /etc/hostfile.txt

sc.cfg is your enter file , the /etc/hostfile.txt your output file
-F"=" option to separte with the symbole =, the second separator is "/", you print the field $1 and in the second the second field

best regards
bela

yasir15 08-21-2003 01:01 PM

All- Thanks for replying.

Now, if I want to append a variable Name at the start of each ip, how can I go about implementing that ?

For example:

Var1=3.87.88.3
Var2=3.87.88.41
Var3=3.87.88.42

david_ross 08-21-2003 02:37 PM

Sorry for taking so long (bit busy now). Try:
Code:

#!/bin/bash

IFS="
"
num=0
for IP in `grep "ASCIP" sc.cfg | cut -d"=" -f2 | cut -d"/" -f1`;do
echo Var$num=$IP
num=$(($num+1))
done


david_ross 08-22-2003 12:17 PM

If reply to your e-mail:
Quote:

Hello David:

Thank you so much for helping me out.

Yet another problem if you can help.

When I try to add a variable name at the end of the IP string, it does not show the out correctly. so basically if i need something like

3.80.88.3 VARNAME
3.80.88.4 VARNAME
3.80.88.5 VARNAME

Can I somehow get an output of this form ?

Thanks again.
All you need to do is change the echo statement:
Code:

#!/bin/bash

IFS="
"
num=0
for IP in `grep "ASCIP" sc.cfg | cut -d"=" -f2 | cut -d"/" -f1`;do
#!/bin/bash

IFS="
"
num=0
for IP in `grep "ASCIP" sc.cfg | cut -d"=" -f2 | cut -d"/" -f1`;do
echo "$IP        VAR$num"
num=$(($num+1))
done

PS. Please don't use e-mail like that. I'll get a message when you respond to a post - I don't need 2.


All times are GMT -5. The time now is 08:58 AM.