GTU OS Program - 11 Filters
11. Shell programming using filters (including grep, egrep, fgrep).
The programs that take plain text as standard input and transforms it into a meaningful format, and then returns it as a standard output is known as Filters.
Some of the most commonly used filters are explained below:
- cat
- head
- tail
- sort
- uniq
- wc
- grep
- tac
- sed
- nl
2. head: Displays the first n lines of the specified text files. If the number of lines is not specified then by default prints first 10 lines.
Syntax
head [-number_of_lines_to_print] [path]
Output
3. tail: It works the same way as head, but in reverse order. It returns the lines from bottom to up
Syntax
tail [-number_of_lines_to_print] [path]
Output
4. sort: sorts the lines alphabetically by default but we can customize the way of sorting.
Syntax
sort [-options] [path]
Output
5. uniq: removes duplicate lines. it has a limitations that it can only remove continuous duplicate lines.
Syntax
uniq [options] [path]
6. wc: It gives the number of lines, words and characters in the data.
Syntax
wc [-options] [path]
Output
wc gives 4 outputs as
- number of lines
- Number of words
- Number of characters
- path
7. grep: grep is used to search a particular information from a text file.
Syntax
grep [options] pattern [path]
Output
9. Sed: It stands for stream editor. It allows us to apply search and replace operation on our data effectively.
Syntax
sed [path]
The expression we have used below is of the form
's/search/replace/g'
Output
In the above output we replaced cat with the text "this is replaced text"
Comments