Line Filters in COBOL
Here’s the translation of the Go line filter program to COBOL, formatted in Markdown for Hugo:
A line filter is a common type of program that reads input on stdin, processes it, and then prints some derived result to stdout. grep
and sed
are common line filters.
Here’s an example line filter in COBOL that writes a capitalized version of all input text. You can use this pattern to write your own COBOL line filters.
In this COBOL program:
We define the input and output files as STANDARD-INPUT and STANDARD-OUTPUT, which correspond to stdin and stdout in other languages.
We use a FILE SECTION to define the structure of our input and output lines.
The main procedure opens the input and output, processes lines until we reach the end of the file, then closes the files.
The PROCESS-LINES paragraph reads a line from the input. If it’s not the end of the file, it capitalizes the line and writes it to the output.
The CAPITALIZE-LINE paragraph uses the COBOL intrinsic function UPPER-CASE to convert the input line to uppercase.
To try out our line filter, first make a file with a few lowercase lines.
Then use the line filter to get uppercase lines. Assuming your COBOL program is compiled into an executable named ’line-filter':
Note that the exact command to run the COBOL program may vary depending on your COBOL compiler and operating system. You might need to set up the appropriate environment for file handling as well.