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

sort problem

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
Apologies if this is a dumb question..


awk {
for(x=0 ; x <= NR ; x++)

if ($x ~ /pat1/) {
mode[pat] = $x
}
}

What if I want to continue here and look for additional
patterns using the x var?
How is this done?

Thank You.
 
Not quite sure what you mean. Post some sample input data and tell us what you want to get from it. Just looking at your program, it seems that you want x=1 not x=0 and NF not NR in the for loop and you want to index mode by pat1 not pat. You can add another if statement and look for each field in another pattern, if that is what your question means e.g.

awk {
for(x=1 ; x <= NF ; x++)

if ($x ~ /pat1/) {
mode[pat1] = $x
}
if ($x ~ /pat2/) {
mode2[pat2] = $x
}

}
 
Right, you got it.
But, I do not get a satisfactory return.
Maybe it is just the data format or the regexp.

Details:

#!/bin/sh
hold= &quot;/tmp/hold.$$&quot;

echo -n &quot;Ide device to test: &quot;
read dev
/sbin/hdparm -i $dev > $hold
awk ' {
for(x=0 ; x <= NR ; x++)
if ($x ~ /DMA/) {
mode[dma] = $x
}
} else if ($x ~ /MaxMult/) {
mode[max] = $x
} else if ($x ~ /BuffSize/) {
mode[buff] = $x
}
for (j in mode)
if (length(mode[j]) > 4) {
print mode[j] > &quot;/tmp/srcfile/&quot;
}
}' $hold

I always end up with a missing element, and even with
parsing the string length, I continue to encounter this problem.

The regexp matches, it just does not return a value from the array sort.

I could do it in tcl but it takes forever and I have to
proc prod_istring...
use regexp &quot;(\[^a-z|A-Z\])&quot; $string name t
set final [concat $istring=$t]
set srcfile [open &quot;/tmp/srcfile&quot; w+], etc...
blah...awk is much more succinct if I can get it to work.
 
I'm still not quite sure what you are trying to do. Post a short sample from the hold file you create with hdparm and an example of the output you want.

CaKiwi
 
I think you're missing a &quot;{&quot; for the outter &quot;for&quot; loop.



awk ' {
for(x=0 ; x <= NR ; x++) { # <--- missing
if ($x ~ /DMA/) {
mode[dma] = $x
}
} else if ($x ~ /MaxMult/) {
 
The data is output(see below)in a paragraph each field is separated by a comma :pretty typical output.
I have tried split:
split($0, arr, &quot;,&quot;)
for (x in arr)
if (arr[x] ~ /BuffSize/) {
etc .. but to no avail, an element
does not match, or at least is not returned, or
another regexp returns twice...

OUTPUT:
hdparm program output sample line:
BuffSize=2048, UDMA=yes, OldDMA=no, DMA=yes, DblWordIO=no,
etc...Seems very simple but something is not quite right with it.

If anyone has seen an example of a successful sort of this type of data could they just recommend a source I can look at?

Thanks for your help,
M
 
once again - I think you're missing a &quot;{&quot; for the &quot;for&quot; loop.
Check your &quot;{}&quot; for BALANCE and &quot;scoping&quot;.

vlad
 
Thanks vlad. I have never had a problem
before with a for loop and nested ifs without
a {} and it does not seem to be the case this time.
Umm. I got it to work using multiple for
loops through the array with different
indexes.

split($0, arr, &quot;,&quot;)
for (i in arr)
if (arr ~ /pat/) {
mode[var] = arr
delete arr
}
for (j in arr)
etc..

Live and learn.
Thanks for the help.


 
Marsd

As Cakiwi had suggested to post 1. sample input data and 2. Sample expected output from the program. You have given sample of input data only :-(. Please post the sample output data, which will help everybody to understand your problem. Finally what solution you have found is still not clear to me. I wish, I could give a solution. I have worked a lot on AWK and developed a &quot;select&quot; command using AWK, which works like a &quot;select&quot; command of SQL on &quot;plain text files&quot;. I have made two version of it UNIX and DOS. Yes, it is true. Shall contact later. Bye for now. --P C Das (India). s-)
 

ANy chance of enlightening the crowd on &quot;select&quot; - would be curious to see

vlad
 
Okay..

The system command hdparm outputs
a &quot;,&quot; delimited paragraph of plain text.

Sample line:
MaxMultSect=16, DMA=yes, ATA=ATA1 ATA2 ATA3
There are 8 or 9 of these lines,all similar.

With my sort , depending on whether or not
certain regexps were matched I would get one OR the other pattern matches:
example:

split($0, arr, &quot;,&quot;)
for (x in arr)
if (arr[x] ~ /BuffSize/) {
mode[buff] = arr[x]
}
this would return BufferSize=2048, as
expected.

However if I included THIS pattern afterwards
for (j in arr)
if (arr[j] ~ /MaxMult/) {
mode[sect] = arr[j]
)
only one or the other would return a value.

There were multiple for loops in my solution,
indexing patterns and building the array.
Then when printing all the array vars:

for (i in mode)
print mode

I would get either MaxMult=16 OR
BufferSize=2048, depending on which was
sorted first These patterns were located on the same line of the paragraph, but were separated by a comma. All the other patterns returned predictable results=all matches of the pattern in the array var. I also tried earlier with nested ifs, which did not work well at all, which was the reason for my original inquiry.

Even though I reported the problem fixed it
recurred when I tried mixing and matching these two patterns again.
I hope this tells you what was going on.

I am having better results with an expect script right now on this problem but am still
interested in a solution for the other half
finished awk/shell script.

Thank You.
MD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top