[SOLVED] awk: how to achieve a 64character column file from a list of FORTH commands (and other things) ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
awk: how to achieve a 64character column file from a list of FORTH commands (and other things) ?
Hello,
I have following file which I want to be restructured as output (a Forth program working on a Debian 64bits cross-development platform)
Code:
3.14159265359e FCONSTANT PINB
FVARIABLE B
FVARIABLE A
FVARIABLE TV
1.0e FVARIABLE R
DECIMAL
: PERE12
B STO
." Par B: " F. CR CR
X=0?
IF F+
4.0E0
F*
." ELL PERI = " F. EXIT THEN
X<>Y
." Par A: " F. CR CR
A STO
X=0?
IF F+
4.0E
F*
." ELL PERI = " F. EXIT THEN
FENTER F*
X<>Y FENTER F*
F+ TV STO
FDROP
1.0E0 R STO
FDROP
BEGIN
A RCL FENTER
B RCL FENTER
RDN
F*
SQRT B STO
RDN
F-
CHS
R RCL
2.0e
F*
R STO
FDROP
LASTX
F/
A RCL
X<>Y
F- A STO
FDROP
B RCL
X<>Y
LASTX X^2
R RCL
F* TV RCL F-
CHS TV STO FDROP
X=Y? UNTIL
1/X
TV RCL
F*
PINB
F*
." ELL PERI = " F. CR ;
: TEST
3.0e
11.0e
PERE12 ;
and would like to have it reformatted with awk in following output
Code:
3.14159265359E0 FCONSTANT PINB
FVARIABLE B
FVARIABLE A
FVARIABLE TV
1.0E0 FVARIABLE R
DECIMAL
: PERE12 B STO ." Par B: " F. CR CR X=0? IF F+ 4.0E0 F*
." ELL PERI = " F. EXIT THEN X<>Y ." Par A: " F. CR CR A
STO X=0? IF F+ 4.0E0 F* ." ELL PERI = " F. EXIT THEN
FENTER F* X<>Y FENTER F* F+ TV STO FDROP 1.0E0 R STO FDROP
BEGIN A RCL FENTER B RCL FENTER RDN F* SQRT B STO RDN F- CHS R
RCL 2.0E0 F* R STO FDROP LASTX F/ A RCL X<>Y F- A STO FDROP B
RCL X<>Y LASTX X^2 R RCL F* TV RCL F- CHS TV STO FDROP X=Y?
UNTIL 1/X TV RCL F* PINB F* ." ELL PERI = " F. CR ;
: TEST 3.0E0 11.0E0 PERE12 ;
why awk? because the first file above is a result of a filter with awk therefore I would like to keep awk.
I achieved the second posted file with manual work. Thats ok: take not much time. But for the "beauty", it would be good if the output is achieved straight with awk.
Target:
a) rework all lines between ": " and " ;" only (for : and ; not within brackets "")
b) file output with with 64 characters columns
c) file recognizing 1.0e or 4.0E and changing it to e0 or E0 (= 1.0E0 or 4.0E0) with no number after "E" exponent of the number
d) .... (more to come.. but of lower priority)
Any advice is welcome. No hurry; manually is so far fine. Thanks to everyone.
Ahh.. perhaps you identified something I missed in my program.
The target was to initialize the float variable R with 1.0 but since you are asking, the correct would be "FVARIABLE R" only (no 1.0 before or after) since I initialize later to 1.0. Good remark/question. (however it worked, only it had the side effect I just understood now, a 1.0 appeared in the float stack). I am a beginner (this is the first Forth program in my life).
The following variant looks for the ending semicolon only in the formatted block, and then prints it unconditionally.
Code:
#!/usr/bin/awk -f
BEGIN{wrap=54}
/^:/{
line=""
flag=1
}
flag{
line=line $0
if (/;$/){
flag=0
print line
next
}
if (length(line)>wrap){
print line
line=""
}
next
}
{print}
Thanks. I am using it in a terminal. Good.
awk -f filter1 PERE12.fth > temp.txt
awk -f filter2 temp.txt > RESULT.SRC
Is there any possibility to make only 1 command out of the 2 above?
(filter2 is from the post above)
verbal description below (since the awk script is a problem under my account to be uploaded; as txt or as pdf; I receive the message "
Sorry, you have been blocked You are unable to access linuxquestions.org").
Code:
# This program helps for filtering commented files on a PC.
# The cleaned-up files with this script will be later uploaded
# into the HP programming board
# It will delete following type of comments:
# xxxx \ yyyy -> changed to xxxx (deleted from \ on)
# D_xxx in a line -> line deleted
# G_xxx in a line -> changed to xxx (G_ deleted)
# line with blanks -> whole line deleted
# " ( .... )" identified -> replaced by " "
# nothing in a line = only <cr> or <tab> -> whole line deleted
#
Last edited by floppy_stuttgart; 06-16-2023 at 12:07 PM.
Unfortunately for all of us, "FORTH" is a thoroughly obsolete technology which, much like "MUMPS," still refuses to die.
When "digital computers were still too weak to get out of their own way," these systems actually made sense. (And, yes, I definitely remember those days, entirely without(!) nostalgia.)
Last edited by sundialsvcs; 06-16-2023 at 01:50 PM.
Unfortunately for all of us, "FORTH" is a thoroughly obsolete technology which, much like "MUMPS," still refuses to die.
When "digital computers were still too weak to get out of their own way," these systems actually made sense. (And, yes, I definitely remember those days, entirely without(!) nostalgia.)
Perhaps you think that all current firmware is written in X86 assembler.
But let me cite some Web articles.
Quote:
Forth is still used today in many embedded systems (small computerized devices) because of its portability, efficient memory use, short development time, and fast execution speed.
Quote:
Forth has a niche in astronomical and space applications as well as a history in embedded systems. The Open Firmware boot ROMs used by Apple, IBM, Sun, and OLPC XO-1 contain a Forth environment. Forth has often been used to bring up new hardware.
# This program helps for filtering commented files on a PC.
# The cleaned-up files with this script will be later uploaded
# into the HP programming board
# It will delete following type of comments:
# xxxx \ yyyy -> changed to xxxx (deleted from \ on)
# D_xxx in a line -> line deleted
# G_xxx in a line -> changed to xxx (G_ deleted)
# line with blanks -> whole line deleted
# " ( .... )" identified -> replaced by " "
# nothing in a line = only <cr> or <tab> -> whole line deleted
#
I don't see why you cannot do both things with one awk script.
I like FORTH too, in the Pioneer days there were lots of interesting, small, efficient languages.
I have it on a Rasperry Pico at work to amuse myself while I am downloading Python frameworks for money.
Of course, you should really be writing the FORTH formatter in FORTH.
Please give examples for input and desired output!
Or copy/paste the relevant awk code.
Code:
: PERE12 ( r1=a r2=b -- r=perimeter )
\ X Y Z T L later on HP STACK
\ b a .. .. ..
B STO
\
." Param B: " G_F. CR CR \ optional Show X doesnt alter the content of X
." Optional which will be deleted later with awk script" B D_F.@ CR \ optional show the content of the variable
." line 131 in emacs of PERE12 " D_RPNS \ typical optional line to show the content of the stacks
\
X=0? \ assumption A & B in Y and X
\
IF G_F+
." line 136 " D_RPNS \ optional show the content of the stacks
4.0E0
\ ." line 138 " D_RPNS \ optional show the content of the stacks
G_F*
." ELLIPSE PERIM = " G_F. EXIT THEN
\ ." line 141 " D_RPNS \ optional show the content of the stacks
X<>Y
\ ." line 143 " D_RPNS \ optional show the content of the stacks
." Param A: " G_F. CR CR
A STO
\ A D_F.@ \ optional show the content of the variable
\
\ ." line 85 " D_RPNS
\
X=0? \ A B
IF G_F+
\ ." line 152 " D_RPNS \ optional show the content of the stacks
4.0E0
\ ." line 154 " D_RPNS \ optional show the content of the stacks
G_F*
\ ." line 156 " D_RPNS \ optional show the content of the stacks
." ELLIPSE PERIM = " G_F. EXIT THEN
\
\ ." line 159 " D_RPNS \ optional show the content of the stacks
\
FENTER G_F* \ A*A B
\
\ ." line 163 " D_RPNS \ optional show the content of the stacks
\
X<>Y FENTER G_F* \ B*B A*A
\
\ ." line 167 " D_RPNS \ optional show the content of the stacks
\
G_F+ TV STO \ A^2+B^2 (stored in TV)
\
\ ." line 171 " D_RPNS \ optional show the content of the stacks
\
G_FDROP \ why cleaning up the stack? necessary?
1.0E0 R STO
G_FDROP \ why cleaning up the stack? necessary?
\
BEGIN
A RCL FENTER \ An An
\ ." line 179 " D_RPNS \ optional show the content of the stacks
B RCL FENTER \ Bn Bn An An
RDN \ Bn An An Bn
\ ." line 182 " D_RPNS \ optional show the content of the stacks
G_F* \ Bn*An An Bn Bn
\ ." line 184 " D_RPNS \ optional show the content of the stacks
SQRT B STO \ SQRT(An*Bn)=BN An Bn Bn
\ ." line 186 " D_RPNS \ optional show the content of the stacks
RDN \ An Bn Bn BN
G_F- \ Bn-An Bn BN BN
\ ." line 189 " D_RPNS \ optional show the content of the stacks
CHS \ An-Bn Bn BN BN
\ ." line 191 " D_RPNS \ optional show the content of the stacks
R RCL \ Rn An-Bn Bn BN
\ ." line 193 " D_RPNS \ optional show the content of the stacks
2.0e0 \ 2.0e Rn An-Bn Bn
G_F* \ 2*Rn An-Bn Bn Bn
\ ." line 196 " D_RPNS \ optional show the content of the stacks
R STO \ 2*Rn=RN An-Bn Bn Bn
G_FDROP \ An-Bn Bn Bn Bn
\ ." line 199 " D_RPNS \ optional show the content of the stacks
LASTX \ 2.0e An-Bn Bn Bn
\ ." line 201 " D_RPNS \ optional show the content of the stacks
G_F/ \ (An-Bn)/2. Bn Bn Bn
\ ." line 203 " D_RPNS \ optional show the content of the stacks
A RCL \ An (An-Bn)/2 Bn Bn
\ ." line 205 " D_RPNS \ optional show the content of the stacks
X<>Y \ (An-Bn)/2 An Bn Bn
\ ." line 207 " D_RPNS \ optional show the content of the stacks
G_F- A STO \ (An+Bn)/2=AN Bn Bn Bn L..(An-Bn)/2.
\ ." line 209 " D_RPNS \ optional show the content of the stacks
G_FDROP \ Bn Bn Bn Bn
\ ." line 211 " D_RPNS \ optional show the content of the stacks
B RCL \ BN Bn Bn Bn
X<>Y \ Bn BN Bn Bn
\ ." line 214 " D_RPNS \ optional show the content of the stacks
LASTX X^2
\ ." line 216 " D_RPNS \ optional show the content of the stacks
R RCL \ RN ((An-Bn)/2.)^2 Bn BN
G_F* TV RCL G_F- \ -T+RN*((An-Bn)/2)^2 Bn BN BN
\ ." line 219 " D_RPNS \ optional show the content of the stacks
CHS TV STO G_FDROP \ Bn BN BN BN
." line 170 " D_RPNS \ optional show the content of the stacks
\ WTF?? \ this was included here for testing the awk script with tab in this file
\
X=Y? UNTIL
\ ." line 225 " D_RPNS \ optional show the content of the stacks
1/X
\ ." line 227 " D_RPNS \ optional show the content of the stacks
TV RCL
\ ." line 229 " D_RPNS \ optional show the content of the stacks
G_F*
\ ." line 231 " D_RPNS \ optional show the content of the stacks
PINB
\ ." line 233 " D_RPNS \ optional show the content of the stacks
G_F*
\ ." line 235 " D_RPNS \ optional show the content of the stacks
\
\ *********** OUTPUT the result *************
." ELLIPSE PERIM = " G_F. CR ;
For any reason the awk code cannot be uploaded between "code".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.