checking co-presence of Var. - Shell or Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting checking co-presence of Var. - Shell or Perl
# 1  
Old 10-15-2012
checking co-presence of Var. - Shell or Perl

Hey fellas,

I've posted this problem a few days back and I received just one post which was in PHP that I have no idea about! (Thanks to DGPickett) It would be so nice if you can help me with this in Shell or Perl. Here is the story:

I have a big table with variables and observations. I managed to simplify the data to I/O so now it's something looks like this:

* 1 2 3 4 5 6 7 8 9 10
A 1 0 0 1 0 0 1 0 1 1
B 1 1 1 1 0 0 1 0 0 0
C 1 1 0 1 0 1 0 1 1 0
D 0 0 0 1 1 1 1 0 0 0
E 1 1 0 0 1 1 0 0 1 1

What I like to find out is the co-presence of the variables. The script has to read the table in a loop which in each round check one row with all the others (including itself). While checking 2 rows write the number of times there is number 1 for both in the same column.

Simply, it starts from row A and first check it with itself (since the first row is A!). The returning number would be the times that we have "1" in A. Then by checking row A and B it returns number of columns in which we have "1" for A and B. Like this it goes until the end of the table and writes the results to the first row of the output.

Then the output would be a table with variables as both rows and columns containing the co-presence of them based on the calculations.

It is a cool problem that I unfortunately don't have enough knowledge to solve it. If you have knowledge and time, you're a lifesaver.

Thanks for reading even if you are not going to help! Smilie
# 2  
Old 10-15-2012
Please post the output required for this sample.
# 3  
Old 10-15-2012
It would be something like this:

* A B C D E
A 5 3 2 2 3
B 3 5 3 2 2
C 2 3 6 2 4
D 2 2 2 4 2
E 3 2 4 2 6

If I didn't make a mistake in my calculations! Smilie
# 4  
Old 10-15-2012
Code:
awk 'NR>1{name[NR-1]=$1;for(i=2;i<=NF;i++) if($i==1) oneset[NR-1,i]=1}
END{
for(i=1;i<=(NR-1);i++)
{
 if(i==1)
 {
  print "*"
  for(j=1;j<=(NR-1);j++)
   print name[j]
  printf "\n"
 }
 print name[i]
 for(j=1;j<=(NR-1);j++)
 {
  n=0
  for(k=2;k<=NF;k++)
   if(oneset[i,k] && oneset[j,k])
    n++
  print n
 }
 printf "\n"
}
}' ORS='\t' file

This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 10-15-2012
WOW! honestly... There are a-lot here that I have to learn first! Smilie I'm going through it and try to apply it on my data. I'll let you know if I managed to make it or not.
Thanks man...
# 6  
Old 10-15-2012
elixir_sinari is unbeatable. Still, I can see some (minor!) simplifications to his proposal:
Code:
awk     'NR>1 {name[NR-1]=$1
               for(i=2;i<=NF;i++) oneset[NR-1,i]=$i}
         END  { print "*"
                for(j=1;j<=(NR-1);j++)
                print name[j]
                printf "\n"

                for(i=1;i<=(NR-1);i++)
                {
                 print name[i]
                 for(j=1;j<=(NR-1);j++)
                         {n=0
                          for(k=2;k<=NF;k++)  n += oneset[i,k] * oneset[j,k]
                          print n
                         }
                 printf "\n"
                }
             }
        ' ORS='\t' file

# 7  
Old 10-15-2012
Oh my! You guys are amazing! You know what Rudic I think You are right! elixir_sinari is awesome! So are you as I've seen your posts in the forum! Smilie
I was trying to understand what elixir_sinari wrote. It seems it's working what he gave me!! Then I will go through your simplification! Smilie

Thanks to you both!!

Last edited by @man; 10-15-2012 at 09:27 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Shell Programming and Scripting

Passing PERL var values to SH Shell Script

Greetings all, If I have a SH script that calls a PERL script in the following way: perl $HOME/scripts/config.properties And in the config.properties PERL file, this Perl script only sets a number of environmental parameters in the following way: #!/usr/bin/perl $VAR1 = ( ... (3 Replies)
Discussion started by: gikyo12
3 Replies

3. Shell Programming and Scripting

checking the request for urls - perl

hi everybody . i'm trying to extract a bunch of urls from an http request but when i do that i get nothing . here it's my code use LWP::UserAgent; $url = "$ARGV"; $ua = LWP::UserAgent->new; $req = HTTP::Request->new(GET=>$url); $res = $ua->request($req); if... (0 Replies)
Discussion started by: KiD0
0 Replies

4. Shell Programming and Scripting

Checking for presence of any files

Is there code in Cshell scripting to check for the presence of any files in the current directory (and only the current directory)? I tried: if (-r *) then ... but Cshell doesn't like that. Thanks, Paul Hudgens (0 Replies)
Discussion started by: phudgens
0 Replies

5. Shell Programming and Scripting

Checking for presence of any files

I have tried the following script to check for the presence of any files in a folder: if (-r *) then goto ZipMiscFiles else echo "" echo " No Miscellaneous files found. Exiting program." echo "" exit endif The -r works fine with the wildcard in combo with other... (4 Replies)
Discussion started by: phudgens
4 Replies

6. Shell Programming and Scripting

Perl error checking question

I am not very good with perl but trying to force myself to start learning... I have a script that calls three other scripts in variables. I want to use a if statement to check the exit status and not sure how to do it.. This is basically what I have, the individual scripts print either a Y or... (1 Reply)
Discussion started by: i9300
1 Replies

7. Shell Programming and Scripting

Perl data type checking

I am using perl 5.8.0. I need to check some values to see it they are floats. Our system does not have Data::Types so I can't use is_float. Is there something else that I can use? The only thing in Data is Dump.pm. I am not allowed to download anything to our system so I have to use what I have.... (3 Replies)
Discussion started by: ajgwin
3 Replies

8. Shell Programming and Scripting

Checking for the presence of a string within another string

How to check if a string in contained in another string ? Like Whether the String "brown" is contained in "A quick brown fox jumps over a lazy the dog" (1 Reply)
Discussion started by: hidnana
1 Replies

9. Shell Programming and Scripting

get a part from a $var in perl

Hi, I have a file which has list of files with some other info too.. say.. 1/1/2008./to/path/filename1.: ... ... something like that.. can anyone tell me how can i just open the file and read the contents and then cut just the filename1 and write it to an @array.. thanks (12 Replies)
Discussion started by: meghana
12 Replies

10. UNIX for Dummies Questions & Answers

How do I test for the presence of a file in Bourne Shell

How do I test for the presence of a file in Bourne Shell (3 Replies)
Discussion started by: vins
3 Replies
Login or Register to Ask a Question

Featured Tech Videos