Solved

I'd like a neat way to parse a list

Posted on 2015-01-14
4
81 Views
Last Modified: 2015-01-14
I have a text file, which contains lots of configuration parameters, e.g.

file.txt:
blah, blah, blah
HOSTS=10.10.12.7,10.10.12.16,10.10.12.20,10.10.13.235
blah, blah, blah

I want to do stuff with each host in this list.  I have a very crude parse as follows in my script.

for i in `grep HOSTS file.txt | awk -F"=" '{print $2}' | awk -F"," '{print $1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9}'`
  do
      stuff
  done

I don't want to list the elements $1 etc. as the list could be any size.  Anyone have a neater way for doing this?
0
Comment
Question by:mickt
  • 2
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40548604
IFS=,
for i in `awk -F= '$1=="HOSTS"{print $2}' file.txt`
  do
      stuff
  done
0
 

Author Comment

by:mickt
ID: 40548612
Doesn't work as it is seen as a single entity, i.e. 10.10.12.7,10.10.12.16,10.10.12.20,10.10.13.235

What I need to so is break it out further so that I can for example ssh to each IP.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40548616
Did you set
IFS=,
0
 

Author Comment

by:mickt
ID: 40548628
Forgot the ,

Great, thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
exchange 2007 3 56
Problem logging tar errors 11 54
RoboCopy to Changing External Drives 2 39
Unable to disassociate and associate elastic IP address EC2-VPC 6 46
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now