留下INSPECT yourtext REPLACING BY SPACES
空间。INSPECT yourtext REPLACING BY ""
是不允许的(两者需要具有相同的长度,或者替换标识符是一个象征性常量:SPACE[S]、ZERO[S|ES] 或 QUOTE[S]) - 如果您尝试,一个好的编译器将输出一条可以理解的消息这。
只剩下3个选择:
不要使用 COBOL(如果您不想对文本文件执行任何其他操作,这将是#1 选项),而是使用系统调用,例如sed -e 's/#RT#//g' yourfile > yourtarget
,取决于您使用的 COBOL 运行时(始终是一个好主意在问题中添加此信息!)您可以通过 COBOL 启动该过程CALL "SYSTEM" USING external-command
。
READ
数据,使用特定于您的运行时的扩展来转换它(例如使用 GnuCOBOL: MOVE FUNCTION SUBSTITUTE (yourtext, '#RT#', '', '#LT#'. '') TO translated-text
) - 可能是FUNCTION
一个系统库CALL
- 并将WRITE
数据返回。
古老的 COBOL 方式 - 见下文。
由于问题不是关于读取数据或将其写回,这只是替换部分:
选项 a):UNSTRING 语句
MOVE 0 TO t1, t2, t3, t4
UNSTRING yourtext
DELIMITED BY ALL '#RT#' OR
ALL '#LT#' OR
...
INTO target-1 COUNT IN t1
target-2 COUNT IN t2
target-3 COUNT IN t3
target-4 COUNT IN t4
...
END-UNSTRING
MOVE SPACES TO translated-text
STRING target-1 (1:t1)
target-2 (1:t2)
target-2 (1:t2)
target-2 (1:t2)
...
DELIMITED BY SIZE INTO translated-text
END-STRING
Run Code Online (Sandbox Code Playgroud)
选项b) 一个简单的PERFORM VARYING
带有两个指针的,结合一个简单的IF
语句。
*> you may get more performance if you `REDEFINE` source-text as a `PIC X OCCURS length-of-text TIMES` - but I find this one more better to read and it shouldn't consume much more time...
MOVE 0 TO target-pointer
PERFORM VARYING source-pointer
FROM 1 BY 1
UNTIL source-pointer > length-of-text
IF source-text (source-pointer:1) = '#'
*> a very good optimizer would calculate the constant on the
*> right side, you may write it directly
IF source-pointer + 4 > length-of-text
ADD 1 TO target-pointer
MOVE source-text (source-pointer:)
TO target-text (target-pointer:)
EXIT PERFORM
END-IF
IF source-text (source-pointer:4) = '#RT#' OR '#LT#' OR ...
ADD 4 TO source-pointer
EXIT PERFORM CYCLE
END-IF
END-IF
ADD 1 TO target-pointer
MOVE source-text (source-pointer:1)
TO target-text (target-pointer:1)
END-PERFORM
Run Code Online (Sandbox Code Playgroud)