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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

merge files 2

Status
Not open for further replies.

idiotbrother24

Technical User
Mar 3, 2003
1
US
I've got several files I need to manipulate.
file Grid:
1 4562.853
2 6782.875
3 1234.890
4 8763.659
5 1235.812
6 1895.893
7 1234.543

file Disp
3 8.02
2 1.01
5 4.21
4 6.72
1 5.43

I'd like to find the occrances of column 1 in file Disp in file Grid and create a third file which looks like this:

output: 3rdfile

1 4562.853 5.43
2 6782.875 1.01
3 1234.890 8.02
4 8763.659 6.72
5 1235.812 4.21


If you guys could help me out that would be fantastic.
 
Try this. The -v fn="Disp" is only needed if you want to use a file other than Disp for the second file.

awk -v fn="Disp" -f ib.awk Grid > 3rdfile

# ------ ib1.awk ------
BEGIN {
if (!fn) fn="Disp"
while ((getline < fn) > 0) {
a[$1] = $2
}
}
{
if (a[$1]) print $0, a[$1]
} CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
If both of your files are sorted you could just use the &quot;join&quot; command...
[tt]
$ sort -o Disp Disp
$ join Grid Disp
1 4562.853 5.43
2 6782.875 1.01
3 1234.890 8.02
4 8763.659 6.72
5 1235.812 4.21
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top