Difference between revisions of "Piping and redirecting output from one command or file to another"

From CUC3
Jump to navigation Jump to search
import>Mp466
import>Lb415
m
 
Line 1: Line 1:
 
Piping entails sending standard output to the standard input of a linux command.
 
Piping entails sending standard output to the standard input of a linux command.
 
Pipes are the primary power of using the command line. They can be used with sed, awk, perl, and python
 
Pipes are the primary power of using the command line. They can be used with sed, awk, perl, and python
amoungst other tools to produce very specific behavior. To pipe the output to an new command place a '|'
+
amongst other tools to produce very specific behavior. To pipe the output to a new command place a '|'
 
character between them.
 
character between them.
   
Line 21: Line 21:
   
 
Another way of piping is to set up a pipe that can persist outside of a specific command line.
 
Another way of piping is to set up a pipe that can persist outside of a specific command line.
This can also be used to montor calculations that are in progress.
+
This can also be used to monitor calculations that are in progress.
   
 
For example (from wikipedia), one can create a pipe and set up gzip to compress things piped to it:
 
For example (from wikipedia), one can create a pipe and set up gzip to compress things piped to it:

Latest revision as of 12:23, 29 October 2008

Piping entails sending standard output to the standard input of a linux command. Pipes are the primary power of using the command line. They can be used with sed, awk, perl, and python amongst other tools to produce very specific behavior. To pipe the output to a new command place a '|' character between them.

Examples

command line pipe

wc -l * | sort -rn

pipes in for loops

 for f in `ls -al | awk '{print$9}' | sort`; 
 do 
    echo $f; 
    cp $f $f.bak; 
 done

named pipes

Another way of piping is to set up a pipe that can persist outside of a specific command line. This can also be used to monitor calculations that are in progress.

For example (from wikipedia), one can create a pipe and set up gzip to compress things piped to it:

mkfifo my_pipe
gzip -9 -c < my_pipe > out.gz
rm my_pipe

In a separate process shell, independently, one could send the data to be compressed:

cat file > my_pipe