Maybe you want to use the "range pattern" syntax, like this:
/^%block AtomicCoordinatesAndAtomicSpecies/,/^%endblock AtomicCoordinatesAndAtomicSpecies/ {
if (++nr > 16)
$3 = sprintf("%.9f",$3 + (nr > 96 ? ($3 - 30.8202973324/7)*0.01 : ($3 - 30.8202973324/7)*0.01))
}
1
Note: Above assumes block occurs only once (as seems to be the case for OP).
If you want to deal with multiple such blocks, you will need to figure out a
way to reset nr to 0 once you get past (each occurrence of) the block.
--
Faced with the choice between changing one's mind and proving that there is
no need to do so, almost everyone gets busy on the proof.
- John Kenneth Galbraith -
> Maybe you want to use the "range pattern" syntax, like this:
>
> /^%block AtomicCoordinatesAndAtomicSpecies/,/^%endblock
> AtomicCoordinatesAndAtomicSpecies/ {
> if (++nr > 16)
> $3 = sprintf("%.9f",$3 + (nr > 96 ? ($3 - 30.8202973324/7)*0.01 :
($3 -
> 30.8202973324/7)*0.01))
> }
> 1
I use the following code according to your advices, these code are saved
in a file named positions.awk):
awk '
/^%block AtomicCoordinatesAndAtomicSpecies/,/^%endblock
AtomicCoordinatesAndAtomicSpecies/ {
if (++nr > 16)
$3 = sprintf("%.9f",$3 + (nr > 96 ? ($3 - 30.8202973324/7)*0.01 :
($3 - 30.8202973324/7)*0.01))
}
' ./POSITIONS.fdf
Then I invoke positions.awk as follows but give me onthing:
werner@debian:~$ ./positions.awk
werner@debian:~$
Why?
>
> Note: Above assumes block occurs only once (as seems to be the case for
> OP).
> If you want to deal with multiple such blocks, you will need to figure
> out a way to reset nr to 0 once you get past (each occurrence of) the
> block.
Regards.
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
2 things:
1) The last line of the file should be 1 (just a simple digit 1), not "'
./POSITIONS.INF" (Whatever that's supposed to be...)
2) You should invoke it from the shell as: awk -f file.awk file.dat
Also, it seems this line:
/^%block AtomicCoordinatesAndAtomicSpecies/,/^%endblock AtomicCoordinatesAndAtomicSpecies/ {
was split into 2 lines in my post (no doubt because of my editor settings).
Anyway, it needs to be all one single line.
--
> No, I haven't, that's why I'm asking questions. If you won't help me,
> why don't you just go find your lost manhood elsewhere.
CLC in a nutshell.
> 2 things:
> 1) The last line of the file should be 1 (just a simple digit 1),
What's the meaning of 1 used here? Any hints?
> not "'
> ./POSITIONS.INF" (Whatever that's supposed to be...)
> 2) You should invoke it from the shell as: awk -f file.awk file.dat
--
Condition 'true' (with default action { print }).
It means the same thing as {print}
but saves a little typing.
--
Is God willing to prevent evil, but not able? Then he is not omnipotent.
Is he able, but not willing? Then he is malevolent.
Is he both able and willing? Then whence cometh evil?
Is he neither able nor willing? Then why call him God?
~ Epicurus
And in my humble opinion (not that anyone cares) is absolutely miserable
programming practice. If you mean { print } then write { print }.
The next guy who has to read your code and who never saw awk will thank you.
Sheesh.
--
Aharon (Arnold) Robbins arnold AT skeeve DOT com
P.O. Box 354 Home Phone: +972 8 979-0381
Nof Ayalon Cell Phone: +972 50 729-7545
D.N. Shimshon 99785 ISRAEL
ITYM: "If you mean { print $0 } then write { print $0 }." ;-)
(In the past newbies also wondered why a plain 'print' doesn't print
an empty line, and you need, e.g., print "" for that purpose.
Just BTW, since below you mention a guy "who never saw awk". :-)
The problem with the "1" in Kenny's program was that it's easy to get
overlooked beneath the three preceding lines with those very bulky
rexexp constants.
For one-(or few-)liners on the command line the '1' is advantageous
to know and use. Have you ever typed on a, say, german keyboard,
where you need that <Alg Gr>-<{> keys to access those curly brackets?
My 2 cents.
>
> The next guy who has to read your code and who never saw awk will thank you.
There's an inherent logical flaw in the situation described by this
sentence. (I agree that in practice it occasionally happens anyway.)
Janis
>
> Sheesh.
As Janis pointed out, if you're catering to the next guy who doesn't know awk
then you should probably go with { print $0 } since that's what you really mean.
Of course if a goal is to make the script readable by someone who never saw awk,
then you should replace code like this:
awk '1' file
with:
awk 'BEGIN{
while ( (getline line < "file") > 0 ) {
print line
}
}'
as that's clearer to that target demographic.
FWIW I use the "1" idiom for 2 reasons:
1) it's brief.
2) it forces people to learn/know about both awks condition/action language
structure and its default action so it's a good, simple learning tool for some
key concepts and once you understand those key concepts then the meaning of "1"
is obvious.
Regards,
Ed.