Turning files into an array


View Poll Results: Which language is the best for data processing
Awk 5 50.00%
Perl 5 50.00%
Voters: 10. This poll is closed

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Turning files into an array
# 8  
Old 03-15-2009
look at the split function, it is using {space}, maybe your files are using {tab}. Once you get the split working the totals will populate. The script now has the mechanics you need. if you have altering conditions like more columns or more files you should be able to make the required changes. This is a learning exercise play with it and learn.
# 9  
Old 03-16-2009
Code:
#!/usr/bin/perl
my (%hash,$first,$second,$third);
my @files=('a.txt','b.txt','c.txt');
sub l_sub{
	my $file=$_[0];
	open FH,"<$file";
	while(<FH>){
		chomp;
		my @tmp=split;
		if($.==1 and $file="a.txt"){
			($first,$second,$third)=@tmp;
			next;
		}
		$hash{$tmp[0]}->{POS}=$. if not exists $hash{$tmp[0]};
		$hash{$tmp[0]}->{$first}.=$tmp[1]." ";
		$hash{$tmp[0]}->{$second}.=$tmp[2]." ";
		$hash{$tmp[0]}->{$third}.=$tmp[3]." ";
	}
}
foreach $f (@files){
	l_sub($f);
}
foreach my $ind ($first,$second,$third){
	print $ind,"\n";
	print join " ", @files;
	print "\n";
	foreach my $key (sort {$hash{$a}->{POS} <=> $hash{$b}->{POS}} keys %hash){
		print $key," ",$hash{$key}->{$ind},"\n";
	}
	print "--------------\n";
}

output:
Code:
GOOD
a.txt b.txt c.txt
Strawberry 1 1 1 
Banana 23 2 2 
Plantain 0 0 5 
Orange 0 0 0 
--------------
BAD
a.txt b.txt c.txt
Strawberry 4 1 0 
Banana 12 1 1 
Plantain 0 0 0 
Orange 0 0 7 
--------------
FAIR
a.txt b.txt c.txt
Strawberry 5 3 0 
Banana 4 0 4 
Plantain 1 0 7 
Orange 0 0 0 
--------------

# 10  
Old 03-19-2009
thanks a lot, the codes work well for the sample, I am still working on it for long list.
# 11  
Old 03-19-2009
you should add Python
Code:
#!/usr/env/python

file1_data = open("file1").readlines()[1:]
file2_data = open("file2").readlines()
file3_data = open("file3").readlines()
good={};fair={};bad={}
for i in range(len(file2_data)):
    file1_data[i] = file1_data[i].split()
    file2_data[i] = file2_data[i].split()
    file3_data[i] = file3_data[i].split()    
    good.setdefault(file1_data[i][0],[])
    fair.setdefault(file1_data[i][0],[])
    bad.setdefault(file1_data[i][0],[])
    good[file1_data[i][0]].append(file1_data[i][1])
    good[file2_data[i][0]].append(file2_data[i][1])
    good[file3_data[i][0]].append(file3_data[i][1])
    fair[file1_data[i][0]].append(file1_data[i][2])
    fair[file2_data[i][0]].append(file2_data[i][2])
    fair[file3_data[i][0]].append(file3_data[i][2])
    bad[file1_data[i][0]].append(file1_data[i][3])
    bad[file2_data[i][0]].append(file2_data[i][3])
    bad[file3_data[i][0]].append(file3_data[i][3])
print "Good"
print "*" * 100    
for i,j in  good.iteritems(): print i,','.join(j)
print "*" * 100    
print "Fair"
for i,j in  fair.iteritems(): print i,','.join(j)
print "*" * 100    
print "Bad"
for i,j in  bad.iteritems(): print i,','.join(j)

Code:
 # ./test.py
Good
****************************************************************************************************
Orange 0,0,0
Strawberry 1,1,1
Banana 23,2,2
Plantain 0,0,5
****************************************************************************************************
Fair
Orange 0,0,7
Strawberry 4,1,0
Banana 12,1,1
Plantain 0,0,0
****************************************************************************************************
Bad
Orange 0,0,0
Strawberry 5,3,0
Banana 4,0,4
Plantain 1,0,7

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Turning given date to epoch

i can probably script this in bash, but, i was wondering, does anyone know of a simple way to translate a given time to epoch? date -d@"29/Oct/2013:17:53:11" the user would specify the date: 29/Oct/2013:17:53:11 and the script will simply interpret that to epoch: 1348838383 (this is just... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. SCO

Need help turning off bootpd

OSR 5.0.7 patched with MP 5 The syslog is flooded with messages: May 9 13:42:12 asiwc bootpd: IP address not found: 192.168.230.215 May 9 13:42:13 asiwc bootpd: IP address not found: 192.168.230.142 May 9 13:42:50 asiwc bootpd: IP address not found: 192.168.230.202 The system... (4 Replies)
Discussion started by: migurus
4 Replies

3. Shell Programming and Scripting

Comparing files in a directory against an array of files

I hope I can explain this correctly. I am using Bash-4.2 for my shell. I have a group of file names held in an array. I want to compare the names in this array against the names of files currently present in a directory. If the file does not exist in the directory, that is not a problem.... (5 Replies)
Discussion started by: BudMan
5 Replies

4. Shell Programming and Scripting

Turning CSV files into individual Variables

I want to be able to convert the following data from a CSV into individual variables from the columns 2 4 and 8 I can use awk to grab the columns using var1=`cat text.csv | awk "," '{print $2}'` but how do I create separate variables for each line. 595358 ,ECON1010 ,THU ,08:00 - 10:00 ,11 Mar... (6 Replies)
Discussion started by: domsmith
6 Replies

5. AIX

turning CIO on and how to monitor

Hi Guys, I have a database server where we run AIX 5.3 on a power5 box and we just turned on CIO (concurrent I/O) for the database filesystems. Now my assumption is that enabling CIO the database basically will bypass the filesystem cache releasing some extra memory that can be allocated... (1 Reply)
Discussion started by: hariza
1 Replies

6. UNIX for Advanced & Expert Users

turning CIO on and how to monitor

Hi Guys, I have a database server where we run AIX 5.3 on a power5 box and we just turned on CIO (concurrent I/O) for the database filesystems. Now my assumption is that enabling CIO the database basically will bypass the filesystem cache releasing some extra memory that can be allocated... (1 Reply)
Discussion started by: hariza
1 Replies

7. Solaris

Turning in.ftpd on and off

For two straight days someone was running in.ftpd in my server (apparently looking to break in) and when I would do "top" almost every line would read "in.ftpd". I had a unix sysadmin friend of mine shut it down and then start it back up in a day and a half and all seems OK for now. Here's what I... (1 Reply)
Discussion started by: thomi39
1 Replies

8. UNIX for Dummies Questions & Answers

Turning Echo off

Hi, Is there any way like in dos to turn the echo off in a script? i have some lines popping up that i dont wish to be viewed when i am unziping a file it brings up the message updating: log.txt (deflated 72%) and extracting: log.txt i dont want these be viewed. Andy (4 Replies)
Discussion started by: chapmana
4 Replies

9. Gentoo

Turning on/off the network interface

Hi all, I'm trying to write a script that will turn off the network interface eth0 on a linux Gentoo machine and then turn it back on, any help? Thanks, Neked (1 Reply)
Discussion started by: neked
1 Replies

10. UNIX for Advanced & Expert Users

Turning off the CDE

I am running Solaris 9 and wanted the CDE stopped when my users login. Can this be done by adding something to the .profile? Basically when they login they should be at the command line and have to start the CDE themselves. Thanks (11 Replies)
Discussion started by: meyersp
11 Replies
Login or Register to Ask a Question