Link to home
Start Free TrialLog in
Avatar of jaxstorm
jaxstormFlag for United Kingdom of Great Britain and Northern Ireland

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
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

Open in new window

Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Hi,

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
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tintin
Tintin


#!/bin/bash
while IFS=',' read hostname mac
do
  echo "hostname = $hostname"
  echo "mac = $mac"
done </tmp/addnodes

Open in new window