|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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.
Thanks & Regards Last edited by vgersh99; 01-04-2013 at 09:54 AM.. Reason: code tags, please! |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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]}
' File1As always, if you're using a Solaris system, use /usr/xpg4/bin/awk or nawk instead of awk. |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|