Reading a .dat file in to 2 different arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading a .dat file in to 2 different arrays
# 1  
Old 04-12-2010
Data Reading a .dat file in to 2 different arrays

hi all, i have a data file that contains 2 columns, names and numbers. i need to read names in to a an array call names and numbers in to an array call numbers. i also have # and blank lines in my dat file and i need to skip those when i read the dat file. how do i do this? btw, my column 1 and column 2 is seperated by "TAB". i am using k shell (.ksh).

i was able to read without an issue when there was a one cloumn of data. i am not sure how to do this with 2 columns. can someone please help me?

Data file format as follows

2345th3 rghtyk
2345thh fhtene

345672 dkerjgt
#fjerflghh
er4566 djefklrgl


i would like arrays to be like this,
numbers[0]=2345th3
numbers[1]=2345thh
numbers[2]=345672
numbers[3]=er4566


names[0]=rghtyk
names[1]=fhtene
names[2]=dkerjgt
names[3]=djefklrgl

Last edited by usustarr; 04-12-2010 at 08:39 PM..
# 2  
Old 04-12-2010
assuming I got what you want - try this (requires a modern awk, on
solaris try nawk):
Code:
awk '{ if( /^#/ || /^$/ ) {next}
         print $1 >> "one"; 
         print $2 >> "two"   
      }' inputfile

The file named one has column one
the file named two has column two
/^$/ == blank line
/^#/ == line starting with a #
# 3  
Old 04-12-2010
Try...
Code:
$ eval $(awk '!/^$/&&!/^#/{printf "numbers[%s]=%s\nnames[%s]=%s\n",c+0,$1,c++,$2}' file1)

$ echo ${names[2]}
dkerjgt

$ echo ${numbers[3]}
er4566

$

# 4  
Old 04-13-2010
Thank you very much, both of you guys. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use 'ls' command to list files like *.dat, not *.*.dat?

How to use 'ls' command to list files like *.dat, not *.*.dat (5 Replies)
Discussion started by: pmcginni777
5 Replies

2. Shell Programming and Scripting

Reading columns using arrays

Hello, Please help in how to read rows and columns using array and print them. I have below output and i want to store this in array and print the required rows or columns. aaaaaaa 123 bbbbbb 456 ccccccc 888 Use code tags, thanks. (1 Reply)
Discussion started by: Cva2568
1 Replies

3. Shell Programming and Scripting

FASTEN count line of dat file and compare with the CTRL file

Hi All, I thinking on how to accelerate the speed on calculate the dat file against the number of records CTRL file. There are about 300 to 400 folder directories that contains both DAT and CTL files. DAT contain all the flat files records CTL is the reference check file for the... (3 Replies)
Discussion started by: ckwan
3 Replies

4. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

5. Shell Programming and Scripting

reading a .dat file in to a string

i have folowing code. i dont want data in an array. i like to put my data file info (after filtering and converting to lower case) in to a string call "name". so it would look like this, name= ffjtgj345 thgkty3 456gfhf rhtfn4 ...... how do i do that? i dont want to read # or blank lines. ... (13 Replies)
Discussion started by: usustarr
13 Replies

6. Shell Programming and Scripting

KSH: Reading a file line by line into multiple arrays

Hi - I have a file that contains data in this format:- #comment value1 value2 value3 #comment value4 value5 value6 value7 #comment value8 value9 I need to read value1, value2 and value3 into one array, value4 value5 value6 and value7 into another array and value8 and value9 into a 3rd... (2 Replies)
Discussion started by: sniper57
2 Replies

7. Shell Programming and Scripting

Performance issue in UNIX while generating .dat file from large text file

Hello Gurus, We are facing some performance issue in UNIX. If someone had faced such kind of issue in past please provide your suggestions on this . Problem Definition: /Few of load processes of our Finance Application are facing issue in UNIX when they uses a shell script having below... (19 Replies)
Discussion started by: KRAMA
19 Replies

8. Shell Programming and Scripting

Arrays & File Reading

Ok; here is the code INCREMENT=0 #Final Count MATCH=0 #Treated as a Boolean declare -a LINEFOUR #Declared Array for FILE in $DIR; do # DIR was declared earlier test -f $FILE && ( TEMP=(sed -n '4p' $FILE) #How do I assign the fourth line of the file to TEMP? This doesn't... (1 Reply)
Discussion started by: Asylus
1 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. UNIX for Dummies Questions & Answers

Reading and writing SCO DAT tapes uing Linux

Hi, Guys. I've been trying to read and write SCO DAT tapes to my Linux hard disk. I'm using RedHat 6.0 because it is the only version that has device drivers for my SCSI host adapter and SCSI tape drive. When I run the command "cpio -ivt > /dev/st0" I can read the archive from the SCO... (3 Replies)
Discussion started by: cstovall
3 Replies
Login or Register to Ask a Question