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 two files and create third file from them... 1

Status
Not open for further replies.

CaKiwi

Programmer
Apr 8, 2001
1,294
US
I'm assuming you made a mistake with the output you say you want. The following awk program
Code:
# a.awk
BEGIN {
  FS = "|"
  OFS = "|"
  while ((getline < &quot;file2&quot;) > 0) {
    j++
    a1[j] = $1
    sub(/^[^\|]*/,&quot;&quot;,$0)
    a0[j] = $0
  }
}
{
  for (i=1;i<=j;i++) {
    if ($1 == a1[i]) {
      print $2 &quot; &quot; a0[i]
    }
  }
}
gives this output:

a | 1 | 2 | 3 | 4|
a | 3 | 4 | 5 | 6|
b | 1 | 2 | 3 | 4|
b | 3 | 4 | 5 | 6|
c | 1 | 2 | 3 | 4|
c | 3 | 4 | 5 | 6|
d | 7 | 8 | 9 | 10|
d | 11 | 12 | 13 | 14|
e | 7 | 8 | 9 | 10|
e | 11 | 12 | 13 | 14|

Run it by entering

awk -f a.awk file2 > file3

If this is not want you want, you will need to post a further explanation. CaKiwi
 
Thanks CaKiwi,

That was indeed very stupid on my part. Yes, you very rightly corrected the output I want.

I have two questions -
a) What happens to file1 ? I mean, program doesn't mention about it anywhere in the code. Neither does it get called during awk -f command....

b) I see 'file2' inside the program. Are we hardcoding the name of file2?

Thanks for your help.
Sachin
 
Sorry, I should have said run it by entering

awk -f a.awk file1 > file3

I hard coded file2 in the program. You could use stdin by changing the getline to be

while ((getline < &quot;-&quot;) > 0) {

and running it by entering

awk -f a.awk file1 < file2 > file3

Or pass filename in as a variable

while ((getline < fn2) > 0) {

awk -f a.awk -v fn2=file2 file1 > file3
CaKiwi
 
Boss,

Donn't know how to thank you.

Earlier, I had written a Pl/SQL script that was suppose to do same thing - it used to upload data from two files - merge them and used to move the output to another table - Then, I used to run another Pl/SQL procedure to extract data out from the table. Was really cumbursome process.

But not now..... Thanks to you :)

I owe you a drink.

Cheers,
Sachin
 
&quot;Boss&quot;,
get a star! vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Someone should start a &quot;beerpal&quot; service so I can take up these drink offers!

Vlad, thanks for the star. CaKiwi
 
Hi CaKiwi,

Is it possible to re-arrange the columns in the output file... ?

Ofcourse, I could can re-arrange columns in File2 itself.... But, that file comes from another team..... And you know what it takes to get things done from another team. So, would be really great if I could re-arrange them in the output file (File3).

File 2 contains seven columns. (Col1, Col2, Col3, Col4, Col5, Col6, Col7). Still, comparision happens for Col1 only.

But in the output, File 3 is re-formatted, and it should look like - (Col2, Col3, Col1, Col4, Col5, Col6, Col7)

Note - All columns are pipe seperated....

Thanks for yopur help.
Sachin
 
This not tested but should get you started. I changed the variable i to k so I could highlight the changed areas.

BEGIN {
FS = &quot;|&quot;
OFS = &quot;|&quot;
while ((getline < &quot;file2&quot;) > 0) {
j++
a1[j] = $1
sub(/^[^\|]*./,&quot;&quot;,$0)
a0[j] = $0
}
}
{
for (k=1;k<=j;k++) {
if ($1 == a1[k]) {
split(a0[k],b)
print b[1] FS b[2] FS $2 FS b[3] FS b[4] FS b[5] FS b[6]

}
}
} CaKiwi
 
Bingo !

It worked as required.

Thanks a ton man. You have been a great help :)

Cheers,
Sachin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top