Different type of join


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Different type of join
# 1  
Old 09-16-2009
Different type of join

Hi guys is there any simple one liner that can join some thing like this
input
Code:
10  20  30  11  24  26
11  22  34  -2  88  22
-2  24 12  24  20  -90
         10  28  -9  -80

1st column should join the similar values in 3rd or 4th
2nd column should join the similar value in 5th or 6th column

output
Code:
10  10  28  20  20  -90
11  30  11  22  88  22
-2  34  -2  24  24  26

# 2  
Old 09-16-2009
Quote:
Originally Posted by repinementer
... is there any simple one liner that can join some thing like this ...

output
Code:
10  10  28  20  20  -90
11  30  11  22  88  22
-2  34  -2  24  24  26

Can't say if it's simple, but it's definitely not a one-liner.

Code:
$
$ cat f1
 10  20  30  11  24  26
 11  22  34  -2  88  22
 -2  24  12  24  20 -90
         10  28  -9 -80
$
$
$ ##
$ perl -lne 'chomp;
>            @x=unpack("A3xA3xA3xA3xA3xA3",$_);
>            push @y, [@x];
>            END {
>              $i=0;
>              while($y[$i][0] ne ""){
>                printf("%s ",$y[$i][0]);
>                for ($j=0; $j<=$#y; $j++){
>                  if ($y[$i][0] eq $y[$j][2] or $y[$i][0] eq $y[$j][3]){
>                    printf("%s %s",$y[$j][2],$y[$j][3]);
>                    break;
>                  }
>                }
>                printf(" %s ",$y[$i][1]);
>                for ($j=0; $j<=$#y; $j++){
>                  if ($y[$i][1] eq $y[$j][4] or $y[$i][1] eq $y[$j][5]){
>                    printf("%s %s",$y[$j][4],$y[$j][5]);
>                    break;
>                  }
>                }
>                $i++;
>                print;
>              }
>            }
>           ' f1
 10  10  28  20  20 -90
 11  30  11  22  88  22
 -2  34  -2  24  24  26
$
$

tyler_durden
# 3  
Old 09-17-2009
Oh, I didn't realize it needs a huge script.
ANyways Thanx for the code .but I'm not getting the right results. I'm attaching my inputfile. could you check please.

And one more thing. If input is the same with some additional columns. Is it possible to modify the script??

input
Code:
10  20  30  11  XX  24  26  YY
11  22  34  -2  Q1  88  22  D3E
-2  24 12  24  ZZ  20  -90  AA
         10  28  SS  -9  -80  QQ

ouput
Code:
10  10  28  20  XX  20  -90  YY
11  30  11  22  Q1  88  22  D3E
-2  34  -2  24  ZZ  24  26  AA


Last edited by repinementer; 09-17-2009 at 12:52 AM..
# 4  
Old 09-17-2009
# 5  
Old 09-17-2009
Code:
open FH,"<a.txt";
while(<FH>){
chomp;
my @tmp=split;
if($#tmp<5){
  @tmp=('','',@tmp);
}
$hash{$.}->{1}=$tmp[0];
$hash{$.}->{2}=$tmp[1];
$hash1{$tmp[2]." ".$tmp[3]}=1;
$hash2{$tmp[4]." ".$tmp[5]}=1;
}
foreach my $key(sort keys %hash){
  my $str1=$hash{$key}->{1};
  if ($str1 != ""){
  my @t1= grep {/([0-9]{2})?$str1([0-9]{2})?/} keys %hash1;
  my $str2 = $t1[0];
  my $str3=$hash{$key}->{2};
  my @t2= grep {/([0-9]{2})?$str3([0-9]{2})?/} keys %hash2;
  my $str4 = $t2[0];
  print $str1," ",$str2," ",$str3," ",$str4,"\n";
  }
}

# 6  
Old 09-23-2009
Thanx Cherry Working great
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
2 Replies

2. UNIX for Dummies Questions & Answers

How to use the the join command to join multiple files by a common column

Hi, I have 20 tab delimited text files that have a common column (column 1). The files are named GSM1.txt through GSM20.txt. Each file has 3 columns (2 other columns in addition to the first common column). I want to write a script to join the files by the first common column so that in the... (5 Replies)
Discussion started by: evelibertine
5 Replies

3. UNIX for Dummies Questions & Answers

how to join two files using "Join" command with one common field in this problem?

file1: Toronto:12439755:1076359:July 1, 1867:6 Quebec City:7560592:1542056:July 1, 1867:5 Halifax:938134:55284:July 1, 1867:4 Fredericton:751400:72908:July 1, 1867:3 Winnipeg:1170300:647797:July 15, 1870:7 Victoria:4168123:944735:July 20, 1871:10 Charlottetown:137900:5660:July 1, 1873:2... (2 Replies)
Discussion started by: mindfreak
2 Replies

4. Shell Programming and Scripting

Join help

im trying to join 3 files together sort -n ${ASS1_DATA_DIR}Prac1 | sed '/^#/d' > ${ASS1_OUTPUT_DIR}Prac1temp sort -n ${ASS1_DATA_DIR}Prac2 | sed '/^#/d' > ${ASS1_OUTPUT_DIR}Prac2temp join -a1 -a2 -1 1 -2 1 -o "0 1.2 2.2" -e " " ${ASS1_OUTPUT_DIR}Prac1temp ${ASS1_OUTPUT_DIR}Prac2temp >... (6 Replies)
Discussion started by: bigubosu
6 Replies

5. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

6. Programming

sql,multiple join,outer join issue

example sql: select a.a1,b.b1,c.c1,d.d1,e.e1 from a left outer join b on a.x=b.x left outer join c on b.y=c.y left outer join d on d.z=a.z inner join a.t=e.t I know how single outer or inner join works in sql. But I don't really understand when there are multiple of them. can... (0 Replies)
Discussion started by: robbiezr
0 Replies

7. Shell Programming and Scripting

join (pls help on join command)

Hi, I am a new learner of join command. Some result really make me confused. Please kindly help me. input: file1: LEO oracle engineer 210375 P.Jones Office Runner ID897 L.Clip Personl Chief ID982 S.Round UNIX admin ID6 file2: Dept2C ID897 6 years Dept5Z ID982 1 year Dept3S ID6 2... (1 Reply)
Discussion started by: summer_cherry
1 Replies

8. Programming

array type has incomplete element type

Dear colleagues, One of my friend have a problem with c code. While compiling a c program it displays a message like "array type has incomplete element type". Any body can provide a solution for it. Jaganadh.G (1 Reply)
Discussion started by: jaganadh
1 Replies

9. Shell Programming and Scripting

String type to date type

Can one string type variable changed into the date type variable. (1 Reply)
Discussion started by: rinku
1 Replies
Login or Register to Ask a Question