Entry tags:
Unix script help needed
I have hit a brick wall and any help that can be offered would be gratefully appreciated. My Google-fu isn't up to the task, we have no reference books on Unix scripting here ("here's a card with basic Unix commands - have fun!"), my local library has no books on Unix scripting and I haven't got five days for Canada Post to lose deliver an Amazon order of books for me.
I'm attempting to loop through some files in a directory, count the number of files that contain a particular word sequence and put that count into a file that I can then email. All the filenames that I am searching begin with 'O'. So far, if I cd to the directory and put this into the command line it works:
My log file correct shows a count of 2.
When I do the same thing in my script and run it, I get big fat zeros in my report :-(
According to my log, I've cd'd to the correct directory. I've tried using $LOGDIR1/O* with no luck.
Can anyone point me to some resources that might help? If it helps, I would appear to be using the Korn shell in this installation of Unix. The mere fact that I've had to write "appear to be using" should tell you how much I actually know about Unix :-(
I'm attempting to loop through some files in a directory, count the number of files that contain a particular word sequence and put that count into a file that I can then email. All the filenames that I am searching begin with 'O'. So far, if I cd to the directory and put this into the command line it works:
print cat O* | grep -c 'XXXX_PROD' >> test.log
My log file correct shows a count of 2.
When I do the same thing in my script and run it, I get big fat zeros in my report :-(
cd $LOGDIR1
print $PWD >> $REP
print "Count of XXXX_PROD jobs run (test only):" >> $REP
print cat O* | grep -c 'XXXX_PROD' >> $REP
According to my log, I've cd'd to the correct directory. I've tried using $LOGDIR1/O* with no luck.
Can anyone point me to some resources that might help? If it helps, I would appear to be using the Korn shell in this installation of Unix. The mere fact that I've had to write "appear to be using" should tell you how much I actually know about Unix :-(
no subject
(no subject)
(no subject)
(no subject)
(no subject)
no subject
- What scripting language are you using? i.e. what kind of file is it?
- Is there some kind of 'end of line' character that needs to go in there?
Just guessing, sorry, but the above might prompt a thought?
(no subject)
no subject
grep -binary-files=text -c -r 'XXXX_PROD' *
(not that I'm sure that's an entirely valid command...)
Or
You may not have the cat and grep around the right way. You're feeding the output of cat in to grep, rather than the other way around which I think is what you'd want, no?
As you can tell this isn't really my thing either...
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
no subject
How are you running your script, and what is the first line of the script? If you type
The first line of your script should contain a line something like the following,
If not, then the shell used will depend on how you run the script. If you are just typing in the full name of the script, then it will default to your login shell (presumably korn shell).
Your command could be changed (there are always many ways to do the same thing in a Unix shell), the syntax for grep is,
So try this,
This will find all instances of XXXX_PROD in all files starting O*, pipe the results into wc and count the lines. The output number is your total.
(no subject)
(no subject)
(no subject)
no subject
grep -l PROD_XXXX O* | wc -l
grep -l will list the file name that match; wc -l will count them.
Note: you said "word sequence". It gets more awkward if you want to match something with whitespace, or punctuation that might be interpreted by your shell.
(no subject)
(no subject)
(no subject)