|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today! |
|
#1
|
|||
|
|||
|
Using pipes inside of find -exec
Hi,
one more question related to LC: I'm trying to demonstrate to some of my colleagues, how hopeless is trying to count the lines of code in C/C++ files by just parsing them (i.e. without invoking the "gcc -E" to remove comments and #if 0). So I'm trying to extract simple strings out of C/C++ files: boclu23:r0923 {106} find . \( -name \*.cpp -o -name \*.c \) -print | xargs sed -ne 's/.*"\([^"]*\)\".*/"\1"/p' | head "st.h" "global.h" "product.h" "type_def.h" "mac_def.h" "hw.h" "os.h" "pn.h" "cm.h" "cm_time.h" And then grep those extracted strings for the characters "/*" : boclu22:r0923 {153} find . \( -name \*.cpp -o -name \*.c \) -print | xargs sed -ne 's/.*"\([^"]*\)\".*/"\1"/p' | grep '/\*' | head "/* This file is generated %s by\n** '%s'.\n" " %d, /* PMM_INDEX_T base; */" "\n { /* byte region_model[HW_FLASH_DEVICES]; */\n" " { /* PMM_CATEGRY_STR categories[PMM_NF_CATEGRIES]; */\n" "#endif /* PMM_FLASH_DEF_H */\n" "\n\n/* End of file */\n" "#define %-25s 0x%.2x /* %d */\n" "/* routing table. Maps Id's onto task id's */" " 0xFF , /* Empty */\n" "/* Declaration of functions to appear in the function routing table */" So there are quite a few files with weird strings. And here is my problem: how could I print the names of the files who contain the weird strings (i.e. with embedded comments)? I try: boclu23:r0923 {116} find . \( -name \*.cpp -o -name \*.c \) -print -exec sed -ne 's/.*"\([^"]*\)\".*/"\1"/p' {} \| grep '/\*' \; but get the sed error messages because it doesn't recognize the pipe: sed: can't read |: No such file or directory sed: can't read grep: No such file or directory sed: can't read /\*: No such file or directory And if I don't escape the pipe like this, then the find complains: boclu23:r0923 {116} find . \( -name \*.cpp -o -name \*.c \) -print -exec sed -ne 's/.*"\([^"]*\)\".*/"\1"/p' {} | grep '/\*' \; find: missing argument to `-exec' grep: ;: No such file or directory So the questions are: 1) is it possible to use pipes in "find -exec" and how? 2) is there a way to print the filename before sedding it? I'm using RH Linux and thus GNU tools but would be interested in a portable solution too. (And yes I know I could do it all in Perl, but I'd like to learn some nice shell oneliners) Thank you! Alex |
|
#2
|
|||
|
|||
|
Using pipes inside of find -exec
In article <3F1D0552.F2AA33C1@nokia.com>,
A. Farber <Alexander.Farber@nokia.comwrote: >So the questions are: > >1) is it possible to use pipes in "find -exec" and how? Yes, if you run a shell via -exec. The simplest way is to put the pipeline in a shell script, and then specify that scriptname as the command in the -exec option, e.g. find -exec myscript {} \; But if you really *must* do it as a one-liner, you can do: find -exec sh -c 'some_command $1 | other_command' {} {} \; >2) is there a way to print the filename before sedding it? find -print -exec -- Barry Margolin, barry.margolin@level3.com Level(3), Woburn, MA DN'T SEND TECHNICAL QUESTINS DIRECTLY T ME, post them to newsgroups. Please DN'T copy followups to me -- I'll assume it wasn't posted to the group. |
|
#3
|
|||
|
|||
|
Using pipes inside of find -exec
I'm a bit confused If : find . \( -name \*.cpp -o -name \*.c \) -print | xargs sed -ne 's/.*"\([^"]*\)\".*/"\1"/p' outputs: "st.h" "global.h" "product.h" "type_def.h" "mac_def.h" "hw.h" "os.h" "pn.h" "cm.h" "cm_time.h" How come you get any results when you pipe the output into grep looking for /* ? Anyhow, I don't know how/if you can pipe commands within "find -exec". You can use exec multiple times though (-exec sed '' -exec grep "") but that's not what you are looking for is it? If what you are trying to achieve is to extract the filenames above and to grep those files for your commented strings - why not just pass the results of your find as a list of files to grep? e.g. grep "/\*" $(find . -exec sed -ne 's/.*"\([^"]*\)\".*/"\1"/p' {} \;) This should output the filename and the grep'd string together. Am I understanding the problem correctly? -- Posted via http://dbforums.com |
|
#4
|
|||
|
|||
|
Using pipes inside of find -exec
"A. Farber" <Alexander.Farber@nokia.comwrote in message news:<3F1D0552.F2AA33C1@nokia.com>
Hi, > one more question related to LC: I'm trying to demonstrate to some of my colleagues, how hopeless is trying to count the lines of code in C/C++ files by just parsing them (i.e. without invoking the "gcc -E" to remove comments and #if 0). > So I'm trying to extract simple strings out of C/C++ files: > boclu23:r0923 {106} find . \( -name \*.cpp -o -name \*.c \) -print | xargs sed -ne 's/.*"\([^"]*\)\".*/"\1"/p' | head "st.h" "global.h" "product.h" "type_def.h" "mac_def.h" "hw.h" "os.h" "pn.h" "cm.h" "cm_time.h" > > And then grep those extracted strings for the characters "/*" : > boclu22:r0923 {153} find . \( -name \*.cpp -o -name \*.c \) -print | xargs sed -ne 's/.*"\([^"]*\)\".*/"\1"/p' | grep '/\*' | head "/* This file is generated %s by\n** '%s'.\n" " %d, /* PMM_INDEX_T base; */" "\n { /* byte region_model[HW_FLASH_DEVICES]; */\n" " { /* PMM_CATEGRY_STR categories[PMM_NF_CATEGRIES]; */\n" "#endif /* PMM_FLASH_DEF_H */\n" "\n\n/* End of file */\n" "#define %-25s 0x%.2x /* %d */\n" "/* routing table. Maps Id's onto task id's */" " 0xFF , /* Empty */\n" "/* Declaration of functions to appear in the function routing table */" > > So there are quite a few files with weird strings. And here is my problem: how could I print the names of the files who contain the weird strings (i.e. with embedded comments)? I try: > boclu23:r0923 {116} find . \( -name \*.cpp -o -name \*.c \) -print -exec sed -ne 's/.*"\([^"]*\)\".*/"\1"/p' {} \| grep '/\*' \; > but get the sed error messages because it doesn't recognize the pipe: > sed: can't read |: No such file or directory sed: can't read grep: No such file or directory sed: can't read /\*: No such file or directory > And if I don't escape the pipe like this, then the find complains: > boclu23:r0923 {116} find . \( -name \*.cpp -o -name \*.c \) -print -exec sed -ne 's/.*"\([^"]*\)\".*/"\1"/p' {} | grep '/\*' \; find: missing argument to `-exec' grep: ;: No such file or directory > > So the questions are: 1) is it possible to use pipes in "find -exec" and how? find . -type f \( -name \*.c -o -name \*.cpp \) \ -exec sh -c 'grep "[^\"]*" "$1" | sed -e "/\/\*/!d"' {} {} \; 2) is there a way to print the filename before sedding it? find . -type f \( -name \*.c -o -name \*.cpp \) \ -exec sh -c 'grep "[^\"]*" "$1" /dev/null | sed -e \ "/^\([^:]*:\).*\(\"[^\"]*\"\).*\$/s//\1\2/;/\/\*/!d" ' {} {} \; (assuming that the filenames have no ':' character in them anywhere) |
![]() |
| Viewing: Web Development Archives > FAQs > Unix/Linux > Using pipes inside of find -exec |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|