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= "/tmp/hold.$$"
echo -n "Ide device to test: "
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] > "/tmp/srcfile/"
}
}' $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 "(\[^a-z|A-Z\])" $string name t
set final [concat $istring=$t]
set srcfile [open "/tmp/srcfile" 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.
The data is output(see below)in a paragraph each field is separated by a comma retty typical output.
I have tried split:
split($0, arr, ","
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 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, ","
for (i in arr)
if (arr ~ /pat/) {
mode[var] = arr
delete arr
}
for (j in arr)
etc..
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 "select" command using AWK, which works like a "select" command of SQL on "plain text files". 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-)
The system command hdparm outputs
a "," 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, ","
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.