Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 235175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:14:41+00:00 2026-05-11T20:14:41+00:00

In bash, is there a way to chain multiple commands, all taking the same

  • 0

In bash, is there a way to chain multiple commands, all taking the same input from stdin? That is, one command reads stdin, does some processing, writes the output to a file. The next command in the chain gets the same input as what the first command got. And so on.

For example, consider a large text file to be split into multiple files by filtering the content. Something like this:

cat food_expenses.txt | grep "coffee" > coffee.txt | grep "tea" > tea.txt | grep "honey cake" > cake.txt

This obviously does not work, because the second grep gets the first grep’s output, not the original text file. I tried inserting tee’s but that does not help. Is there some bash magic that can cause the first grep to send its input to the pipe, not the output?

And by the way, splitting a file was a simple example. Consider splitting (filering by pattern search) a continuous live text stream coming over a network and writing the output to different named pipes or sockets. I would like to know if there is an easy way to do it using a shell script.

(This question is a cleaned up version of my earlier one , based on responses that pointed out the unclearness)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:14:41+00:00Added an answer on May 11, 2026 at 8:14 pm

    I like Stephen’s idea of using awk instead of grep.

    It ain’t pretty, but here’s a command that uses output redirection to keep all data flowing through stdout:

    cat food.txt | 
    awk '/coffee/ {print $0 > "/dev/stderr"} {print $0}' 
        2> coffee.txt | 
    awk '/tea/ {print $0 > "/dev/stderr"} {print $0}' 
        2> tea.txt
    

    As you can see, it uses awk to send all lines matching ‘coffee’ to stderr, and all lines regardless of content to stdout. Then stderr is fed to a file, and the process repeats with ‘tea’.

    If you wanted to filter out content at each step, you might use this:

    cat food.txt | 
    awk '/coffee/ {print $0 > "/dev/stderr"} $0 !~ /coffee/ {print $0}' 
        2> coffee.txt | 
    awk '/tea/ {print $0 > "/dev/stderr"} $0 !~ /tea/ {print $0}' 
        2> tea.txt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a bash command that takes as input a file path and returns
Is there a way to prevent a command from being added to the bash
Is there a way to run the BASH built-in commands from Python? I tried:
Is there a command within the bash shell of fedora that will give me
Are there any idioms for returning multiple values from a bash function within a
Is there a way in Bash to recall the argument of the previous command?
I'm using a pipe of several commands in bash. Is there a way of
Is there a way, I can execute in python a bash command with expansion:
Is there a way to debug a Bash script? E.g., something that prints a
Is there a way to print same character repeatedly in bash, just like you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.