CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
redirect output to different files

 



utpalmtbi
New User

Jan 12, 2015, 12:27 AM

Post #1 of 2 (1436 views)
redirect output to different files Can't Post

 
Hi all...

I have a matrix.txt file that contains clusters with specific numbering such as : (here i have shown only three columns of data; first row and first columns are headers)


1 2 3
1cluster 1 1 2 ..........
2cluster 0 2 3 ..........
3cluster 1 2 0 ..........
4cluster 0 7 0 ..........
5cluster 11 0 0 ..........
6cluster 3 9 0 ..........
7cluster 1 2 1 ..........
8cluster 0 0 9 ..........
9cluster 1 1 13 ..........
.............................
.............................
.............................



Form this matrix , I want to create three files based upon the number of '0' present in each row..

first file (x.txt) should contain those rows that have no '0' it them.. based upon above matrix, file x.txt should contain:


1cluster 1 1 2
7cluster 1 2 1
9cluster 1 1 13
second file (y.txt) should contain those rows that has '0' in each position except one position that has certain number( I do not have a row with a value of all 0)..based upon above matrix, file y.txt should contain:


4cluster 0 7 0
5cluster 11 0 0
8cluster 0 0 9

and the third file (z.txt) should contain all other rows.. i.e. rows having some 0 and some numbers in them..based upon above matrix, file z.txt should contain:

2cluster 0 2 3
3cluster 1 2 0
6cluster 3 9 0



Plz help..
Thanks


BillKSmith
Veteran

Jan 12, 2015, 7:19 AM

Post #2 of 2 (1421 views)
Re: [utpalmtbi] redirect output to different files [In reply to] Can't Post

Your problem becomes much easier if you think of it differently. Consider your input as a CSV file with whitespace separators. You want to partition this file based on the number of non-zero entries in columns 1, 2, and 3.

Read and ignore the header.

for each line:

  • split on whitespace

  • count the non-zero columns with grep

  • write the line to the file indicated by the count


  • count file
    0 -> error
    1 -> y
    2 -> z
    3 -> x



    This should get you started. Show us where you have a problem.
    Good Luck,
    Bill

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives