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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to spilit a row into fields and store the field content to the array
# 1  
Old 02-23-2010
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[0]=A
array[1]=B
array[2]=C
array[3]=D
array[4]=E

and the array content should be displayed.

echo "${array[$0]}"
echo "${array[$1]}"
echo "${array[$2]}"
echo "${array[$3]}"
echo "${array[$4]}"

provide me code and i am using bash script.

Thanks
barani
# 2  
Old 02-23-2010
Same answer as two days ago.

This work with all posix-sh compatible shells (ksh, posixsh, bash, ...).
Code:
oifs="$IFS"   # -save IFS value
IFS="#" 
flds=( $fileline )
IFS="oifs"    # - return original alue
nrofflds=${#flds[@]}

fld1="${flds[0]}"  
fld=0
while (( fld<nrofflds ))
do
       echo "{flds[$fld]}"  
       ((fld+=1))
done


Last edited by kshji; 02-23-2010 at 03:22 AM..
# 3  
Old 02-23-2010
In perl:
Code:
$str="A#B#C#D#E#F#G#H";
@arr=split(/#/,$str);

for($i=0;$i<=$#arr;$i++)
{
print "Array at pos $i is:$arr[$i]\n";
}

cheers,
Devaraj Takhellambam
# 4  
Old 02-23-2010
You can use the cut command with the -d option. this is used to specify the delimiter

And you can specify the field numbers using -f.

echo A#B#C#D#E#F#G#H | "cut -d"#" -f 1
The above code will give 'A'. Then assign this to array[$0]

obviously A#B#C#D#E#F#G#H | "cut -d"#" -f 2 will give 'B'
and so on

Last edited by thillai_selvan; 02-23-2010 at 02:49 AM..
# 5  
Old 02-23-2010
MySQL solution

For specifying the index you used $0,$1,etc.I thing you misunderstood wrongly .just use 0,1,etc for the index.

Here is the solution.using the IFS you can achieve it easily.

Code:
str="A#B#C#D#E#F#G#H"

IFS="#"

array=( $str )

echo "${array[0]}"
echo "${array[1]}"
echo "${array[2]}"
echo "${array[3]}"
echo "${array[4]}"

# 6  
Old 02-23-2010
Karthi's is simple and straight forward but as a code discipline we should follow some rules like we must not change the IFS variable.

for that we must keep the IFS value by take a copy of that
hence I change his code with a little difference
Code:
 
str="A#B#C#D#E#F#G#H"
tmp=$IFS ; # take a copy 
IFS="#"

array=( $str )

echo "${array[0]}"
echo "${array[1]}"
echo "${array[2]}"
echo "${array[3]}"
echo "${array[4]}"

IFS =$tmp;  # get back the previous value

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Analyzing last 2 fields of 1 row and 3rd field of next row

I have the following script that will average the last two fields of each row, but im not sure how to include the 3rd field of the following row. An example of the analysis that I need to perform from the input - (66.61+58.01+54.16)/3 awk '{sum=cnt=0; for (i=13;i<=NF;i++) { sum+=$i; cnt++... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

2. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

3. Shell Programming and Scripting

Inserting a field without disturbing field separator on other fields

Hi All, I have the input as below: cat input 032016002 2.891 97.109 16.605 27.172 24.017 32.207 0.233 0.021 39.810 0.077 0.026 19.644 13.882 0.131 11.646 0.102 11.449 76.265 23.735 16.991 83.009 8.840 91.160 0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625 0.121 1.126 40.189... (15 Replies)
Discussion started by: am24
15 Replies

4. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

5. Shell Programming and Scripting

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 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. file=abc.txt file=def.txt file=ghi.txt i thought of using the... (8 Replies)
Discussion started by: Little
8 Replies

6. Shell Programming and Scripting

Store content from array to Spread_sheet using perl

How to store the content from array to either "row-column" or "column-row" order? (0 Replies)
Discussion started by: kavi.mogu
0 Replies

7. Shell Programming and Scripting

Store all the passed arguments in an array and display the array

Hi I want to write a script which store all the parameters passed to the script into an array. Once it is stored I want scan through the array and and delete those files for last month present inside the directory. The files in directory is appneded with YYYY_MM_DD. I want to know how can I... (3 Replies)
Discussion started by: dgmm
3 Replies

8. Shell Programming and Scripting

how to split the row(array) in to fields and store in to oracle database in unix

Hi, the csv file with the delimeter #. A#B#C#D#E#F#G#H 1#2#3#4#5#6#7#8 Z#x#c#V 7#2#8#9 N. I want to read the file line by line and store in rowarray. then the rowarray content should be spilt or made to fields using the delimeter #. i am not sure can we store the fields in to... (3 Replies)
Discussion started by: barani75
3 Replies

9. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

10. Shell Programming and Scripting

split varibles and store fields into shell varible array

I need to split a long varible which is a whole line read from a file into fields and store them in an array, the fields are delimited by pipe and a field may contain white spaces. I tried the following concept test and it has problem with field 5 which contain a space, appearently so because... (3 Replies)
Discussion started by: gratus
3 Replies
Login or Register to Ask a Question