?
Solved

Complicated for loop

Posted on 2011-05-11
3
Medium Priority
?
266 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:jaxstorm
  • 2
3 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35736776
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
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 35736782
Sorry, one more semicolon in te second version:

IFS=","; while read h m; do echo $h\\n$m; done < csvfile

0
 
LVL 48

Expert Comment

by:Tintin
ID: 35736837

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

Open in new window

0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Utilizing an array to gracefully append to a list of EmailAddresses
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question