extract data from a data matrix with filter criteria


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract data from a data matrix with filter criteria
# 1  
Old 04-15-2009
extract data from a data matrix with filter criteria

Here is what old matrix look like,

IDs X1 X2 Y1 Y2
10914061 -0.364613333 -0.362922333 0.001691 -0.450094667
10855062 0.845956333 0.860396667 0.014440333 1.483899333
10844119 -0.256424667 -0.397806 -0.141381333 -0.275729667
10857231 -0.048169667 -0.117945667 -0.069776 0.034550333
10918191 -0.020050333 -0.013577 0.006473333 -0.096175667
10909733 0.162614333 0.234994333 0.07238 0.182145
10808085 0.184618667 0.626846333 0.442227667 0.266913667
10896632 -0.10846 -0.074073333 0.034386667 -0.212201667
10842200 -0.355892 -0.080373333 0.275518667 -0.658731667
10820400 -0.039500333 0.142172333 0.181672667 -0.048522667
10790305 0.060801667 0.296146 0.235344333 0.352136333
10850793 0.192093667 0.016334333 -0.175759333 0.018589
10898454 0.266829667 0.251706333 -0.015123333 0.358631
10900279 -0.135841333 -0.208101 -0.072259667 -0.08459
10915745 -0.426376 -0.090368 0.336008 -0.50354
10879483 0.171624333 -0.022833667 -0.194458 0.414899333

extract rows when |X1| or |X2| or |Y1| or |Y2| >= 0.5 and make them a new matrix.

Thanks in advance!
# 2  
Old 04-15-2009
Fairly easy in Tcl. I'm not sure how you'd go about doing it in a "standard" shell script like Ksh, Bash, etc.

Tcl is pretty good for this because all variables are both strings and "lists" (sorta like arrays).

Code:
#! /usr/bin/tclsh

# open the data file for reading.
set fid [open /path/to/matrix.data r]

# loop through all lines in the file.
while { ![eof $fid] } {

        gets $fid row

        # Row are: ID, X1, X2, Y1, Y2
        # We don't care if the ID row is higer than
        # .5, so don't check it.
        set data [lrange $row 1 end]

        #data = X!, X2, Y1, Y2.
        # so foreach d $data will check the individual elements of the list.
        foreach d $data {

                if { $d >= 0.5 } {
                        set newdata $row
                }
        }

        # if we have a match from above, then
        # spit out the entire row.
        if { $newdata != "" } {
                puts stdout $newdata
                set newdata ""
        }
}
# close the file.
close $fid

So using your data above, the script produces:
Code:
[jeffm@stalin:~] tclsh script.tcl
 X1 X2 Y1 Y2
10855062 0.845956333 0.860396667 0.014440333 1.483899333
10808085 0.184618667 0.626846333 0.442227667 0.266913667

I'm sure the Perl people have a better way of doing it. Smilie
# 3  
Old 04-15-2009
Quote:
Originally Posted by ssshen

extract rows when |X1| or |X2| or |Y1| or |Y2| >= 0.5 and make them a new matrix.

Thanks in advance!
here's a partial solution in awk
Code:
awk '$2>=0.5 || $3 >=0.5' file

this only does X1 or X2. i leave it to you to do Y1 and Y2.
# 4  
Old 04-15-2009
but what I meant is absolute value >=0.5, thanks!
# 5  
Old 04-16-2009
can someone please make it work?

the filter based on absolute value, X1>=0.5 and X1<=0.5.

Thanks
# 6  
Old 04-16-2009
Code:
awk '$2 >= 0.5 || $3 >= 0.5 || $4 >= 0.5 || $5 >= 0.5' filename

# 7  
Old 04-16-2009
Thank you folks so much! But it didn't work for me with both awk or Tcl. By the way, I wanted to filter absolute value. Second, I use Mac OS 10. Third The file is much bigger than the sample part.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert data into tabular matrix

Hi There, I want to convert the following data into tabular matrix, based on column 4th and 5th, and output the column 10th value chr1 2804449 2804450 NACpG_1 window1 + chr1 2804443 2804475 1 chr1 2804450 2804451 NACpG_1 window2 + chr1 2804443 ... (3 Replies)
Discussion started by: ChiragNepal
3 Replies

2. Shell Programming and Scripting

[Solved] Converting the data into matrix with 0's and 1's

I have a file that contains 2 columns tag,pos cat input_file tag pos atg 10 ata 16 agt 15 agg 19 atg 17 agg 14 I have used following command to sort the file based on second column sort -k 2 input_file tag pos atg 10 agg 14 agt 15 ata 16 agg 19 atg 17 (2 Replies)
Discussion started by: raj_k
2 Replies

3. Shell Programming and Scripting

Reformatting data in matrix form

Hi, Some assistance with respect to the following problem will be very helpful. I want to reformat my dataset in the following manner for subsequent analysis. I have first column values (which repeat for each value of 2nd column) which are names, the second column specifies position ad the... (1 Reply)
Discussion started by: newbie83
1 Replies

4. Ubuntu

How to convert full data matrix to linearised left data matrix?

Hi all, Is there a way to convert full data matrix to linearised left data matrix? e.g full data matrix Bh1 Bh2 Bh3 Bh4 Bh5 Bh6 Bh7 Bh1 0 0.241058 0.236129 0.244397 0.237479 0.240767 0.245245 Bh2 0.241058 0 0.240594 0.241931 0.241975 ... (8 Replies)
Discussion started by: evoll
8 Replies

5. Shell Programming and Scripting

Extract data based on specific search criteria

I have a huge file (about 2 millions records) contains data separated by “,” (comma). As part of the requirement, I can't change the format. The objective is to remove some of the records with the following condition. If the 23rd field on each line start with 302 , I need to remove that from the... (4 Replies)
Discussion started by: jaygamini
4 Replies

6. Shell Programming and Scripting

Invert Matrix of Data - Perl

I have columnar data in arrays perl, Example - @a = (1,2,3); @array1 = (A,B,C); @array2 = (D,E,F); @array3 = (I,R,T); I want the data to be formatted and printed as 1 A D I 2 B E F 3 C F T and so on... (8 Replies)
Discussion started by: dinjo_jo
8 Replies

7. Shell Programming and Scripting

Extract specific data content from a long list of data

My input: Data name: ABC001 Data length: 1000 Detail info Data Direction Start_time End_time Length 1 forward 10 100 90 1 forward 15 200 185 2 reverse 50 500 450 Data name: XFG110 Data length: 100 Detail info Data Direction Start_time End_time Length 1 forward 50 100 50 ... (11 Replies)
Discussion started by: patrick87
11 Replies

8. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies

9. Shell Programming and Scripting

Merge 70 files into one data matrix

Hi, I have a list of 70 files in a directory and I need to merge the content of each file into one big matrix file (71 columns x 3060 rows). Each file has the following format only two columns per file: unique identifier1 randomtext1 randomtext1 a 5 b 3 c 6 d 3 e 2... (11 Replies)
Discussion started by: labrazil
11 Replies

10. UNIX for Dummies Questions & Answers

changing data into matrix form

Hi, I have a file whose structure is like this 7 7 1 2 3 4 5 1 3 4 8 6 1 4 5 6 0 2 6 8 3 8 2 5 7 8 0 5 7 9 4 1 3 8 0 2 2 3 5 6 8 basically first two row tell the number of rows and column but the data following them are not arranged in that format. now i want to create another... (1 Reply)
Discussion started by: g0600014
1 Replies
Login or Register to Ask a Question