Solved

I'd like a neat way to parse a list

Posted on 2015-01-14
4
85 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

861 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