Solved

I'd like a neat way to parse a list

Posted on 2015-01-14
4
88 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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

729 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