LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
LinkBack Search this Thread
Old 03-26-2010, 01:26 PM   #1
dyq
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:

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!

Last edited by dyq; 03-26-2010 at 01:31 PM.
 
Old 03-26-2010, 03:13 PM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 4&5, Fedora 10, CentOS 5.4, IPCop
Posts: 569

Rep: Reputation: 54
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.
 
1 members found this post helpful.
Old 03-26-2010, 03:15 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
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
 
Old 03-26-2010, 03:55 PM   #4
dyq
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.
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 09:27 AM.

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