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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 108k
  • Answers 108k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Within your log4j.xml configuration, you can create two appenders, each… May 11, 2026 at 9:15 pm
  • Editorial Team
    Editorial Team added an answer OK, so let's assume the following (forgive me for re-clarifying… May 11, 2026 at 9:15 pm
  • Editorial Team
    Editorial Team added an answer So you want to capture every click? Event the right… May 11, 2026 at 9:15 pm

Related Questions

Is there a way to implement/use lambda functions in bash? I'm thinking of something
Problem: Customer X is a Windows user who wants to be able to trigger
I have the following way to submit a job with cluster using qsub: Submitting
In bash, calling foo would display any output from that command on the stdout.
I need to set an environment variable in Python and find the address in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.