LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-21-2003, 12:23 PM   #1
yasir15
LQ Newbie
 
Registered: Aug 2003
Posts: 8

Rep: Reputation: 0
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
 
Old 08-21-2003, 12:39 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
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.
 
Old 08-21-2003, 12:41 PM   #3
abd_bela
Member
 
Registered: Dec 2002
Location: algeria
Distribution: redhat 7.3, debian lenny
Posts: 598

Rep: Reputation: 31
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
 
Old 08-21-2003, 01:01 PM   #4
yasir15
LQ Newbie
 
Registered: Aug 2003
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 08-21-2003, 02:37 PM   #5
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
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
 
Old 08-22-2003, 12:17 PM   #6
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Shell] Read a File from script yussef Programming 4 08-19-2008 04:26 AM
Parsing a File in a Bash Script TGWDNGHN Programming 4 12-02-2005 02:38 PM
(shell script) string parsing kuru Programming 4 09-12-2005 07:59 PM
Script File: Parsing command sent to shell cheema Programming 1 07-01-2005 12:54 PM
Search for a file using shell script sharathkv Linux - Newbie 2 08-06-2003 05:05 AM


All times are GMT -5. The time now is 05:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration