what's wrong with my -exec in find


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting what's wrong with my -exec in find
# 8  
Old 07-14-2012
Quote:
Originally Posted by yanglei_fage
Code:
find ./ -name *Kconfig -exec cat {} |grep CONFIG_MTD |grep depend \;

how could I handle this
Here is a solution near your own code:
Code:
find ./ -name '*Kconfig' -exec grep CONFIG_MTD {} \; | grep depend

But I think some of the already given answers is the best, like that with using xargs as an example.
# 9  
Old 07-14-2012
Quote:
Originally Posted by methyl
Quick answer to post #1 is to not use exec or cat.
Code:
find ./ -type f -name *Kconfig -print | while read filename
do
      grep "CONFIG_MTD" "${filename}" |grep "depend"
done

Please remember to post what Operating System you have and what Shell you use.
I've got error here:

Code:
./script.sh 
find: paths must precede expression: 2.Kconfig
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

---------- Post updated at 03:29 PM ---------- Previous update was at 03:28 PM ----------

Quote:
Originally Posted by 244an
Here is a solution near your own code:
Code:
find ./ -name '*Kconfig' -exec grep CONFIG_MTD {} \; | grep depend

But I think some of the already given answers is the best, like that with using xargs as an example.
That solution doesn't specify to which file belongs each line, but is shorter after all.
# 10  
Old 07-14-2012
Quote:
Originally Posted by Tribe
Quote:
Originally Posted by methyl
Quick answer to post #1 is to not use exec or cat.
Code:
find ./ -type f -name *Kconfig -print | while read filename
do
      grep "CONFIG_MTD" "${filename}" |grep "depend"
done



Please remember to post what Operating System you have and what Shell you use.
I've got error here:

Code:
./script.sh 
find: paths must precede expression: 2.Kconfig
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

The -name pattern argument needs to be quoted.


Quote:
Originally Posted by Tribe
Quote:
Originally Posted by 244an
Here is a solution near your own code:
Code:
find ./ -name '*Kconfig' -exec grep CONFIG_MTD {} \; | grep depend

But I think some of the already given answers is the best, like that with using xargs as an example.
That solution doesn't specify to which file belongs each line, but is shorter after all.
To enable the printing of filenames without otherwise altering the results, you can add /dev/null to grep's list of files to process, e.g., -exec grep PATTERN {} /dev/null \;.

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
# 11  
Old 07-15-2012
Quote:
That solution doesn't specify to which file belongs each line, but is shorter after all.
The advantage of the while-read-do-done construct is that you can add to the contents of the loop.
For example:

Code:
find ./ -type f -name '*Kconfig' -print | while read filename
do
      echo "Searching: ${filename}"
      grep "CONFIG_MTD" "${filename}" |grep "depend"
done


Ps. Corrected quotes in back post. I'd originally copied the line from post #1.
These 2 Users Gave Thanks to methyl For This Post:
# 12  
Old 07-16-2012
For me it was an interesting thread, not considering the "best" solution I learned some new things.
Quote:
Originally Posted by alister
...
To enable the printing of filenames without otherwise altering the results, you can add /dev/null to grep's list of files to process, e.g., -exec grep PATTERN {} /dev/null \;.
Using /dev/null that way was good to know (I also noticed now that alister was meaning the solution I suggested in his first post).

And good to learn the use of ... | while read file; do ...; done in
Quote:
Originally Posted by methyl
...
And finally using xargs
Quote:
Originally Posted by Tribe
...
Code:
find ./ -name '*Kconfig' -print | xargs grep -n CONFIG_MTD | grep depend

But I have a question to the xargs-solution, why is this printing filenames? Is xargs using grep (in the example) as with grep file1 file2 fil3 ... ? I thought that the used construction with find ... -print | xargs grep ... would do one grep for each file, not one grep with all files.
Anyone understands my question? Smilie
# 13  
Old 07-16-2012
Quote:
Originally Posted by 244an
For me it was an interesting thread, not considering the "best" solution I learned some new things.

Using /dev/null that way was good to know (I also noticed now that alister was meaning the solution I suggested in his first post).

And good to learn the use of ... | while read file; do ...; done in
And finally using xargs

But I have a question to the xargs-solution, why is this printing filenames? Is xargs using grep (in the example) as with grep file1 file2 fil3 ... ? I thought that the used construction with find ... -print | xargs grep ... would do one grep for each file, not one grep with all files.
Anyone understands my question? Smilie
In this case, xargs executes the grep command only once , because the file1, file2, etc you say don't exist, just the standard input that is a block of data. It is grep that detects inside the stream what are the files.

You can see here:
Code:
find . -name '*Kconfig' -print0 | xargs -0 echo | wc -l
1

While the -exec runs the command once per found item:
Code:
find . -name '*Kconfig' -exec echo \; | wc -l
19

------------------------------------
In fact, I made a mistake using -print instead of -print0, because if there is a filename with spaces or other odd characters will cause error.

Code:
touch aKconfig "b Kconfig" cKconfig
find . -name '*Kconfig' -print > data.bin
find . -name '*Kconfig' -print0 > data0.bin

Results in binary comparison:
Code:
od -x data.bin 
0000000 2f2e 4b61 6f63 666e 6769 2e0a 632f 634b
0000020 6e6f 6966 0a67 2f2e 2062 634b 6e6f 6966
0000040 0a67

Code:
od -x data0.bin 
0000000 2f2e 4b61 6f63 666e 6769 2e00 632f 634b
0000020 6e6f 6966 0067 2f2e 2062 634b 6e6f 6966
0000040 0067

So -print and -print0 for find are not equal. The same option has to be used with xargs to prevents errors.

Last edited by Tribe; 07-16-2012 at 03:55 PM..
# 14  
Old 07-16-2012
Quote:
Originally Posted by yanglei_fage
Code:
find ./ -name *Kconfig -exec cat {}  \;

but it won't work with

Code:
find ./ -name *Kconfig -exec cat {} |grep CONFIG_MTD |grep depend \;

how could I handle this
neutronscott already suggest a very similar (and simpler) way. Here is the closest solution to your attempt:
Code:
find . -name "*Kconfig" -exec sh -c 'cat "$1" | grep CONFIG_MTD | grep depend' foo {} \;

Not sure why it wasn't considered.
This User Gave Thanks to jlliagre For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing perl script in Linux gives :Exec format error. Wrong Architecture

i have perl script that used to be working great , once i edit it in windows and convert it to UTF-8 and then via FTP return it . also did: chmod +x foo.pl and then when i try to run it : ./foo.pl im getting this error: ./foo.pl: Exec format error. Wrong Architecture.... (4 Replies)
Discussion started by: umen
4 Replies

2. Shell Programming and Scripting

2 exec in find

Guys, I want to find the log files greather than 23 days and i want to perform 2 things here. one is to list the files and second is to gzip the files. hope this can be done using sh -c option. but not sure the exact command. find . -name "*.log" -mtime +23 -exec ls -la {} \; ... (5 Replies)
Discussion started by: AraR87
5 Replies

3. Shell Programming and Scripting

find: missing argument to `-exec' while redirecting using find in perl

Hi Friends, Please help me to sort out this problem, I am running this in centos o/s and whenever I run this script I am getting "find: missing argument to `-exec' " but when I run the same code in the command line I didn't find any problem. I am using perl script to run this ... (2 Replies)
Discussion started by: ramkumarselvam
2 Replies

4. Ubuntu

Find and EXEC

This is a huge issue. and I need it fixed ASAP. account-system gate-system race_traffic_sensor achievement-system global race_voicepack admin glue-system realdriveby admin-system gps realism-system... (5 Replies)
Discussion started by: austech360
5 Replies

5. Ubuntu

Find and exec

Hello, I am a linux newbe. I want to install a program. I can download it only with wget command from internet. As far as i know this wget command does not transfer the exacutable flags. Because of that i wanted to find all configure files and change their mod to 744. I found this... (1 Reply)
Discussion started by: disconnectus
1 Replies

6. UNIX for Dummies Questions & Answers

Find Exec

Hello All, Is there a way to make exec do a couple of operations on a single input from find? For example, find . -type d -exec ls -l "{}" ";" I would like to give the result of each "ls -l" in the above to a wc. Is that possible? I want to ls -l | wc -l inside... (1 Reply)
Discussion started by: prasanna1157
1 Replies

7. Shell Programming and Scripting

Using MV FIND and -EXEC

Hi, i would like to rename files in directories and subdirs. Files contains specific french or strange caracters. I want to replace all non alpha-numerics by _ (underscore) First, i made this, but i think the "for" is limited. How can i do this directly by FIND ? for file in $(find .... (0 Replies)
Discussion started by: degraff63
0 Replies

8. Shell Programming and Scripting

| with find -exec

can we use |(pipe operator) with find -exec.....? or can pipe the output of find command to another command...? if not, why...? pls explain (3 Replies)
Discussion started by: vijay_0209
3 Replies

9. UNIX for Advanced & Expert Users

Using a pipe with find .... -exec ...

Hi, I am trying to run the following command: find ./ -name lea_01.001 -print -exec CEOS {} | grep -i radio \; where "CEOS" converts the lea_01.001 files to text, then grep looks for the string "radio." This however does not work as I have constructed it. This command mostly works, but... (1 Reply)
Discussion started by: pmallas
1 Replies

10. UNIX for Advanced & Expert Users

find and exec

Hi, Happy new year. Would you be so kind to explain me what does this instruction : find /rep/app -type l -exec ls -l {} \;> allink.lst Many thanks. (2 Replies)
Discussion started by: big123456
2 Replies
Login or Register to Ask a Question