Solved

I'd like a neat way to parse a list

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 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

10 Experts available now in Live!

Get 1:1 Help Now