Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

merge 2 files into third

Status
Not open for further replies.

Meher1

Programmer
Jun 4, 2003
7
US
Hi, I have two files with the same format. Each of these files are | delimited and have a record identifier.
1 header, 2 detail, 3 trailer.

In the out put file:
I need to have the header of file a, merge the detail records of two files and sum up the corresponding trailer values of these two files excluding the identifier. Here is the format.

file a.
1|lkjh|ertwui|wweweqweefgh00|wqeqwe|sweqwe|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
3|1|2|3||5|

file b.
1|abcd|abcd|efgh00|bnm|sswee|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
3|1|2|3||3|

out put file c.
1|abcd|abcd|efgh00|bnm|sswee|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
2|wert00|weqweqwe|weqweqwe|qweqweqwe|qewqe|qeqeqwe||qweqeqw|
3|2|4|6||8|

**********************************************

Please help!

Thanks,
Meher.

 
awk -F\| '
NR==1 || $1==2 {print}
$1==3 {for(i=2;i < NF; i++) a+=$i;max=i}
END {printf &quot;3&quot;;for(i=2; i < max; i++) printf &quot;|%d&quot;, a;printf &quot;|\n&quot;}
' a b > c
 
Thanks Ygor for the quick reply. Sorry, I did not understand the solution. I copied my test files in to a nad b files. I tried executing the above code on command line but got this error.

**********************
&quot;}awk: There is a regular expression error.
?, *, or + not preceded by valid regular expression
The input line number is 1. The file is b.
The source line number is 1.
****************************************

Where did it go wrong? thx.
 
Not sure why you are getting this error - the above code works on my version of awk. Maybe try....

awk -F\| '
NR==1 {print}
$1==2 {print}
$1==3 {for(i=2;i < NF; i++) a+=$i;max=i}
END {printf &quot;3&quot;;for(i=2; i < max; i++) printf &quot;|%d&quot;, a;printf &quot;|\n&quot;}
' a b > c
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top