LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-28-2012, 04:56 PM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Rep: Reputation: 60
Assigning Multiple Comma Separated IP's To A Bash Array


I am in the process of creating a BASH shell scripts for a project at work. So the scenario is as such:

I have a file with each line entry separated by ':'

Code:
testaccount:D#%G%^V&:MeMyselfandI:memyselfandi@somesite.com:11/242012:192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5:$1360.
In parsing these strings, The only data that I need as of right now is only the row of ip address which is always the second to the last field of the line. I can successfully create my array using:

Code:
test_array=( $( cat wspasswd| awk -F ':' '/^testaccount/ {print $(NF-1)  }' ) )
which produces:

Code:
echo ${#test_array[@]}
192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5
which is exactly what I want except I noticed that it is putting all of the results in only "1" index

so:

Code:
echo ${#test_array[@]}
1
echo ${test_array[0]}
192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5
I need each one of these ip address to go into its own index, so for example:

Code:
echo ${#test_array[@]}
5
echo ${test_array[0]}
192.168.1.1
echo ${test_array[1]}
192.168.1.2
and so forth
What am I missing?

Last edited by metallica1973; 06-28-2012 at 08:25 PM.
 
Old 06-28-2012, 05:17 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
You forgot to convert the commas in the field into newlines in the awk script just prior to printing the field. So, add
Code:
gsub(/,+/, "\n", $(NF-1)) ;
into the awk command, just before the print part. I would personally use
Code:
gsub(/[\t\v\f ,]+/, "\n", $(NF-1)); printf("%s\n", $(NF-1))
to remind myself \n is explicitly used.
 
1 members found this post helpful.
Old 06-28-2012, 08:23 PM   #3
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
thanks for the reply. I am angry at myself becaused I was using the
Code:
IFS=','
which will causes a split instead of a space before but totally forgot to use it during testing from the cli. Once I declared field separator all worked as planned.

Code:
echo ${#test_array[*]}
5
 
Old 06-29-2012, 04:30 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Just a word of caution that when you (un)set IFS to be wary of undoing your change as it may affect other parts of your code in an unexpected fashion
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script to merge files together (given as a comma separated string) DomeKor Linux - Newbie 10 09-27-2011 11:29 PM
[SOLVED] Randomize the comma separated string in shell mariakumar Linux - General 13 10-11-2010 12:32 AM
How to delete Comma in a comma separated file with double quotes as quote character pklcnu Linux - Newbie 2 03-24-2009 05:50 PM
bash syntax: looping through a comma-separated list David the H. Linux - General 10 09-06-2007 10:23 AM
Reading comma-separated data from file MeLassen Programming 4 04-04-2004 02:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration