Solved

I'd like a neat way to parse a list

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

705 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

20 Experts available now in Live!

Get 1:1 Help Now