Line Filters in Scilab

Our line filter program reads input from stdin, processes it, and then prints a derived result to stdout. This example writes a capitalized version of all input text. You can use this pattern to write your own Scilab line filters.

// Read input from stdin
lines = mgetl();

// Process each line
for i = 1:size(lines, 1)
    // Convert to uppercase
    upper_line = convstr(lines(i), 'u');
    
    // Write out the uppercased line
    mprintf('%s\n', upper_line);
end

// Check for errors during reading
if status <> 0 then
    mprintf('error: %s\n', lasterror());
    exit(1);
end

In Scilab, we use the mgetl() function to read all lines from standard input. We then process each line using a for loop, converting it to uppercase with the convstr() function. The mprintf() function is used to write the uppercased line to stdout.

To try out our line filter, first make a file with a few lowercase lines:

$ echo 'hello'   > /tmp/lines
$ echo 'filter' >> /tmp/lines

Then use the line filter to get uppercase lines:

$ cat /tmp/lines | scilab -nwni -f line-filters.sce
HELLO
FILTER

Note that in Scilab, we run scripts using the scilab command with the -nwni option (No Window, No Interactive mode) and the -f option to specify the script file.

This Scilab version provides similar functionality to the original Go program, reading input lines, converting them to uppercase, and printing the result. The error handling is simplified, as Scilab’s mgetl() function doesn’t provide the same level of granular error reporting as Go’s bufio.Scanner.