What are filters?
Filters are a very important category of programs. What is common between them is how they work with input and output. This is what makes them very flexible and they can be combined on one line to make a powerful program.
On a historical note, most of the userland programs in the original Unix system were filters and this has to do with the philosophy of the Unix operating system.
How they interpret arguments.
cat interprets all the arguments as files. If there are no arguments, then it will read from STDIN till it receives an EOF (usually ctrl-D typed by the user.) It can accept any number of arguments along with specific flags.
sed is quite the same, but it interprets the first argument as a set of commands. The shell passes single-quoted strings as a single argument. It interprets further arguments as input files. If there are no further arguments, then it will read from STDIN
Input from a file
If the input has to come from a file, then it is very simple. We have three possibilities :
# the input file is an argument to the command
grep word file1.txt file2.txt file3.txt
# the input file is streamed into the command
cat file.txt | grep word
# input redirection
grep word < file.txt
Input file name omitted
# read from user input
grep word
# read from a pipe
echo "string" | grep word
No comments:
Post a Comment