Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strip lines of file as Variable 1

Status
Not open for further replies.

TamedTech

IS-IT--Management
May 3, 2005
998
GB
Hello guys,

I've got a file that I need to strip the lines out of and then create each line as a seperate variable.

I'm looking for your opinions on the best way to achieve this.

So i have a file that perhaps looks as follows.

Code:
00:09:2D:31:69:4C-2
00:0E:6D:04:6D:06-9
00:0E:6D:E4:62:03-9

I would like to grab each line as a seperate variable such as $a, $b, $c or somthing along those lines.

Whats the best way on achieving this?

I'm quite happy to limit the script to only account for the top 10 lines of code if that makes it any easier to develop a solution.

I'm thinking something along the lines of SED, but i'm not that great with the syntax yet.

I'm using Bash Shell, Thanks for your thoughts guys,

Rob
 
A starting point:
for mac in $(</path/to/file)
do echo "mac='$mac'"
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for the concept PH,

It doesn't quite fit my requirements really, as I plan on binding the address's to defferent ports, so the first line to port 1, second line to port 2, third to port 3 etc etc ... you get the picture.

So simply looping the commands doesnt quite work, if i was binding them all to the same port it wouldnt be an issues, but as they are going to seperate ports i really need to keep them as seperate variables.

Any other ideas on how i could tackle this?

Thanks,

Rob
 
If you don't state all of your requirements, you won't get an answer that fits them. :)

What are the commands to bind them to ports? Where does the list of ports come from? If you have that in a file as well, then the loop may well work... maybe two loops reading into an array.

Annihilannic.
 
Thanks Annihilannic,

The ports are not in a text file, they would just be typed in by hand.

So if I were to hand code the commands to bind those macs above it would look somthing like this

Code:
rfcomm -i bind rfcomm0 00:0E:6D:04:6D:06 9
rfcomm -i bind rfcomm1 00:09:2D:31:69:4C 2
rfcomm -i bind rfcomm2 00:0E:6D:E4:62:03-9

Is it possible to have the loop incredment the number after the rfcomm for each time it loops the process?

That would work a charm, then for every line of the macs list it would run the bind statement and increment the rfcomm port number.

Appologies for my niavity but i've only been working with this stuff for a couple of weeks.

Thanks,

Rob
 
something like this ?
port=0
for mac in $(</path/to/file)
do echo "rfcomm -i bind rfcomm$port $mac"; ((port+=1))
done > /path/to/ouput

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Ahh ... PH, thats an excelent snippet, I've been playing with the LET command but was struggling a little.

Ok, so that works, now onto another Issue i have noticed with the solution.

Basicly the bind statement wont work with the '-' that seperates the mac address and the channel in my files.

So, its very easy for me to output to my mac list without the '-' in between them, but that throws the loop out of sync as it now loops through 6 times making $mac the mac then the channel then the mac then the channel and so on...

So, is there a way of changing the delimiter on the loop so that it treats the whole line with a ' ' instead of the '-' as $mac?

Thanks for any more help you can pass my way.

Rob
 
port=0
IFS="-$IFS" while read mac chan
do echo "rfcomm -i bind rfcomm$port $mac $chan"; ((port+=1))
done < /path/to/file > /path/to/ouput

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks again PH.

I had managed to cobble together using my own version which used the SED inside the loop to switch the '-' for a ' ' but it was a little on the slow side.

So i've tried your code and i get the following error:

Code:
bash: while: command not found
bash: /hj/bluepod/getmaclist2: line 5: syntax error near unexpected token `do'
bash: /hj/bluepod/getmaclist2: line 5: `do echo "rfcomm -i hci0 bind /dev/rfcomm$port $mac $chan"'

Is it unusual that it cant find the while command?

Thanks,

Rob
 
And this ?
port=0: oldIFS="$IFS"
export IFS="-$IFS"
while read mac chan
do echo "rfcomm -i bind rfcomm$port $mac $chan"; ((port+=1))
done < /path/to/file > /path/to/ouput
IFS="$oldIFS"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PH,

I'm out of the office untill tommorow now,

I'll post back with the results, purple star for your help today.

Rob
 
An alternative:

awk -F- 'BEGIN{i=0}{print "rfcomm -i bind rfcomm" i++,$1,$2}' filename | bash -x

Annihilannic.
 
Thanks Annihilannic,

Unfortunatly my box doesnt support the Awk command which is a little fustrating, not to worry though, thanks for your input.

I'll be testing PHV's solution later today, just to see if it is any better than the SED inside the loop i've been running up untill now, we shall see though.

Thanks again guys,

Rob
 
It's Linux!

I've been trying to figure out which distribution its running but i'm struggling to do so.

I'm looking to re-build the box soon with a fresh distribution on it, so i'll have a clearer picture then.

I inherited this box from someone else and its not very well documented.

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top