Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: revise the third column accroding to formula.

4 views
Skip to first unread message

Kenny McCormack

unread,
Apr 30, 2011, 8:25:00 AM4/30/11
to
In article <ipgt50$kl$7...@aspen.stu.neva.ru>,
Hongyi Zhao <hszh...@gmail.com> wrote:
...
>According to your and Ed's code, I write the following ones to do my job:
>
>------------
>awk '
> /^%block AtomicCoordinatesAndAtomicSpecies/ { nr = 0 }
> { nr++ }
> (nr > 16) && (nr <= 96) { $3 = sprintf("%.9f",$3 + ($3 -
>30.8202973324/7)*0.01) }
> (nr > 96) { $3 = sprintf("%.9f",$3 +
>(30.8202973324*5/7)*0.01) }
> { print }
>' ./POSITIONS.fdf
>------------
>
>The contents in the POSITIONS.fdf is the stuff I've posted in the top of
>this thread.

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 -

Hongyi Zhao

unread,
Apr 30, 2011, 9:38:48 AM4/30/11
to
On Sat, 30 Apr 2011 12:25:00 +0000, Kenny McCormack wrote:

> 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 :.

Kenny McCormack

unread,
Apr 30, 2011, 9:55:46 AM4/30/11
to
In article <iph3d7$8cn$2...@aspen.stu.neva.ru>,

Hongyi Zhao <hszh...@gmail.com> wrote:
>On Sat, 30 Apr 2011 12:25:00 +0000, Kenny McCormack wrote:
>
>> 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?

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.

Hongyi Zhao

unread,
May 1, 2011, 10:50:55 PM5/1/11
to
On Sat, 30 Apr 2011 13:55:46 +0000, Kenny McCormack wrote:

> 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

--

Janis Papanagnou

unread,
May 2, 2011, 2:42:46 AM5/2/11
to
On 02.05.2011 04:50, Hongyi Zhao wrote:
> On Sat, 30 Apr 2011 13:55:46 +0000, Kenny McCormack wrote:
>
>> 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?

Condition 'true' (with default action { print }).

Kenny McCormack

unread,
May 2, 2011, 7:40:58 AM5/2/11
to
In article <ipl66e$o3q$1...@aspen.stu.neva.ru>,

Hongyi Zhao <hszh...@gmail.com> wrote:
>On Sat, 30 Apr 2011 13:55:46 +0000, Kenny McCormack wrote:
>
>> 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?

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

Aharon Robbins

unread,
May 2, 2011, 6:22:35 PM5/2/11
to
In article <ipm58a$ic5$1...@news.xmission.com>,

Kenny McCormack <gaz...@shell.xmission.com> wrote:
>In article <ipl66e$o3q$1...@aspen.stu.neva.ru>,
>Hongyi Zhao <hszh...@gmail.com> wrote:
>>On Sat, 30 Apr 2011 13:55:46 +0000, Kenny McCormack wrote:
>>
>>> 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?
>
>It means the same thing as {print}
>but saves a little typing.

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

Janis Papanagnou

unread,
May 3, 2011, 8:28:42 AM5/3/11
to
Am 03.05.2011 00:22, schrieb Aharon Robbins:
> In article<ipm58a$ic5$1...@news.xmission.com>,
> Kenny McCormack<gaz...@shell.xmission.com> wrote:
>> In article<ipl66e$o3q$1...@aspen.stu.neva.ru>,
>> Hongyi Zhao<hszh...@gmail.com> wrote:
>>> On Sat, 30 Apr 2011 13:55:46 +0000, Kenny McCormack wrote:
>>>
>>>> 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?
>>
>> It means the same thing as {print}
>> but saves a little typing.
>
> And in my humble opinion (not that anyone cares) is absolutely miserable
> programming practice. If you mean { print } then write { print }.

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.

Ed Morton

unread,
May 3, 2011, 8:55:46 AM5/3/11
to
On 5/2/2011 5:22 PM, Aharon Robbins wrote:
> In article<ipm58a$ic5$1...@news.xmission.com>,
> Kenny McCormack<gaz...@shell.xmission.com> wrote:
>> In article<ipl66e$o3q$1...@aspen.stu.neva.ru>,
>> Hongyi Zhao<hszh...@gmail.com> wrote:
>>> On Sat, 30 Apr 2011 13:55:46 +0000, Kenny McCormack wrote:
>>>
>>>> 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?
>>
>> It means the same thing as {print}
>> but saves a little typing.
>
> 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.

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.

0 new messages