Filter Commands in Linux and Unix Operating Systems

Filter Commands in Linux and Unix Operating Systems.

A filter accepts input from the Standard Input, processes the input and then sends the output to the Standard Output. Filters also take input from a file.

Filters are used to extract the lines, which contain a specific pattern, to arrange the contents of a file in a sorted order, to replace the existing characters with other characters, etc. Filters are also used to store the intermediate results of a long pipe. We can extract specific columns of a file and can merge two or more files together using filters.

head – output the first part of files

syntax:

=> $head -<n> <filename>

=>$<command >| head

Note : By Default first 10 lines will displayed

 

tail – output the last part of files

=> $ tail  -<n> <filename>

=>  $<command >|tail

Note : By Default first 10 lines will displayed

wc – print newline, word, and byte counts for each file.

Syntax :

$wc <file>

options:

=> ( l ) – Number of lines in a file
=> ( w ) – Number of words in a file
=>( c ) – Number of character counts in a file

Possible combinations of options

=> ( lw ) : gives the output of number of lines and number of words
=>( wc ) : gives the output of number of words and number of characters count
=>( lc ) : gives the number of lines and number of characters

grep – print lines that match patterns

It is used to search for regular expression/word/string/pattern/name and print from the file/and other command output.

Now, we can find one specific line in a file using grep filter command

 

Few optionsof grep command:

-v Displays only those lines that do not match the specified pattern.
-c Displays only the count of the lines that match the specified pattern.
-n Displays those lines the match the specified pattern along with the line number at the beginning of the line.
-i Displays those lines that match the specified pattern ignoring the case distinction.

 egrep – extension to grep command

sort – sort lines of text files

The -r Option

  • When the -r option is used with the sort command it will display the input taken from the keyboard in the reverse alphabetical order.
  • The -f OptionAs a rule the digits, alphabets and other special characters that are taken in as input are converted to their ASCII value. Then sort arranges the input according to their ASCII value.The ASCII values for A to Z are lesser than that of a to z. Hence there might be unpredictable results.
  • The -n OptionWe have seen that the sort command arranges the numbers, alphabets and special characters according to their ASCII value. The ASCII value of 10 for example is lesser than that of 2 and hence 10 will be displayed above 2, which is not the correct order.

    The sort -n option will arrange the input according to numerical value and then display it

  • The +pos1 -pos2 OptionLet us assume that we have a file containing the complete names of people in the following order – first name, middlename lastname. The three names are separated by a single space. Each column is called a field. If we want to sort the names on any particular field, then the “+pos1 -pos2” option of sort can be used.
  • The -t OptionThe default field separator for sort is a blank space or a tab space. But if the field separator is going to be any character other than a blank space or tab, then it can be specified along with the sort command using the -t option.
  • The -u Option =>This option will remove duplicate lines from the input and display the output.
  • The -o OptionThe output of the sort command is sent to the Standard Output. Using this sort option we will be able to redirect the sorted contents to the file mentioned.
  • The -b OptionSince the sort command uses the ASCII value of the input character, any leading blanks in the input will cause undesired results. To overcome this problem the sort -b option is used along with the +pos -pos option.

tr – translate or delete characters

The tr command reads standard input and, for each input character, maps it to an alternate character, deletes the character, or leaves the character alone. The output is written to the standard output.

The output of several commands is separated from each other by more than a single space. This command when used with the “-s” option squeezes multiple spaces into a single space.

uniq – report or omit repeated lines

The different options of the uniq command are:

 

-d Displays only the lines that are duplicated in the input file.
-u Displays only the lines with single occurrences.
-c Precedes each line displayed by the number of times it occurs.

cut – remove sections from each line of files

One particular field from any file or from output of any command can be extracted and displayed using the cut command. A single character can also be extracted by using the -c option of the cut command.

option  “-d” represents the delimiter or the field separator. The option “-f” is followed by the list of fields that have be to displayed.

See also: