LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-03-2023, 06:33 AM   #1
floppy_stuttgart
Senior Member
 
Registered: Nov 2010
Location: EU mainland
Distribution: Debian like
Posts: 1,156
Blog Entries: 5

Rep: Reputation: 107Reputation: 107
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.
 
Old 06-03-2023, 07:23 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
What is this line supposed to do?
Code:
1.0E0 FVARIABLE R
I would guess it does the same as
Code:
FVARIABLE R ( ___ )
1.0E0       ( ___ 1.0 )

Last edited by NevemTeve; 06-03-2023 at 10:13 AM.
 
1 members found this post helpful.
Old 06-03-2023, 10:13 AM   #3
floppy_stuttgart
Senior Member
 
Registered: Nov 2010
Location: EU mainland
Distribution: Debian like
Posts: 1,156

Original Poster
Blog Entries: 5

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by NevemTeve View Post
What is this line supposed to do?
Code:
1.0E0 FVARIABLE R
I would guess it does the same as
Code:
FVARIABLE R
1.0E0
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).
 
Old 06-13-2023, 06:35 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Perhaps not quite what you wanted, but could be the first step
Code:
#!/usr/bin/awk -f
BEGIN{wrap=54}
/^:/{
        line=""
        flag=1
}
flag{
        line=line $0
        if (length(line)>wrap){
                print line
                line=""
                next
        }
}
!flag{print}
/;$/{
        flag=0
        if (length(line)<=wrap) print line
}
 
Old 06-13-2023, 12:54 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
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}
 
2 members found this post helpful.
Old 06-16-2023, 10:10 AM   #6
floppy_stuttgart
Senior Member
 
Registered: Nov 2010
Location: EU mainland
Distribution: Debian like
Posts: 1,156

Original Poster
Blog Entries: 5

Rep: Reputation: 107Reputation: 107
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)
 
Old 06-16-2023, 10:21 AM   #7
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
What does filter1 do?
 
Old 06-16-2023, 12:04 PM   #8
floppy_stuttgart
Senior Member
 
Registered: Nov 2010
Location: EU mainland
Distribution: Debian like
Posts: 1,156

Original Poster
Blog Entries: 5

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by shruggy View Post
What does filter1 do?
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.
 
Old 06-16-2023, 01:48 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,691
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.
 
Old 06-17-2023, 01:26 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Quote:
Originally Posted by sundialsvcs View Post
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.
 
Old 06-17-2023, 04:47 AM   #11
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Quote:
Originally Posted by floppy_stuttgart View Post
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
#
I don't see why you cannot do both things with one awk script.
 
Old 06-17-2023, 11:49 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Quote:
" ( .... )" identified -> replaced by " "
Please give examples for input and desired output!
Or copy/paste the relevant awk code.

Last edited by MadeInGermany; 06-17-2023 at 01:02 PM.
 
Old 06-19-2023, 05:12 AM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
After studying the forth description in Wikipedia I have made the following:
Code:
#!/usr/bin/awk -f
BEGIN { wrap=64 }

# Delete \ comment
{ sub(/\\ .*/, "") }

# Delete line that starts with D_
$1 ~ /^D_/ { next }

# Delete a G_ prefix
$1 ~ /^G_/ { sub(/G_/, "") }

# Delete ( Comment )
{ gsub(/[(] [^)]* [)]/, "") }

# Delete blank line
/^[[:space:]]*$/ { next }

# Format : block ;
$1 == ":" {
  line=""
  flag=1
}
flag {
  nline=(line $0)
  if (length(nline) > wrap) {
    print line
    line=$0
  } else {
    line=nline }
  if ($NF == ";") {
    flag=0
    print line
  }
  next
}
{ print }
It also has the line wraps at 64 boundary as required.
 
Old 06-21-2023, 07:37 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
1 members found this post helpful.
Old 06-23-2023, 11:13 AM   #15
floppy_stuttgart
Senior Member
 
Registered: Nov 2010
Location: EU mainland
Distribution: Debian like
Posts: 1,156

Original Poster
Blog Entries: 5

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by MadeInGermany View Post
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".

So far, now I am using a script progfilter.sh
Quote:
#!/bin/bash
awk -f hp71bccfilter FILEIN.fth > temp.txt
awk -f forthbuild64column temp.txt > FILEOUT.SRC
And I am happy. Could be in one command instead of 2 commands.. but whatever, it works therefore I move to other tasks.

Last edited by floppy_stuttgart; 06-23-2023 at 11:19 AM.
 
  


Reply

Tags
awk



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Concat one column values in loop bashed on other column Snehasish Linux - Newbie 1 04-01-2019 08:55 AM
If the 1th column of file f1 and file f2 is the same, then export those line with maximum string of 2nd column weichanghe2000 Programming 1 04-26-2018 12:42 PM
[SOLVED] AWK - How to parse a Web log file to count column and the last occurrence of that column Alvin88 Linux - Newbie 10 06-23-2017 05:59 AM
[SOLVED] AWK fill column from previuos line column akeka Programming 4 01-30-2013 07:16 PM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration