Line Filters in Fortran
Here’s the translation of the Go line filter program to Fortran, along with explanations in Markdown format suitable 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 Fortran that writes a capitalized version of all input text. You can use this pattern to write your own Fortran line filters.
This Fortran program reads lines from standard input, converts them to uppercase, and writes them to standard output. Here’s a breakdown of how it works:
We use the
iso_fortran_env
module to get standard input, output, and error units.We use a
do
loop to continuously read lines from standard input.The
read
statement reads a line into theline
variable. Theiostat
parameter allows us to check for end-of-file or errors.If we’ve reached the end of the file (io_status < 0), we exit the loop.
If there’s an error reading (io_status > 0), we print an error message to stderr and stop the program.
We call the
to_upper
subroutine to convert the line to uppercase.We write the uppercased line to standard output.
The
to_upper
subroutine uses ado
loop andselect case
to convert each lowercase letter to uppercase.
To try out our line filter, first make a file with a few lowercase lines.
Then compile and use the line filter to get uppercase lines.
This Fortran version achieves the same functionality as the original program, reading lines from standard input, converting them to uppercase, and writing them to standard output.