in reply to Re: Running a string through a filter program and recieving the output as a string
in thread Running a string through a filter program and recieving the output as a string

"What exactly is the tr/sed command doing?"

From unix man page, in a nut shell:

"Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors."
"The tr utility copies the standard input to the standard output with substitution or deletion of selected characters. The options specified and the string1 and string2 operands control translations that occur while copying characters and single-character collating elements."
  • Comment on Re^2: Running a string through a filter program and recieving the output as a string

Replies are listed 'Best First'.
Re^3: Running a string through a filter program and recieving the output as a string
by ikegami (Patriarch) on Sep 30, 2005 at 04:55 UTC
    I think he meant in this case specifically, since Perl can do tr and sed tasks quite easily.
      Actually I just used tr and sed as examples. I want to use a different filter program but similar in that it takes input as stdin and outputs to stdout.
        Well, now you're still just being too vague. Does this "different" filter program depend critically on reading exactly one word per run? If so, I think you should look for a better implementation of whatever this program does.

        If, like most well-written filter programs, it accepts a stream of one or more lines of text, does its work one line at a time, and outputs each line as it finishes, then you can easily set up a process using Perl to feed it and collect its output for further operations. In this case, it's just a matter of making sure you feed it properly.

        For example, if it accepts one word per line, does some transformation and outputs a line of data for each input word, then a perl snippet like this would be one "easy" way to handle the job for a given data file:

        # open a pipeline in which a perl one-liner feeds # word-tokenized data from $file to "word_filter"; # the FILT file handle is then used to read the output # of word_filter (one line per word): open( FILT, "perl -pe 's/^\\s*//; s/\\s+/\\n/g' $file | word_filter |" + ) or die "subshell: $!"; ## note the "\\" in the one-liner while (<FILT>) { # $_ holds one line of output from "word_filter" # so do something with that... }