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

Reading file to array 1

Status
Not open for further replies.

demis001

Programmer
Aug 18, 2008
94
US
$XXXX00000182183
A B C D
2336458 0.38052728 0.47743206 0.7575053 -0.6718711
2336459 0.28057157 0.15925342 1.6066561 -0.6718711
2336464 0.51131811 0.09128078 2.0090009 -0.6718711
2336466 -0.24900861 0.13113013 -1.7475722 -0.6718711
2336469 0.38724031 0.06652635 2.2380376 -0.6718711
2336472 -0.38239292 0.21324028 -1.3923107 -0.6718711

$XXXX00000162378
A B C D
2336498 0.49046121 0.040488739 2.6031871 -0.4343451
2336499 1.52994741 0.042017215 2.5756016 -0.4343451
2336502 -0.15919230 0.647693047 -0.4807927 -0.4343451
2336503 0.24834931 0.153163959 1.6350111 -0.4343451
2336505 -0.09731513 0.754664392 -0.3271534 -0.4343451

I want to reads the above data to array using "$XXXX*" as index of an array; I don't know how to do it in awk. Please, would you help?

Here is what I want, convert the above data to matrix by ignoring line 2 with letter header.
XXXX00000182183 2336458 0.38052728 0.47743206 0.7575053 -0.6718711
XXXX00000182183 2336459 0.28057157 0.15925342 1.6066561 -0.6718711
XXXX00000182183 2336464 0.51131811 0.09128078 2.0090009 -0.6718711
XXXX00000182183 2336466 -0.24900861 0.13113013 -1.7475722 -0.6718711
XXXX00000182183 2336469 0.38724031 0.06652635 2.2380376 -0.6718711
XXXX00000182183 2336472 -0.38239292 0.21324028 -1.3923107 -0.6718711

Then, get the line from the list if abs($3) is maximum number

XXXX00000182183 2336464 0.51131811 0.09128078 2.0090009 -0.6718711

Thanks
 
Hi

demis001 said:
I want to reads the above data to array using "$XXXX*" as index of an array
Why ?
demis001 said:
Here is what I want, convert the above data to matrix by ignoring line 2 with letter header.
Code:
awk 'NF==1{x=$1}NF==5{print x,$0}' /input/file
demis001 said:
Then, get the line from the list if abs($3) is maximum number
You forgot to specify whether you need the maximum line from the file (1) or from each section (2).
Code:
[gray]# (1)[/gray]
awk 'NF==1{x=$1}NF==5&&abs($2)>m{m=abs($2);l=x OFS $0}END{print l}function abs(n){return n<0?-n:n}' /input/file

[gray]# (2)[/gray]
awk 'NF==1{if(l)print x,l;x=$1;m=l=""}NF==5&&abs($2)>m{m=abs($2);l=$0}END{print x,l}function abs(n){return n<0?-n:n}' /input/file
Tested using [tt]gawk[/tt] and [tt]mawk[/tt].


Feherke.
 
Fehrke,

Thank you as always!!

Both options produced the result I wanted. But I have a difficaulty to construe the code.

Thanks
 
Hi Fehrke,

Here is the logic which I don't understand on the second code
abs($2)>m{m=abs($2)? why >?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top