You are on page 1of 10

Chapter 12 Pipes

Why Use Pipelines?
Pipelines allow you to transfer the output of one command directly as the input of another command

Example:
$ ps –f | more

$ ls | more
$ ls | sort –r | more $ who > who.out $ sort < who.out who | sort

Redirection in a Pipeline
$ grep one myfile | sort > sorted.users $ grep one < myfile 2> grep.err | sort > sorted.users 2> sort.err $ grep one < myfile | sort 2> sort.err | wc –l > wc.out 2> wc.err

cut
Cuts out specified columns or fields of text from standard input or a file and display the result to stdout. The -c option is for cutting columns, and the -f is for cutting fields.
Syntax:

cut –clist [file. . .] cut –flist [-dchar][-s][file. . .]
Examples:

$ date | cut –c1-3 $ tail -1 myfile

$ cut –f1, 6 –d: myfile
$ cut –f1, 6 –d: myfile | sort –r $ ps –ef | cut –c49- | sort -d

Cont.
A-B A-B A, B Fields or Columns A through B inclusive Fields or Columns A through the end of the line Beginning of line through field or column B Fields or columns A and B

*any combination of the above is also permissible Example:

$ cut –f1,3,5-7 myfile

tr
Translates characters. It can be used to convert many consecutive blank spaces to a single blank space. IT can be used to substitute literal strings or convert text from lowercase to uppercase and vise versa.
Syntax:

$ tr [-s] [string1][string2]
Examples:

$ who | tr –s “ “ $ date | cut –c1-3 | tr “[:lower:]” “[:upper:]”

tee
Reads from standard input and writes its output to standard output and to the specified file Syntax:

tee [-a] file [file. . .]
Example: $ who | $ who | $ who | $ who | $ who |
sort tee unsorted | sort tee unsorted | sort | tee sorted wc -l tee whoson.txt | wc -l

pr
Print to stdout; it is used to format the standard input stream or the contents of spedified files. It sends its output to the screen, not the printer. Useful for printing long files because it will insert a header on the top of each new page that includes the file name, and a page number.
Syntax:

pr [-option] [file . . . ]
Examples:

$ pr –n3 funfile
$ pr –n3 funfile | more $ ls | pr -3

The pr command supports many options: -k -a -t -d -wN -lN -h -p produces k-column output; prints down the column produces multicolumn output; used with -k; prints across removes the trailer and header doublespaces the output sets the width of a line to N characters sets the length of a page to N lines. users the following string as the header text pauses and waits for return before each page

Printing from a Pipeline
. . . | lp

Examples:

$ pr -158 funfile | lp