Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-26-2010, 01:26 PM   #1
LQ Newbie
Registered: Feb 2010
Posts: 4

Rep: Reputation: 0
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:

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:

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!

Last edited by dyq; 03-26-2010 at 01:31 PM.
Old 03-26-2010, 03:13 PM   #2
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
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.
echo "sed '/string1/,/string2/ !d' <${1} >NewDirectory/${1}" >
chmod 755
find . -type f -name '*.ext' -exec ./ {} \;
It just makes life easier I think.
1 members found this post helpful.
Old 03-26-2010, 03:15 PM   #3
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
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?:
for file in `find . -type f`; do
sed '/string1/,/string2/ !d' <$file >./directory/$file
Old 03-26-2010, 03:55 PM   #4
LQ Newbie
Registered: Feb 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thank you Ian and Robhogg for your helpful replies. I ended up using a shell script as suggested.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Command to find list of patches applied to standard kernel in Red Hat? ndh_singh80 Linux - Newbie 2 01-17-2009 06:42 PM
How do I parse the output of a command that output to standard out? gauol Programming 9 01-17-2008 01:54 PM
command to sort by floats in fourth column of standard output jhwilliams Linux - Software 4 06-22-2007 01:27 PM
can't capture 'at' command standard output to a file bartoni Linux - General 3 02-28-2006 03:52 PM
How to redirect standard output of piped command to log file andrewstr Linux - General 10 02-04-2004 02:07 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:35 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration