how to use pipes + redirection to create two files?

# this works
# creates two.txt
(echo one;echo two;echo three;) | (
( grep two > two.txt )
)

# wamt this to create two files:
# two.txt containing "two" and three.txt containing "three"
# Both files get created, but three.txt is empty
# is there a way to do this?
(echo one;echo two;echo three;) | (
( grep two > two.txt )
( grep three > three.txt )
)

Thanks for helping me learn unix scripting!

how about:

(echo "one"; echo "two"; echo "three") | grep -e "two" -e  "three"
(echo "one"; echo "two"; echo "three")|xargs -i ksh -c '[[ ! -z $(echo {}|grep two) ]] && echo {}|grep two >2.txt;[[ ! -z $(echo {}|grep three) ]] && echo {}|grep three >3.txt'

Thanks klashxx. I had to replace "ksh" with bash and it worked for me.

It's pretty hard for me to understand, but I will study it and work on my bash fu :slight_smile: