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

sorting columns of second file to the columns of the first

Status
Not open for further replies.

agilo

Programmer
Feb 4, 2004
73
TR
Hi,

I do have the following two files:

file1:

x1 x2 x3 x4
name1 33 4 54
name2 23 1 32
..................


file2:

x3 x4 x1 x2
23 2 xxx 223
21 3 yyy 23
...............

I want to sort the columns of file2 to the same order of the columns of file1 (according to the header of file1).

Can any body help...

Thanks in advance.
 
Try

awk -f agilo.awk file2

#agilo.awk
BEGIN {
getline < "file1"
split ($0,a)
}
NR==1{
for (j=1;j<=4;j++) {
for (k=1;k<=4;k++) {
if (a[j]==$k) {
n[k] = j
break
}
}
}
}
{
printf("%3s %3s %3s %3s\n", $n[1],$n[2],$n[3],$n[4])
}

CaKiwi
 
Thanks very much Cakiwi for your quick reply.. It is working fine, the only problem I have to count the number of cloumns in the file before running the program.

Agilo
 
Compare the return value of the split function with the actual value of the NF variable.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hello Guys,

I have modified the script of Cakiwi to be general (no limitation for the field number). The problem is that I do not get the correct out, the fields are spaced by tabs and the name of the field (first row) has some times two or three words (e.g., First Name or, Emp. Complete Address) space by one space.

Can any body guide me what is wrong in the attached script.

BEGIN {
FS = "\t"
getline < "file1"
split ($0,a)
}
NR==1{
for (j=1;j<=NF;j++) {
for (k=1;k<=NF;k++) {
if (a[j]==$k) {
n[k] = j
break
}
}
}
}
{

for (j=1;j<=NF;j++) {
printf $n[j] "\t"
}
print ""
}


Thanks very much in advance..
 
Have you tried this ?
split ($0,a,FS)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
thanks PHV,
I have tried split($0,a,FS) but it does not change any thing, I still get the same wrong output.

 
Hi,

I have sample files to show the problem I still have, the attached script still has problem which I can not fix, can any body help ..

file1:

x1 y1 x2 y2 x3 y3 x4 y4
name_xx1 33 4 54
name_xx2 23 1 32

file2:

x1 y1 x3 y3 x4 y4 x2 y2
23 2 lmn_sml 223
21 3 bmn_wvm 23

The output shows that the column x4 y4 of file2 is in the second place while it should be the last according to the header of file1.

here is the script:

BEGIN {
FS = "\t"
if (!fn) fn="file1"
getline < fn
split ($0,a,FS)
}
NR==1{
for (j=1;j<=NF;j++) {
for (k=1;k<=NF;k++) {
if (a[j]==$k) {
n[k] = j
break
}
}
}
}
{
for (j=1;j<=NF;j++) {
printf $n[j] "\t"
}
print ""
}



 
Replace this:
n[k] = j
By this:
n[j] = k

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks very much PHV, this fixed the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top