Title here
Summary here
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 Swift that writes a capitalized version of all input text. You can use this pattern to write your own Swift line filters.
To try out our line filter, first make a file with a few lowercase lines.
Then use the line filter to get uppercase lines.
In this Swift version:
FileHandle.standardInput
to read from standard input.readLine()
function is used to read input line by line.while
loop with optional binding to process each line until we reach the end of the input.uppercased()
method is used to convert each line to uppercase.print()
to write the uppercased line to standard output.stdin.streamError
after the loop. However, note that in Swift, readLine()
doesn’t provide an error for end-of-file; it simply returns nil
.StandardError()
to print error messages to stderr.This Swift code provides equivalent functionality to the original, reading input line by line, converting each line to uppercase, and writing the result to standard output.