Thursday, 25 August 2011

Pipes

A pipe is a way to connect the output of one program to the input of another program without any temporary file.
Pipe - Redirecting output of 1st command to 2nd without creating temporary file
Pipe Defined as:
"A pipe is nothing but a temporary storage place where the output of one command is stored and then passed as the input for second command. Pipes are used to run more than two commands ( Multiple commands) from same command line."
Syntax:
command1 | command2

Filter

If a Linux command accepts its input from the standard input and produces its output on standard output is know as a filter. A filter performs some kind of process on the input and gives output. For e.g.. Suppose you have file called 'hotel.txt' with 100 lines data, And from 'hotel.txt' you would like to print contains from line number 20 to line number 30 and store this result to file called 'hlist' then give command:
$ tail +20 < hotel.txt | head -n30 >hlist
Here head command is filter which takes its input from tail command (tail command start selecting from line number 20 of given file i.e. hotel.txt) and passes this lines as input to head, whose output is redirected to 'hlist' file.
Consider one more following example
$ sort < sname | uniq > u_sname
Here uniq is filter which takes its input from sort command and passes this lines as input to uniq; Then uniqs output is redirected to "u_sname" file.

 Example :-
-bash-3.00$ ls -lrt
total 5828
-rwxrwxrwx   1 qa1      qa       1350256 Jun 11  2010 opx_PAR_ERI_720
-rwxrwxrwx   1 qa1      qa       1597852 Jun 22 03:20 opx_PAR_ERI_6as
-rwxrwxrwx   1 qa1      qa            91 Aug  8 07:16 Din
-rwxrwxrwx   1 qa1      qa            26 Aug  8 07:19 sname
-bash-3.00$ for i in $(ls -lrt)
> do
> echo $i
> done
total
5828
-rwxrwxrwx
1
qa1
qa
1350256
Jun
11
2010
opx_PAR_ERI_720
-rwxrwxrwx
1
qa1
qa
1597852
Jun
22
03:20

-bash-3.00$ IFS="
> "
-bash-3.00$ for i in $(ls -lrt); do echo $i; done
total 5828
-rwxrwxrwx   1 qa1      qa       1350256 Jun 11  2010 opx_PAR_ERI_720
-rwxrwxrwx   1 qa1      qa       1597852 Jun 22 03:20 opx_PAR_ERI_6as
-rwxrwxrwx   1 qa1      qa            91 Aug  8 07:16 Din
-rwxrwxrwx   1 qa1      qa            26 Aug  8 07:19 sname
-rwxrwxrwx   1 qa1      qa            26 Aug  8 07:21 sorted_name
-rwxrwxrwx   1 qa1      qa            26 Aug  8 07:23 cap_names
-rwxrwxrwx   1 qa1      qa            26 Aug  8 07:26 new_sorted_names
-rwxrwxrwx   1 qa1      qa            44 Aug  8 07:50 er



No comments:

Post a Comment