Multiple command redirection

Multiple command redirection

am 19.09.2007 21:37:48 von wimxa

Can this be done as one-liner in bash:
- Find all files (e.g. with find)
- For each found file:
---- Print the name of the found file
---- Print the contents of the found file
in that order. E.g.:

$ls .
file1 file2 file3

$
file1:

file2:

file3:


This is a more general question regarding piping output to multiple
commands at once, in some order (but even without that it could be
helpful). This can easily be solved by making a separate script, but
often I have the need to find and do different operations, so it makes
the script usage rather limited (by the number of times used).

Re: Multiple command redirection

am 19.09.2007 21:41:08 von Ed Morton

wimxa@yahoo.com wrote:

> Can this be done as one-liner in bash:
> - Find all files (e.g. with find)
> - For each found file:
> ---- Print the name of the found file
> ---- Print the contents of the found file
> in that order. E.g.:
>
> $ls .
> file1 file2 file3
>
> $
> file1:
>
> file2:
>
> file3:
>
>
> This is a more general question regarding piping output to multiple
> commands at once, in some order (but even without that it could be
> helpful). This can easily be solved by making a separate script, but
> often I have the need to find and do different operations, so it makes
> the script usage rather limited (by the number of times used).
>

find . -type f -print -exec cat {} \;

Ed.

Re: Multiple command redirection

am 20.09.2007 06:00:43 von Barry Margolin

In article <1190230668.597692.106090@22g2000hsm.googlegroups.com>,
wimxa@yahoo.com wrote:

> Can this be done as one-liner in bash:
> - Find all files (e.g. with find)
> - For each found file:
> ---- Print the name of the found file
> ---- Print the contents of the found file
> in that order. E.g.:
>
> $ls .
> file1 file2 file3
>
> $
> file1:
>
> file2:
>
> file3:
>
>
> This is a more general question regarding piping output to multiple
> commands at once, in some order (but even without that it could be
> helpful). This can easily be solved by making a separate script, but
> often I have the need to find and do different operations, so it makes
> the script usage rather limited (by the number of times used).

command1 | while read line; do command2 "$line"; command3 "$line"; done

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Re: Multiple command redirection

am 20.09.2007 09:39:50 von Stephane CHAZELAS

2007-09-20, 00:00(-04), Barry Margolin:
[...]
> command1 | while read line; do command2 "$line"; command3 "$line"; done

Generally speaking, "read" without "-r" and without removing the
whitespace character from $IFS doesn't read a line of input into
a variable.

cmd1 | while IFS= read -r line; do cmd2 -- "$line"; cmd3 -- "$line"; done

Also note that if the output of cmd1 doesn't end in a newline
character, you'll miss that last unterminated line. It will have
been read by the last invocation of read, though, only its exit
status will have been non-zero, so you can do:

cmd1 | while IFS= read -r line; do cmd2 -- "$line"; cmd3 -- "$line"; done
[ -n "$line" ] && cmd2 -- "$line"; cmd3 -- "$line"

if you need to be able to cope with such outputs (not that many
commands don't terminate their output with a newline character).

--
Stéphane

Re: Multiple command redirection

am 22.09.2007 22:03:33 von wimxa

2 Ed:
> find . -type f -print -exec cat {} \;
>
> Ed.
Excellent! Thank you Ed! Just as an appendix,

find . -type f -print -exec cat {} \; -exec echo \;

will make it work for files not ending with a new line.

2 Barry/St=E9phane:
> command1 | while read line; do command2 "$line"; command3 "$line"; done
> cmd1 | while IFS=3D read -r line; do cmd2 -- "$line"; cmd3 -- "$line"; do=
ne
Thanks guys, this solves the other part of the question - how to do it
for other commands (when I don't need find or just printing). To
simulate what Ed posted, this would to the trick:

find -type f|while read l; do echo "$l"; cat "$l"; echo; done

or apparently more safe

find -type f|while IFS=3D read -r l; do echo "$l"; cat "$l"; echo; done

Hell, this is so good. Have to learn bash programming :)