complex find in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting complex find in script
# 1  
Old 01-27-2009
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
# 2  
Old 01-27-2009
You copy and paste it into a new file, and then run it. See as follows:
Code:
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:
Code:
find .... <whatever> ... | 
while read FNAME ; do  
   .... echo ${C};${FNAME}
done >outputfile.txt

Hope that all helps.
# 3  
Old 01-27-2009
script

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.
# 4  
Old 01-27-2009
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.
# 5  
Old 01-27-2009
Quote:
Originally Posted by TimHortons
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 do not understand the reason why, are you able to explain.
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 ... Smilie

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.
# 6  
Old 01-27-2009
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.


Code:
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


Last edited by methyl; 01-27-2009 at 10:04 AM.. Reason: typos
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Complex Bash Script

I have an FTP server with thousands of Invoices. All Invoices are in a folder called /volume1/MBSInvoices/ Monthly invoices are added to that folder every month. Here is a sample filename of the Invoices: invoice_1_20170101_10010052_10020052_10030052_JOHNDOE.pdf the Account ID is the... (6 Replies)
Discussion started by: badr777
6 Replies

2. Shell Programming and Scripting

awk script (complex)

picked this up from another thread. echo 1st_file.csv; nawk -F, 'NR==FNR{a++;next} a{b++} END{for(i in b){if(b-1&&a!=b){print i";\t\t"b}else{print "NEW:"i";\t\t"b} } }' OFS=, 1st_file.csv *.csv | sort -r i need to use the above but with a slight modification.. 1.compare against 3 month... (25 Replies)
Discussion started by: slashbash
25 Replies

3. Shell Programming and Scripting

Complex find and replace only 1st instance string with dynamic combination

test.txt is the dynamic file but some of combination are fix like below are the lines ;wonder_off = ;wonder_off = disabled wonder_off = wonder_off = disabled the test.txt can content them in any order #cat test.xt ;wonder_off = ;wonder_off = disabled wonder_off = wonder_off =... (5 Replies)
Discussion started by: SilvesterJ
5 Replies

4. UNIX for Advanced & Expert Users

Help with complex find syntax

I need to modify the find command below to exclude the output of the directory /usr/UDPM/PerfMgmt/shmlck find / \( -fstype ctfs -o -fstype mntfs -o -fstype objfs -o -fstype proc -o ! local \) -prune -o -type f -perm -0002 -print 2>/dev/null I have tried many iterations and placement of... (2 Replies)
Discussion started by: interesting?
2 Replies

5. Shell Programming and Scripting

Complex Script

hey... i had a big problem with my professor i have 3 simple archives in.txt -> had all timestamps of users logon (100lines) ex. 111111 222222 333333 out.txt -> had all timestamps of users logof (100lines) ex. 111113 222225 333332 commands.txt... (9 Replies)
Discussion started by: beandj
9 Replies

6. Shell Programming and Scripting

find command with complex logic

I'm looking to write a script that will do a find of directories and delete them if they are older than x days but keep the last x # of folders even if they are older than x days. The usage is for a deployment location, so we want to keep the location clean but retain maybe the last 2 builds that... (5 Replies)
Discussion started by: MaureenT
5 Replies

7. UNIX for Advanced & Expert Users

complex find command

Hi all, I am trying to execute the following command: find 'path' -ls -exec cksum {} \; As you can see this simply finds files from a given path and runs cksum on them. My problem is this, if i have a FIFO in a directory the find tries to execute cksum on it and gets stuck. From the man page i... (9 Replies)
Discussion started by: noam128
9 Replies

8. Shell Programming and Scripting

pls help! complex find and replace

help pls... i would like to change this CURVE2 565489 789458 1258649 random data here... CURVE2 565489 568795 6548921 random data here... CURVE2 565489 123598 6446259 random data here... CURVE2 565489 672956 2489657 into this CURVE2 565489 586423 1258649 random data here...... (2 Replies)
Discussion started by: lakanino
2 Replies

9. Shell Programming and Scripting

Complex find grep or sed command

Haven't worked in bash for ages. did a good bit of shell scripting in regular sh, but have forgotten most of it. I have several thousand php files that now include the following line at the end of the file. There is no LF or CR/LF before it begins, it is just concatenated to the final line of... (3 Replies)
Discussion started by: sjburden
3 Replies

10. Answers to Frequently Asked Questions

advanced/complex uses of the find command

Perhaps the number one advanced find question is: How to stop find from descending into subdirectories? find command Performing a non-recursive find in Unix Use -prune with find command on AIX Searching for files over 30 days old in current directory disk space used for files with in a... (0 Replies)
Discussion started by: Perderabo
0 Replies
Login or Register to Ask a Question