How to load different type of data in a file to two arrays

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to load different type of data in a file to two arrays
# 1  
How to load different type of data in a file to two arrays


I have tried to find some sort of previous similar thread on this but not quite close to what I want to achieve.

Basically I have two class of data in my file..e.g

I wanted to read the "yes" entry to an array and "no" entry into another array.

From there, I wanted to do a random selection of yes[1] or yes[2] to duplicate the existing records.The same goes for no[1],no[2] and no[3].

Could anyone shed some light on this?

I tried some partial solution below but it doesnt work:-


awk -F, ' {


else if($NF=="no"){

}' myfile

I tried to do echo ${yes[@]} and echo ${no[@]} but it doesnt print anything.

Please advise. Thanks.
# 2  

An awk array is not a shell array, and if it were, it is created in a child process (awk) which cannot set a variable in the calling shell.

Try this:

yes=( grep yes$ myfile )
no=( grep no$ myfile )

# 3  

I have tried your suggestion by having that two arrays created. But when i did try echo ${yes[@]}, it print out the the command "grep yes$ myfile".

Is there any thing I missed out?

# 4  
I'm guessing cfajohnson meant

yes=( $(grep yes$ myfile) )
no=( $(grep no$ myfile) )

Your awk script only picked out the first field, whereas this will pick out the whole lines. Which do you want? If you only want the first field, you can add a cut

yes=( $(grep yes$ myfile | cut -d, -f1) )
no=( $(grep no$ myfile | cut -d, -f1) )

Last edited by era; 10-09-2008 at 05:53 AM.. Reason: Add cut example
# 5  
Thanks Era and cfajohnson,

Appreciate alot.

But I have a question on the array..after I get that array, how could I know number of entries that the array hold?

I tried

for i in ${yes[@]}

echo $total

But it gives me weird total number of 11 instead.

E.g I have


The Yes total variable should give me 2 instead.

Any mistakes I did?

Please advise. Thanks.
# 6  

To get the number of elements in an array:

echo ${#yes[@]}

To loop through an array (note the quotes):

for e in "${yes[@]}"; do ...

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #218
Difficulty: Easy
A peer-to-peer network sets all connected computers equal and all connected computers share the same abilities to use resources available on the network.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Load data from a flat file to oracle.

I have a flat file with records like Header 123 James Williams Finance2000 124 Pete Pete HR 1500 125 PatrickHeather Engg 3000 Footer The structure is: Eno:4 characters Name:8 characters Surname : 9 characters Dept:7 characters Sal:4characters These are sample... (1 Reply)
Discussion started by: Shivdatta
1 Replies

2. UNIX for Dummies Questions & Answers

Verify the data type in a file with UNIX function

I am seeking help on this UNIX function, please help. Thanks in advance. I have a large file, named as 'MyFile'. It was tab-delmited, I am told that each record in column 1 is unique. How would I verify this with UNIX function or command? (1 Reply)
Discussion started by: duke0001
1 Replies

3. Web Development

script to load data from csv file

hello i want a script to load the data line by line from a csv file into a mysql table (3 Replies)
Discussion started by: srpa01red
3 Replies

4. Shell Programming and Scripting

Load data to flat file from table.

Hi all, I need to know how to copy data from a table say ABC to a flat file say XYZ.dat in unix, Please leave ur comments and the fastest way to do so, I need to load the table records into flat file. Regards Ann (4 Replies)
Discussion started by: Haque123
4 Replies

5. Shell Programming and Scripting

load a data from text file into a oracle table

Hi all, I have a data like, 0,R001,2,D this wants to be loaded into a oracle database table. Pl let me know how this has to be done. Thanks in advance (2 Replies)
Discussion started by: raji35
2 Replies

6. Shell Programming and Scripting

how to check the file data type(ascii or binary)

hi i am receiving a file from one system , i have to verify the format of the file data i.e whether the data is in acii format or binary format, please help thanks in advance satya (1 Reply)
Discussion started by: Satyak
1 Replies

7. Shell Programming and Scripting

Need help in wrting Load Script for a Load-Resume type of load.

hi all need your help. I am wrting a script that will load data into the table. then on another load will append the data into the existing table. Regards Ankit (1 Reply)
Discussion started by: ankitgupta
1 Replies

8. Shell Programming and Scripting

Shell Script to Load data into the database using a .csv file and .ctl file

Since i'm new to scripting i'm findind it difficult to code a script. The script has to be an executable with 2 paramters passed to it.The Parameters are 1. The Control file name(.ctl file) 2. The Data file name(.csv file) Does anybody have an idea about it? :confused: (3 Replies)
Discussion started by: Csmani
3 Replies

9. Shell Programming and Scripting

Reading in data sets into arrays from an input file.

Hye all, I would like some help with reading in a file in which the data is seperated by commas. for instance: input.dat: 1,2,34,/test for the above case, the fn. will store the values into an array -> data as follows: data = 1 data = 2 data = 34 data = /test I am trying to write... (5 Replies)
Discussion started by: sidamin810
5 Replies

10. Programming

FILE data type

Hi all, Can anyone tell me a little about the datatype FILE, which represents stream. What does its structure look like, and in which header file is it defined and so on... Ex : FILE *fp ; fp = fopen("filename", "w") ; (6 Replies)
Discussion started by: milhan
6 Replies

Featured Tech Videos