LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to direct standard output from find command (https://www.linuxquestions.org/questions/programming-9/how-to-direct-standard-output-from-find-command-798104/)

dyq 03-26-2010 01:26 PM

How to direct standard output from find command
 
Hello everyone,

I'm trying to pull out sections from a bunch of files. For one file, I use:

Code:

sed '/string1/,/string2/ !d' <filename.ext >newfilename.ext
to pull out everything between two strings in the original file and put them in a new file. I would like to do this for all files in a directory. I have tried:

Code:

find . -type f -name '*.ext' -exec sed '/string1/,/string2/ !d' {} >./NewDirectory/{}\;
This results in a single output file in NewDirectory named {}. {} contains output from all of the files found by find. I would like each output to be in a separate file with the same name as the original file. What am I doing wrong?

Thank you for your help!

blacky_5251 03-26-2010 03:13 PM

I've found the simplest approach is to put your complex command inside an executable script file, and use pass it the filename as a parameter.
Code:

echo "sed '/string1/,/string2/ !d' <${1} >NewDirectory/${1}" > sedscript.sh
chmod 755 sedscript.sh
find . -type f -name '*.ext' -exec ./sedscript.sh {} \;

It just makes life easier I think.

Robhogg 03-26-2010 03:15 PM

I don't think that it will work in this way. The problem is that the shell concludes that the command has ended when it sees the redirection operator (>), so the filename here is no longer within the scope of the find command.

Have you tried this instead?:
Code:

for file in `find . -type f`; do
sed '/string1/,/string2/ !d' <$file >./directory/$file
fi


dyq 03-26-2010 03:55 PM

Thank you Ian and Robhogg for your helpful replies. I ended up using a shell script as suggested.


All times are GMT -5. The time now is 12:25 AM.