On Dec 9, 2010, at 1:57 PM, [email protected] wrote:
> I have a situation where I have a bunch of config files that define
> filters, I want to cat a file and run it through all these filters,
> capturing the output
>
> the command line to do so would be something like:
>
> cat input |./filter1 -r file1 |./filter1 -r file2 |./filter2 -r file3 >output
>
> the following line produces a line that looks like this
>
> echo -n "cat input | "; ls *.meta |cut -f 1 -d "." |while read file; do grep
> "^Filter: " $file.meta |head -1 |cut -f 2 -d " " |while read filter; do echo
> -n "./$filter -r $file | "; done; done; echo "cat >output"
Let me see if I understand what you're actually doing...
You have a directory with a file "input" that contains some input and a bunch
of files *.meta that each contain one or more lines that look like "Filter:
something". The "something"s denote commands that accept a -r argument
followed by the name of the meta file that defined it (without the .meta
extension), which expect to read from stdin and write to stdout. You
ultimately want the output of the last filter to land in a file called "output".
Leaving aside your code for extracting the filenames and setting up the
command, I think you want to use eval.
eval `echo -n "cat input | "; ls *.meta |cut -f 1 -d "." |while read file; do
grep "^Filter: " $file.meta |head -1 |cut -f 2 -d " " |while read filter; do
echo -n "./$filter -r $file | "; done; done; echo "cat >output"`
I'd suggest, however, something like this:
(eval `ls -1 *.meta | while read m; do f=\`grep "^Filter:" $m | awk '{print $2;
exit}'\`; echo -n "./$f -r \`basename $m .meta\` |"; done | sed -e "s/|$//"`) <
input > output
which gets rid of the multiple "cut" commands, "head", one of the nested loops,
and both gratuitous uses of "cat".
Hope that helps.
-jan-
--
Jan L. Peterson
http://www.peterson-tech.com/~jlp/
_______________________________________________
Discuss mailing list
[email protected]
https://lists.lopsa.org/cgi-bin/mailman/listinfo/discuss
This list provided by the League of Professional System Administrators
http://lopsa.org/