Pipe & Redirection

Bash-script-21

Difference between Pipe & Redirection

Pipe is used to pass output to another Program or Utility.

  • thing1 | thing2
    • thing1’s output s will be passed to things2 Program or Utility.

Redirect is used to pass output to either a File or Stream.

  • thing1 > thing2
    • thing1’s outputs will be placed in a file called thing2.

 

Pipe

With Pipe, the standard output of one command is fed into the standard input of another.

$ ls | grep "java"

It will find all the file names from ls that contain “java” string.

 

Redirection

Standard output directs its contents to the display. To redirect standard output to a file, the “>” character is used.

$ ls > file_list.txt

In this example, all the file names from ls command will be written in an file named file_list.txt. Since the output of ls was redirected to the file, no results on the display. Each time the command above is repeated, file_list.txt is overwritten (from the beginning) with the output of the command ls. If you want the new results to be appended to the file instead, use “>>“.

$ ls >> file_list.txt

 

Standard Input gets its contents from the keyboard, but like standard output, it can be redirected. To redirect standard input from a file instead of the keyboard, the “<” character is used.

$ sort < file_list.txt

Sort command print the contents of file_list.txt. We could redirect standard output to another file.

$ sort < file_list.txt > sorted_file_list.txt

As you can see, a command can have both its input and output redirected. The redirection operators (the “<” and “>”) must appear after the other options and arguments in the command.

 

Filters

  • sort: Sorts standard input then outputs the sorted result on standard output.
  • uniq: Given a sorted stream of data from standard input, it removes duplicate lines of data (i.e., it makes sure that every line is unique).
  • grep: Examines each line of data it receives from standard input and outputs every line that contains a specified pattern of characters.
  • fmt: Reads text from standard input, then outputs formatted text on standard output.
  • pr: Takes text input from standard input and splits the data into pages with page breaks, headers and footers in preparation for printing.
  • head: Outputs the first few lines of its input. Useful for getting the header of a file.
  • tail: Outputs the last few lines of its input. Useful for things like getting the most recent entries from a log file.
  • tr: Translates characters. Can be used to perform tasks such as upper/lowercase conversions or changing line termination characters from one type to another (for example, converting DOS text files into Unix style text files).
  • sed: Stream editor. Can perform more sophisticated text translations than tr.
  • awk: An entire programming language designed for constructing filters. Extremely powerful.

 

Leave A Comment

Your email address will not be published. Required fields are marked *