Merging two files baased on condition | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Merging two files baased on condition

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-04-2013
satyar satyar is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 24 January 2013, 9:15 AM EST
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Merging two files baased on condition

Hi All,
I have two below files(fileds separated by space).

File1

Code:
001078401      A      5A1
001078401     B       085
001030035   A      5A1
001030035   B       085

File2

Code:
001078401      C      001
001078401      D   065
001030035   C      001
001030035   D   065

And the out file should be


Code:
001078401    A       5A1   B     085   C     001  D     065
001030035  A       5A1   B     085   C     001  D     065

Files would be joined on a common filed, then i need one row for a single id.
Please guide me, how can i achieve this.
Moderator's Comments:
Please use code tags when posting data and code samples!


Thanks & Regards

Last edited by vgersh99; 01-04-2013 at 09:54 AM.. Reason: code tags, please!
Sponsored Links
    #2  
Old 01-04-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 15 July 2014, 11:14 AM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts
try:

Code:
awk '
! a[$1] {a[$1]=$1}
{ for (i=2; i<=NF; i++) a[$1]=a[$1] OFS $i; }
END {for (i in a) print a[i]}
' File1 File2

The Following User Says Thank You to rdrtx1 For This Useful Post:
satyar (01-07-2013)
Sponsored Links
    #3  
Old 01-07-2013
satyar satyar is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 24 January 2013, 9:15 AM EST
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Hi,

I am slightly changing the requirement.
Please help.

I have a single file like..

Code:
001078401*A*001
001078401*B*065
00107840*A*001
001078401*C*001
00107840*D*065
001078401*E*1200
001030035*A*001
001030035*B*065
001030035*C*001
001030035*D*065
001030031*E*200
001030035*E*1200
001030032*D*06
001030031*E*1200

And required output format is...


Code:
001078401*A*001*B*065*C*001*E*1200
00107840*A*001*D*065
001030035*A*001*B*065*C*001*D*065*E*1200
001030031*E*200*E*1200
001030032*D*06

Thanks

Last edited by Scott; 01-07-2013 at 08:02 AM.. Reason: Code tags
    #4  
Old 01-07-2013
Fundix Fundix is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 16 January 2014, 5:23 AM EST
Posts: 71
Thanks: 16
Thanked 11 Times in 11 Posts
Using a Perl script :

Code:
#!/usr/bin/perl -w
use strict;

my $cur_dir = $ENV{PWD};
my $filename = $cur_dir."/file";
my ($record,$field,@fields,%hash);

open(FILE,"<$filename") or die"open: $!";

while( defined( $record = <FILE> ) ) {
  chomp $record;
  @fields=split(/\*/,$record);
  $hash{$fields[0]}.="\*$fields[1]\*$fields[2]";
}

foreach (sort keys %hash) {
  print "$_$hash{$_}\n";
}

close(FILE);

Output :

Code:
001030031*E*200*E*1200
001030032*D*06
001030035*A*001*B*065*C*001*D*065*E*1200
00107840*A*001*D*065
001078401*A*001*B*065*C*001*E*1200

Sponsored Links
    #5  
Old 01-07-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 15 September 2014, 8:27 PM EDT
Location: San Jose, CA, USA
Posts: 4,633
Thanks: 179
Thanked 1,550 Times in 1,318 Posts
Or you could simply change the code provided by rdrtx1 in message #2 in this thread to meet your new requirements:

Code:
awk 'BEGIN{FS = "[*]";OFS = "*"}
! a[$1] {a[$1]=$1}
{ for (i=2; i<=NF; i++) a[$1]=a[$1] OFS $i; }
END {for (i in a) print a[i]}
' File1

As always, if you're using a Solaris system, use /usr/xpg4/bin/awk or nawk instead of awk.
Sponsored Links
    #6  
Old 01-07-2013
satyar satyar is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 24 January 2013, 9:15 AM EST
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Hi,
I managed to do this....
Code given below...


Code:
        BEGIN {FS = "*"}
        a[$1]= a[$1]"*" $2 "*" $3
        END{for(i in a)
                print i a[i] > "satya_new.txt"
           }

Please let me know....if u see any logical error in the code

Thanks

Last edited by radoulov; 01-07-2013 at 08:52 AM.. Reason: Tags fixed.
Sponsored Links
    #7  
Old 01-07-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 15 September 2014, 8:27 PM EDT
Location: San Jose, CA, USA
Posts: 4,633
Thanks: 179
Thanked 1,550 Times in 1,318 Posts
Quote:
Originally Posted by satyar View Post
Hi,
I managed to do this....
Code given below...


Code:
        BEGIN {FS = "*"}
        a[$1]= a[$1]"*" $2 "*" $3
        END{for(i in a)
                print i a[i] > "satya_new.txt"
           }

Please let me know....if u see any logical error in the code

Thanks
Assuming that you saved the above awk script in a file (say satya.awk for purposes of this discussion), and then ran the command:

Code:
awk -f satya.awk File1

then it might or might now work since setting FS to * produces undefined behavior.

I've marked a couple of changes. The first change is needed to get defined results. Your code in the second case is unconventional, but should work as expected either way (i.e., with or without the braces}:

Code:
        BEGIN {FS = "[*]"}
           {a[$1]= a[$1]"*" $2 "*" $3}
        END{for(i in a)
                print i a[i] > "satya_new.txt"
           }

The Following User Says Thank You to Don Cragun For This Useful Post:
satyar (01-08-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Merging two files with condition digipak Shell Programming and Scripting 3 07-30-2011 12:34 AM
Merging of all files based on a condition arund_01 Shell Programming and Scripting 5 02-15-2010 09:40 AM
merging two files rameshonline Shell Programming and Scripting 14 04-06-2009 01:20 AM
Merging files ssuresh1999 UNIX for Dummies Questions & Answers 4 09-15-2008 11:29 AM
merging files vakharia Mahesh Shell Programming and Scripting 6 08-12-2008 02:23 PM



All times are GMT -4. The time now is 08:53 PM.