Send stdout to Multiple Commands


Introduction

When it comes to programming, there are numerous occasions when we need to execute multiple commands and take input or output from a single command. In such cases, sending output of a command to multiple commands becomes an essential task.

Sending stdout to multiple commands is one of most frequently used techniques in programming. In this article, we will discuss what stdout is and how we can send it to multiple commands with examples. So, let’s get started.

What is stdout?

Stdout (Standard Output) is a default stream in Unix-like operating systems, which is used to display output of a program on screen. When you run a program on command line, it may output something on screen using stdout. It is a way of communicating between program and user.

Sending stdout to multiple commands

Now that we understand what stdout is, let’s dive into how we can send it to multiple commands. There are different ways to send stdout to multiple commands, such as using pipes, process substitution, and tee command. Let’s discuss each one of them in detail.

Using Pipes

One of most common ways of sending stdout to multiple commands is by using pipes. Pipes are used to redirect output of one command as input to another command. pipe symbol | is used to connect two commands.

Let’s consider an example. Suppose we have a file called test.txt, and we want to display contents of file and count number of lines simultaneously. We can use following command −

$ cat test.txt | wc -l

In above command, cat command displays contents of file, and pipe symbol | redirects output to wc -l command. wc -l command counts number of lines in file and displays output on screen.

Similarly, we can use multiple pipes to redirect output to multiple commands. For example −

$ cat test.txt | grep "hello" | wc -l

In above command, cat command displays contents of file, and output is redirected to grep command using pipe symbol |. grep command searches for word “hello” in file and redirects output to wc -l command. wc -l command counts number of lines containing word “hello” in file.

Process Substitution

Process substitution is another way of sending stdout to multiple commands. Process substitution is a way of using a command’s output as input for another command. Process substitution is denoted by <(command) or >(command)

Let’s consider an example. Suppose we have a file called test.txt, and we want to display contents of file and count number of lines simultaneously. We can use following command −

$ wc -l <(cat test.txt)

In above command, cat command displays contents of file, and process substitution operator −(command) redirects output of cat command to wc -l command. wc -l command counts number of lines in file and displays output on screen.

Similarly, we can use process substitution to send stdout to multiple commands. For example −

$ grep "hello" <(cat test.txt) >(tee hello.txt)

In above command, cat command displays contents of file, and process substitution operator <(command) redirects output of cat command to grep command. grep command searches for word “hello” in file and redirects output to process substitution operator >(command). >(command) redirects output of grep command to tee command. tee command saves output of grep command to a file called hello.txt and displays output on screen.

Tee Command

The Tee command is another way of sending stdout to multiple commands. tee command reads standard input and writes it to both standard output and one or more files. Tee command is denoted by symbol | tee.

Let’s consider an example. Suppose we have a file called test.txt, and we want to display contents of file and save output to another file called output.txt simultaneously. We can use following command −

$ cat test.txt | tee output.txt

In above command, cat command displays contents of file, and pipe symbol | redirects output to tee command. tee command saves output of cat command to a file called output.txt and displays output on screen.

Similarly, we can use tee command to send stdout to multiple commands. For example −

$ cat test.txt | tee >(grep "hello") >(wc -l)

In above command, cat command displays contents of file, and pipe symbol | redirects output to tee command. tee command redirects output of cat command to process substitution operator >(grep "hello"). >(grep "hello") redirects output of tee command to grep command, which searches for word “hello” in file. tee command also redirects output of cat command to process substitution operator >(wc -l). >(wc -l) redirects output of tee command to wc -l command, which counts number of lines in file.

Benefits of sending stdout to multiple commands

Sending stdout to multiple commands is useful in many scenarios. It allows us to manipulate and analyze data more effectively. By sending output of one command to multiple commands, we can perform multiple tasks on same data without need to store intermediate results in a file. This saves time and disk space.

Moreover, sending stdout to multiple commands enables us to create complex workflows, where output of one command becomes input of another command. This makes it easy to process large volumes of data and automate tasks.

Examples

Let’s look at some more examples to illustrate concept of sending stdout to multiple commands.

Example 1: Sorting and Counting Unique Words in a File

Suppose we have a file called sample.txt, and we want to sort words in file and count number of unique words. We can use following command −

$ cat sample.txt | tr '[:upper:]' '[:lower:]' | tr -s ' ' '
' | sort | uniq | wc -l

In above command, cat command displays contents of file, and output is redirected to tr command. tr command translates all uppercase letters to lowercase and replaces all spaces with newlines. output of tr command is then sorted using sort command. sorted output is then passed to uniq command, which removes all duplicate lines. Finally, output of uniq command is passed to wc -l command, which counts number of unique lines.

Example 2: Extracting Data From a Log File

Suppose we have a log file called access.log, and we want to extract IP addresses of all visitors to our website. We can use following command −

$ grep -oE "\b([0-9]{1,3}.){3}[0-9]{1,3}\b" access.log | sort | uniq

In above command, grep command searches for IP addresses in access.log file using a regular expression. output of grep command is then sorted using sort command, and all duplicate lines are removed using uniq command. This gives us a list of unique IP addresses.

Example 3: Analyzing Disk Usage

Suppose we want to analyze disk usage of a directory and its subdirectories. We can use following command −

$ du -h /path/to/directory | sort -hr | head -n 10 | tee disk_usage.txt

In above command, du command calculates disk usage of directory and its subdirectories. output of du command is then sorted by size using sort command. head command displays top 10 lines of sorted output. Finally, tee command saves output to a file called disk_usage.txt and displays output on screen.

Conclusion

In conclusion, sending stdout to multiple commands is a crucial task in programming. We discussed three different ways of sending stdout to multiple commands, including pipes, process substitution, and tee command, with examples. Each method has its advantages and disadvantages, and choice of method depends on specific use case. I hope this article helps you understand concept of sending stdout to multiple commands and how to use it in your programs.

Updated on: 23-Mar-2023

255 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements