# Merge two rows using awk or python

**URL:** <https://community.unix.com/t/merge-two-rows-using-awk-or-python/248078>\
**Category:** Shell Programming and Scripting\
**Created:** [August 28, 2009, 2:25am UTC](https://community.unix.com/t/merge-two-rows-using-awk-or-python/248078 "2009-08-28T02:25:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![grossgermany](https://community.unix.com/letter_avatar/grossgermany/32/6_449c533f586939b6e5f246632a46421f.png) [@grossgermany](https://community.unix.com/u/grossgermany)\
**Post date:** [August 28, 2009, 2:25am UTC](https://community.unix.com/t/merge-two-rows-using-awk-or-python/248078/1 "2009-08-28T02:25:06Z")

</div>

Hi,  
Suppose I have a space delimited file like this:

Serial# 1970 1971 1972 1973 1974  
193532 21 2 X X X   
200201 20 30 X X 40  
200201 X X 13 15 X  
393666 66 3 X X 5   
393666 77 X X X X

First, I want to check the serial#, if any two lines have the same serial#,(in this case line 2+3, and line 4+5 qualify), then merge these two lines by replacing X with the value of the other line.

Also, when there is a conflict, in this case line 4+5 have the first column as 66,77(rather than having X in either line or in both lines), then do not merge even though they have the same serial#, but flag both lines with FLAGGED on the CONFLICT\_FLAG column.

The result would be:

Serial# 1970 1971 1972 1973 1974 CONFLICT\_FLAG  
193532 21 2 X X X   
200201 20 30 13 15 40  
393666 66 3 X X 5 FLAGGED  
393666 77 X X X X FLAGGED

Is it possible to do this in either python or awk? Thank you.

---

<div class="post-metadata">

**Author:** ![summer\_cherry](https://community.unix.com/letter_avatar/summer_cherry/32/6_449c533f586939b6e5f246632a46421f.png) [@summer\_cherry](https://community.unix.com/u/summer_cherry)\
**Post date:** [August 28, 2009, 4:40am UTC](https://community.unix.com/t/merge-two-rows-using-awk-or-python/248078/2 "2009-08-28T04:40:25Z")

</div>

below perl should help you some.

```nohighlight
open FH,"<a";
while(<FH>){
 my @tmp = split;
 my @t1=@tmp;
 if (not exists $hash{$tmp[0]}){
   $hash{$tmp[0]}=[@tmp[1..5]];
 }
 else{
   $hash{$tmp[0]}=[@{$hash{$tmp[0]}},@tmp[1..5]];
 }
}
foreach my $key ( keys %hash ){
 my $flag=0;
 my @tmp = @{$hash{$key}};
 if($#tmp < 6){
   print $key," ";
   print join " ", @{$hash{$key}};
   print "\n";
 }
 else{
   my @t = @tmp;
   map { s/X/0/ } @t;
   for(my $i=0;$i<=4;$i++){
     if($t[$i]!=0 && $t[$i+5]!=0){
	$flag=1;	
     }
   }
   if($flag==1){
	print $key," ";
	print join " ", @tmp[0..4];
	print " FLAGGED\n";
	print $key," ";
	print join " ", @tmp[5..9];
	print " FLAGGED\n";
   }
   else{
	print $key," ";
	for(my $i=0;$i<=4;$i++){
	  print $t[$i]+$t[$i+5]," ";
	}
        print "\n";
   }
 }
}

```

---

<div class="post-metadata">

**Author:** ![grossgermany](https://community.unix.com/letter_avatar/grossgermany/32/6_449c533f586939b6e5f246632a46421f.png) [@grossgermany](https://community.unix.com/u/grossgermany)\
**Post date:** [August 28, 2009, 3:33pm UTC](https://community.unix.com/t/merge-two-rows-using-awk-or-python/248078/3 "2009-08-28T15:33:15Z")

</div>

thanks a lot, unfortunately, perl is not installed on site, nor do i have permission to.  
is it possible to do it in python or awk?
