Re: Sort across two or more files





pk wrote:

On Thursday 30 October 2008 18:54, Quis custodiet ipsos custodes wrote:

OK, let's say we have three files: names, phone and age

[me@work /] cat names
Adam
Bob
Clare

[me@work /] cat phone
1234567
2345671
3456781

[me@work /] cat age
25
21
23

[me@work /] sort -n age
21
23
25

I now want to maintain the references between Names/Phone numbers/Ages
so that I don't end up just shuffling the data. (i.e. the Names file
gets changed to Bob-Clare-Adam, ditto the phone numbers file)

How do I accomplish this?

You might be ok with just

paste age names phone | sort -n -k1,1 | \
awk '{print $1>"age.new";print $2>"names.new";print $3>"phone.new"}

mv age.new age
mv names.new names
mv phone.new phone

Yes, ATM I'm doing it in a similar way although I was wondering if a
more elegant solution existed. Note that the columns in age, names and
phone may vary so instead I write it this way in a C++ program...

ofstream write_file;
system("pr -m -t data1.txt data2.txt data3.txt | sort +0 -1 -g | uniq
buffer3");
system("grep -v Data buffer3 > buffer3b; mv buffer3b buffer3");
create_script.open("temp_script3");
create_script << "awk '{print $1";
write_file.open("buffer3b");
write_file << "Data 1" << "\n";
write_file.close();
write_file.open("buffer3c");
write_file << "Data 2" << "\n";
write_file.close();
write_file.open("buffer3d");
write_file << "Data 3" << "\n";
write_file.close();
for (i = 1; i < columns1; i++)
{
create_script << ", $" << i;
}
create_script << "}' buffer3 >> buffer3b" << "\n";
create_script << "awk '{print $" << columns1+1;
for (i = columns1+2; i < columns1+columns2+1; i++)
{
create_script << ", $" << i;
}
create_script << "}' buffer3 >> buffer3c" << "\n";
create_script << "awk '{print $" << columns1+columns2+1;
for (i = columns1+columns2+2; i < columns1+columns2+columns3+1; i++)
{
create_script << ", $" << i;
}
create_script << "}' buffer3 >> buffer3d" << "\n";
create_script << "mv buffer3b data1.txt; mv buffer3c data2.txt; mv
buffer3d data3.txt" << "\n";
create_script.close();
system("chmod 700 temp_script3; ./temp_script3; rm -f temp_script3");
.



Relevant Pages