I am a newbie to Perl. Appreciate anybody can give me some suggestions and help here.
I have a file:
___________________
A1a01 A1a03
A1a03 A1a0b
A1a0b A1a2a
A1a2a A1a02
A1app A1a06
Ala06 A1a07
A1b0v A1b0c
Alb0c Alb55
A1b55 A1b04
.. ..
.. ..
.
.
.
I want to extract and print the sequential data into different groups. As you can see from the example given, the groups are from A1a01 to A1a02, A1app to A1a07, and A1b0v to A1b04. Each group is in sequential manner.
So far I have tried the code below, but it doesn't work to get what I need. I only manage to print those matched names after comparison of column1 and column2. I do not have idea how to extract the non-match names. (not sequential in order after compare column1 2nd name with column2 1st name of 2 rows data.) And I need to print each group first and last names.
Expand|Select|Wrap|Line Numbers
- use strict;
- use warnings;
- my $i;
- my $j;
- my @column1;
- my @column2;
- my @array1;
- my $lastname;
- @column1=`awk '{print \$1}' saved4`;
- @column2=`awk '{print \$2}' saved4`;
- for ($i=0;$i<=$#column1;$i++){
- for ($j=0;$j<=$#column2;$j++){
- if ($column1[$i]=~ /$column2[$j]/){
- push (@array1,$column2[$j]);
- }
- else {
- $lastname = $column2[$j];
- }
- }
- }
- print "$column1[0] @array1 $lastname\n";
The expected result is like:
group1:
A1a01 A1a03
A1a03 A1a0b
A1a0b A1a2a
A1a2a A1a02
group2:
A1app A1a06
Ala06 A1a07
group3:
A1b0v A1b0c
Alb0c Alb55
A1b55 A1b04