Библиотека сайта rus-linux.net
When a program performs operations on input and writes the result to the standard output, it is called a filter. One of the most common uses of filters is to restructure output. We'll discuss a couple of the most important filters below.
As we saw in Section 126.96.36.199, grep scans the output line per line, searching for matching patterns. All lines containing the pattern will be printed to standard output. This behavior can be reversed using the
Some examples: suppose we want to know which files in a certain directory have been modified in February:
The grep command, like most commands, is case sensitive. Use the
-i option to make no difference between upper and lower case. A lot of GNU extensions are available as well, such as
--colour, which is helpful to highlight searchterms in long lines, and
--after-context, which prints the number of lines after the last matching line. You can issue a recursive grep that searches all subdirectories of encountered directories using the
-r option. As usual, options can be combined.
Regular expressions can be used to further detail the exact character matches you want to select out of all the input lines. The best way to start with regular expressions is indeed to read the grep documentation. An excellent chapter is included in the grep Info page. Since it would lead us too far discussing the ins and outs of regular expressions, it is strongly advised to start here if you want to know more about them.
Play around a bit with grep, it will be worth the trouble putting some time in this most basic but very powerful filtering command. The exercises at the end of this chapter will help you to get started, see Section 5.5.
The command sort arranges lines in alphabetical order by default:
But there are many more things sort can do. Looking at the file size, for instance. With this command, directory content is sorted smallest files first, biggest files last:
-la | sort
|Old sort syntax|
You might obtain the same result with ls
The sort command is also used in combination with the uniq program (or sort
-u) to sort output and filter out double entries: