LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
Search this Thread
Old 10-18-2010, 08:47 AM   #1
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Rep: Reputation: 0
Applying a script to all the files of the find command


Code:
find "$SOURCEDIR" -type f -name "*$ITEM" -printf "%P\n"
I want to apply some shell script to the files outputed by the find command.


How can i do this.? Can anybody help me on this.

There are multiple files directories and also multiple files.
 
Old 10-18-2010, 08:54 AM   #2
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Rep: Reputation: 31
Isn't exec able to do that? I always use the following to change hostnames on unix systems

Code:
vi `find . -type f -exec grep -li THEOLDHOSTNAME {} +`
The exec could call a bash script I suspect.
 
Old 10-18-2010, 08:54 AM   #3
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Code:
find "$SOURCEDIR" -type f -name "*$ITEM" -printf "%P\n" | xargs -I {} /path/to/your/script {}
`find` is recursive, so unless you tell it otherwise, it will find all files and all folders under the search path. This command I give, will execute:
Code:
/your/script <find result>
for every result from `find`.

If this doesn't help or doesn't work as you expect, tell us what it does or doesn't do correctly.
 
Old 10-18-2010, 08:57 AM   #4
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Plus - genderbender is probably right too, as far as I know; you could use `find`'s -exec command too. I think it would work.
 
Old 10-18-2010, 09:19 AM   #5
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Yes, but remember that using -exec will run that command on each file individually, while xargs will run once (or a few times) on all files.

EDIT:
One more thing, I would use the find -print0 and xargs -0 options to help with possible white space issues.

Last edited by H_TeXMeX_H; 10-18-2010 at 09:21 AM.
 
1 members found this post helpful.
Old 10-18-2010, 09:22 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Which then boils down to how does your script handle input, ie can it take more than a single argument?
 
Old 10-18-2010, 09:36 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
When you use a plus sign at the end of the -exec expression, it acts similarly to xargs, concatenating all the file names into as few runs as possible; as opposed to using a semicolon, which runs the command separately for each file found. See the find man page for details.

Your script/program must be able to handle the multiple inputs, of course.
 
2 members found this post helpful.
Old 10-19-2010, 01:24 AM   #8
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Thanks for all your replies.

The following is the script which needs to be run on the output files of the above find result.

Quote:
awk -f Extract.sh <filename> |awk '{sub(/^[ /*]+/, ""); print}'|awk -f Format.sh
Where Extract.sh and Format.sh are shell scripts.

Meanwhile, i will try the different options provided by all of you.
 
Old 10-19-2010, 01:57 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Well without seeing the other scripts I would have thought it might be time to merge the 3 scripts to get done what you require.

Seeing where you need the filename I am not sure if any of the above would be correct (I could be well wrong here)
I would probably suggest a while loop getting input from the find and performing your tasks as based on post #8 what you have will only act on separate files.
 
Old 10-19-2010, 03:51 AM   #10
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Yes Grail.

The same thing i have done and got it.

Here is the code:

Quote:
while read file; do
echo "File name: " $file
awk -f Extract.sh $SOURCEDIR/$file|awk '{sub(/^[ /*]+/, ""); print}'|awk -f Format.sh
done < <(find "$SOURCEDIR" -type f -name "*$ITEM" -printf "%P\n")
Posted this thread to know some other way of doing this.

Also,

Is there any way to recognize if there is no output from the below:

Quote:
awk -f Extract.sh <filename>
 
Old 10-19-2010, 07:13 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Quote:
Is there any way to recognize if there is no output from the below:

Quote:
awk -f Extract.sh <filename>
Would need more information on what Extract.sh does?
 
Old 10-19-2010, 09:40 AM   #12
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Original Poster
Rep: Reputation: 0
hi Grail,

The following is the script.

Quote:
{
if ($0 ~ "START")
{
#print $0;
getline;
do
{
print $0;
getline;
}while ($0 !~ "END");
print $0
}
}

It will extract the text between the START and the END tags in a given input file.
 
Old 10-19-2010, 10:25 AM   #13
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Well I am not sure the last print $0 should be there as it will print line containing END??
Maybe you could use this much simpler version:
Code:
/START|END/{f=!f;next}f
Maybe if you show me some input and what is in Format.sh we could make it a lot simpler?
 
Old 10-20-2010, 01:39 AM   #14
flamingo_l
Member
 
Registered: Jul 2010
Posts: 41

Original Poster
Rep: Reputation: 0
hi Grail,

Yes print $0 is required to print the line END.

How can i execute the code given by you.?

My Inout file may or may not contain the START or END tags, so i want to skip the file which is not having START and END tags. Is there any way to recognize this?

It is the same script given by you in the following thread.

http://www.linuxquestions.org/questi...t-file-837330/

Last edited by flamingo_l; 10-20-2010 at 01:44 AM.
 
Old 10-20-2010, 08:03 AM   #15
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
So why do we need the first 2 awk scripts?

If you wish to test the above just put it into your extract.awk file and run as you have above.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 06:55 PM
Single find command to find multiple files? thok Linux - Newbie 7 01-31-2009 05:45 PM
Using a single "Find" Command to find files bases on multiple criteria roboxooo Linux - Newbie 6 01-15-2009 05:13 AM
How to find files and copy the found files to the floppy in one command justmehere Linux - Newbie 11 05-05-2008 12:29 AM
problem with find command in script cojo Linux - Software 3 05-26-2004 11:28 AM


All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
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