complex find in script

How to I put my find command string into a script. It is currently to long to be entered manually at command line.

for FNAME in `find /unixsxxx/interface/x.x/xxxxxx -type f \( -name '.KSH' -o -name '.sh' -o -name '.sql' -o -name '.ksh' \) -exec grep -il xxx.xxx.xxx.xxx {} \;`; do C=`grep -c xxx.xxx.xxx.xxx ${FNAME}`; echo "${C}:${FNAME}" >> /unixs317/apps/output.txt;done

You copy and paste it into a new file, and then run it. See as follows:

cd $HOME
cat >new-script.sh
  paste and  end with CTRL-D
sh $HOME/new-script.sh

But I think your grep command is doing what your find command SHOULD be doing. See the "printf" capabilities in the find man pages.

Also I prefer:

find .... <whatever> ... | 
while read FNAME ; do  
   .... echo ${C};${FNAME}
done >outputfile.txt

Hope that all helps.

I took your suggestion and created

find /unixs/STAT_HOME/ -type f -exec grep -il unixsxxx {} \;
while read ${FNAME}; do C=grep -c xxx.xxx.xxx echo ${C}:${FNAME}
done >> outputfile.txt

I get this

/unixsxxx/STAT_HOME/app/server/default/deploy/stat-ds.xml
/unixsxxx/STAT_HOME/app/server/default/log/server.log.2008-12-05
/unixsxxx/STAT_HOME/app/server/default/log/server.log.2008-12-08
/unixsxxx/STAT_HOME/app/server/default/tmp/deploy/tmp28644stat-ds.xml
/unixsxxx/STAT_HOME/stat_log/stat_ui.log
ksh[3]: /unixsxxx/STAT_HOME/stat_log/stat_ui.log: is not an identifier
ksh[3]: new-script.sh: cannot execute

I do not understand the reason why, are you able to explain.

You have several syntax errors. The only things you should change from what I posted the ellipses, the "<whatever>" and optionally changing the >output.txt into a file you want it to be named. >> appends the output which will be confusing on multiple runs.

First, I do not see that you piped your output into the while loop as otheus described. You'll need that pipe for $FNAME to make any sense in your while loop. The ' | ' in otheus' example is important.

You need to use command substitution on your "grep" command in the while loop. Either surround the grep command and arguments with back tics ( the ' ` ' below the tilde) or use the "$(<command>)" (with the double quotes) around your grep line. It depends upon your shell and/or system as to which you use.

Lastly, this may just be how you posted the command and you actually might have done it right, but just in case ... :slight_smile:

You need to separate out your commands with either a newline or a semi colon. You should not have 'do' 'grep' and 'echo' all on the same line, unless you have semi colons between each different command. For clarity, I like to put them on separate lines.

I agree. The missing pipe is part of the problem.
Rearranging the original commands without introducing new syntax.
(We don't know which shell is in use here).
If we used "grep -il" to find the files , we must use "grep -ic" to count the contents.
Full stops in strings need quoting (or the whole string in single quotes) to prevent the full stop becoming part of a regular expression.

find /unixsxxx/interface/x.x/xxxxxx -type f \( -name '*.KSH' -o -name '*.sh' -o -name '*.sql' -o -name '*.ksh' \) -print | while read FNAME
do
        # Find any files which contain xxx.xxx.xxx.xxx
        grep -il 'xxx.xxx.xxx.xxx' "${FNAME}" | while read FOUND
        do
                # Count occurances in selected file
                C=`grep -ic 'xxx.xxx.xxx.xxx' "${FOUND}"`
                echo "${C}:${FOUND}" >> /unixs317/apps/output.txt
        done
done