Linux - Piping
To connect the stdout of one program to the stdin of another program, in *nix we use a pipe (the output flows out one program through the pipe to the input of the other program). The pipe symbol is | and the way to send the output of one program to the input of another program is program1 | program2. For example, to display a long listing of the files in a directory and sort them based on the time the file was last modified,
$ ls -l | sort +7
The program sort sorts its input and sends the sorted output to stdout. The option +7 tells sort to sort the input file based on the data in field 7 which in the ls -l output is the column which contains the timestamp. To sort on size try ls -l | sort + 4. To display the contents of a directory and display the output using less,
$ ls -l | less
As you can see, this is similar to I/O redirection, but we are not dealing with reading and writing to files, but rather with programs that interact in some way. IO redirects and piping can be used in conjunction in the same command as well.
$ func1 < file1.txt | less
The above command will take file1.txt as input to func1 and then pipe the output of func1 to less to display to the screen.
Previous - Concatenating and Appending Files | Next - Info Command
No comments:
Post a Comment