How to extract field delimited by comma and store into an array?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract field delimited by comma and store into an array?
# 1  
Old 05-02-2013
How to extract field delimited by comma and store into an array?

hi,

i have a variable which contains some file names separated by comma. example

Code:
FNAME="abc.txt,def.txt,ghi.txt"

i want to extract each filename and store it into an array and also count the number of files in the array.

Code:
file[0]=abc.txt
file[1]=def.txt
file[2]=ghi.txt

i thought of using the cut command with delimeter as (,)comma, but i dnt know how to extract all file name.

if i use this command it doesnt work.

Code:
file=`echo $FNAME | cut -d"," -f1-`



can anyone tell me how to do this.
# 2  
Old 05-02-2013
Which SHELL are you using?
# 3  
Old 05-02-2013
Quote:
Originally Posted by vidyadhar85
Which SHELL are you using?
i am using bash shell
# 4  
Old 05-02-2013
Here is one way to do it:
Code:
$ cat test.sh
FNAME="abc.txt,def.txt,ghi.txt"

awk_ndx=1
sh_ndx=0
while [ 1 -eq 1 ]; do
  filename=`echo $FNAME | awk '{ print $n }' n=$awk_ndx FS=","`
  [ "$filename" = "" ] && break
  file[$sh_ndx]=$filename
  awk_ndx=`expr $awk_ndx + 1`
  sh_ndx=`expr $sh_ndx + 1`
done

echo ${file[0]}
echo ${file[1]}
echo ${file[2]}
echo ${file[3]}

Code:
$ ./test.sh
abc.txt
def.txt
ghi.txt

# 5  
Old 05-02-2013
can you please display the elements in a for loop instead of writing single statement for each array element.

i used this command but it only displays the 1st element of the array

Code:
for a in $file
do
        echo $a
done

# 6  
Old 05-02-2013
Yes, you can display the elements in a for loop. It's a little hairy. I added to end of script. There is also ${#file[*]} (even more hairy), which is the number of elements in the array, and could be used for same purpose.
Code:
$ cat test.sh
FNAME="abc.txt,def.txt,ghi.txt"

awk_ndx=1
sh_ndx=0
while [ 1 -eq 1 ]; do
  filename=`echo $FNAME | awk '{ print $n }' n=$awk_ndx FS=","`
  [ "$filename" = "" ] && break
  file[$sh_ndx]=$filename
  awk_ndx=`expr $awk_ndx + 1`
  sh_ndx=`expr $sh_ndx + 1`
done

echo all values = ${file[@]}
for a in "${file[@]}"; do
  echo $a
done

Code:
$ ./test.sh
all values = abc.txt def.txt ghi.txt
abc.txt
def.txt
ghi.txt

# 7  
Old 05-02-2013
In bash, assigning arrays is not difficult, nor is the output:
Code:
$ IFS="," files=( $FNAME )
$ echo ${files[1]}
def.txt
$ for f in ${files[*]}; do echo $f; done
abc.txt
def.txt
ghi.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

2. Shell Programming and Scripting

How can awk ignore the field delimiter like comma inside a field?

We have a csv file as mentioned below and the requirement is to change the date format in file as mentioned below. Current file (file.csv) ---------------------- empname,date_of_join,dept,date_of_resignation ram,08/09/2015,sales,21/06/2016 "akash,sahu",08/10/2015,IT,21/07/2016 ... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

3. Shell Programming and Scripting

Help/Advise please for converting space delimited string variable to comma delimited with quote

Hi, I am wanting to create a script that will construct a SQL statement based on a a space delimited string that it read from a config file. Example of the SQL will be For example, it will read a string like "AAA BBB CCC" and assign to a variable named IN_STRING. I then concatenate... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. Shell Programming and Scripting

How can i comma-delimited last field in line?

Awk gurus, Greatly appreciate for any kind of assistance from the expert community Input line: abc,11.22.33.44,xyz,7-8-9-10 pqr,111.222.333.444,wxy,1-2-3 def,22.33.44.55,stu,7-8 used the gsub function below but it changes all of the "-" delimiter: awk 'gsub("-",",")' Desired... (4 Replies)
Discussion started by: ux4me
4 Replies

5. Shell Programming and Scripting

Need a script to convert comma delimited files to semi colon delimited

Hi All, I need a unix script to convert .csv files to .skv files (changing a comma delimited file to a semi colon delimited file). I am a unix newbie and so don't know where to start. The script will be scheduled using cron and needs to convert each .csv file in a particular folder to a .skv... (4 Replies)
Discussion started by: CarpKing
4 Replies

6. Shell Programming and Scripting

Extract a nth field from a comma delimited file

Hi, In my file (which is "," delimited and text qualifier is "), I have to extract a particualr field. file1: 1,"aa,b",4 expected is the 2nd field: aa,b I tried the basic cut -d "," -f 2 file 1, this gave me aa alone instead aa,b. A small hint ot help on this will be very... (5 Replies)
Discussion started by: machomaddy
5 Replies

7. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

8. Shell Programming and Scripting

how to spilit a row into fields and store the field content to the array

consider this is a line A#B#C#D#E#F#G#H note the delimeter is # i want to cut or spilt in to fields using the delimeter # and to store in an array. like this array=A array=B array=C array=D array=E and the array content should be displayed. echo "${array}" echo "${array}"... (5 Replies)
Discussion started by: barani75
5 Replies

9. UNIX for Dummies Questions & Answers

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.... (2 Replies)
Discussion started by: grandtheftander
2 Replies

10. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies
Login or Register to Ask a Question