jaxstorm
asked on
Complicated for loop
I have a CSV file in the following format:
hostname1,mac1
hostname2,mac2
I need to loop through the CSV file printing the hostname and mac in seperate parts of the loop. Ideally I'll assign variables to the hostname and mac and then be able to utilise them like so. I've added some raw pseudo code below, any help appreciated
hostname1,mac1
hostname2,mac2
I need to loop through the CSV file printing the hostname and mac in seperate parts of the loop. Ideally I'll assign variables to the hostname and mac and then be able to utilise them like so. I've added some raw pseudo code below, any help appreciated
for i in `cat /tmp/addnodes`; do
a = hostname
b = mac
print a
print b
done
//output should be
hostname1
mac1
hostname2
mac2
hostname3
mac3
hostname4
mac4
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#!/bin/bash
while IFS=',' read hostname mac
do
echo "hostname = $hostname"
echo "mac = $mac"
done </tmp/addnodes
the easiest way is obviously
tr ',' '\n' < csvfile
This works as well:
IFS="," while read h m; do echo $h\\n$m; done < csvfile
wmp