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

new question - awk 2

Status
Not open for further replies.

lambros

Programmer
Oct 10, 2002
42
0
0
US
I have two lists of numbers matching the line values I wish to match using awk.... How do I write a scritp to start at the values given by the list containing - ( These are the Values I need to match )

5103
1485
5663
4260

Now I need to decrement backwards until I hit the the closest value contained by the value in the list;

5087
2691
5691
4234
5256
4398
1978
5646
1467
5789

In other words I need to get the vale 5103 and match it to the line in the second list with the line number 5087.. Is this possible, given that these values will change everytime the server is recycled.....

Thanks for any help
Lambros.
 
let me get this straight, you want it to show you lines:
5103 to 5087,
1485 to 1467,
5663 to 5646,
4260 to 4234
?

if so, do they have to be in the order i've just said, or could they be:
1485 to 1467,
4260 to 4234,
5103 to 5087,
5663 to 5646
?

also do the lists ever overlap?
5103, 5203, 1485, 4260
would show :
1485 to 1467,
4260 to 4234,
5203 to 5087
?
 
Yeah, perhaps it was a little incomprehensible.... sorry, I'll try again... I'm going some testing, and I wish to find out the best way to match the following line line containing the following strings -

/Error/ and /Invalid/ - as this is the easiest thing to match. When I have found these lines - There should be several lines corresponding to these two words in the file, I wish to go backwards & find the closest line containing the word /200/, I'm trying to do this with the following code;

{
if ( $0 ~ /Invalid/ && $0 ~ /Error/){
for (i = 1; i <= NF; i++)
arrerr[$0] = FNR;
}
}

{
if ( $0 ~ /200/){ arr[$0]=FNR }
}


This assigns a number to the line with /Invalid/ and /Error/ and also to the line with /200/, now I hope to take the number given by the arrerr and match it to the closest value in arr - using some kind of loop I imagine...., if this is possible.... There's probably a better way to do this & am open to suggestions.

Thanks for any help.
L.
 
well tail and head are the quickest and easiest way once you have the line numbers.

to get the line numbers you could sort list 1 with highest at the top,
step through the second list sorted with highest first,
find the first result in list 2 that is less than the search in list 1, store both values (2 arrays might be useful). then at the END {} output the system commands using 'tail -<start_number> <filename> | head -<end_number-start_number>' for each line in the array.

do you want to have a go, or shall i write you something?
 
If I could get something to start with, I'd hope to take it from there.... I'm still learning this stuff & am not to good with loops & stuff yet....

L.
 
#twolist.awk
function findval(v1,l2) {
m = split(l2,array)
prev = 1000000
for (i=1 ; i <= m ; i++) {
#print m, &quot;num elements&quot;, &quot;value=&quot;,array
if ((v1 - array) < prev && (v1 - array) > 0) {
#print (v1 - array), &quot;difference&quot;, v1, array
prev = (v1 - array)
tmp = array
#print prev
}
}
return prev&quot; &quot;tmp
}

BEGIN {

list1 =&quot;5103 1485 5663 4260&quot;
list2 =&quot;5087 2691 5691 4234 2691 5691 1978 1467 5789&quot;

split(list1,arra1)
for (all in arra1) {
print &quot;Looking up arra1[all]&quot;
print &quot;Result for&quot;,arra1[all], &quot;is :&quot;, findval(arra1[all],list2)
}
}

This was fun to write and took only a few seconds.
Heres the OP:
awk -f twolist.awk
Result for 4260 is : 26 4234
Result for 5103 is : 16 5087
Result for 1485 is : 18 1467
Result for 5663 is : 576 5087
 
Thanks Marsd,

I'll play with it a bit.. one question - what is the syntax of the

END
{
'tail -<start_number> <filename> | head -<end_number-start_number>'

}

i've tried;

system(&quot;tail etc...&quot;)

but script hangs...
 
system(&quot;head -&quot;v1&quot; <filename> | tail -&quot;prev);

or something similar at the end of findval ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top