![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Combining information from a comma delimited file | vzismann | UNIX for Dummies Questions & Answers | 1 | 08-06-2007 06:20 PM |
| Parsing comma delimited text file | chengwei | Shell Programming and Scripting | 5 | 02-23-2007 05:38 AM |
| Converting Tab delimited file to Comma delimited file in Unix | charan81 | Shell Programming and Scripting | 22 | 01-20-2006 09:24 AM |
| Comma Delimited file | dbrundrett | Shell Programming and Scripting | 2 | 04-05-2004 10:50 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Loading a comma Delimited file into an Array
Hello,
I have been stuck on this aspect of loading a comma delimited file into an array. I thought i had the syntax right, but my commands are not working the way I want them to. Basically my cut command is splitting the file up by spaces and commas. I want the cut command to ignore white spaces. I have googled, used the man pages, and tried other solutions ie (awk, sed), but I am still stuck on this aspect. comma delimited file: Code:
23232321223,11100,N/A,Hey You There,N/A,N/A,Wed Jul 26 09:02:26 2006 Code:
#script to split it into an array
SPArray=( "${SPArray[@]}" `cat $dnNum"SPTest.txt" | cut -d, -f2`)
SPArray=( "${SPArray[@]}" `cat $dnNum"SPTest.txt" | cut -d, -f3`)
SPArray=( "${SPArray[@]}" `cat $dnNum"SPTest.txt" | cut -d, -f4`)
len=${#SPArray[*]}
#Displaying the contents of the array
i=0
while [ $i -lt $len ]; do
echo "$i: ${SPArray[$i]}"
i=$((i+1))
done
0: 11100 1: N/A 2: Hey 3: You 4: There the output I wish i had: 0: 11100 1: N/A 2: Hey You There |
|
||||
|
Here's an alternative way of doing this that I use (ksh):
Code:
{
x='23232321223,11100,N/A,Hey You There,N/A,N/A,Wed Jul 26 09:02:26 2006'
typeset IFS=,
set -A SParray $( print "${x}" )
set | grep SParray
}
SParray[0]=23232321223
SParray[1]=11100
SParray[2]=N/A
SParray[3]='Hey You There'
SParray[4]=N/A
SParray[5]=N/A
SParray[6]='Wed Jul 26 09:02:26 2006'
|
|
||||
|
More could be said. If you only want certain elements as is indicated by your cut -f2,-f3, and -f4, you can reassign them to another array or simply reference them only SParray[1], SParray[2], SParray[3].
Last edited by tmarikle; 07-26-2006 at 07:17 PM.. Reason: sp |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|