selenay: (questions/comments)
selenay ([personal profile] selenay) wrote2009-07-27 11:02 am
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:

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 :-(
ext_3440: (Default)

[identity profile] tejas.livejournal.com 2009-07-27 02:32 pm (UTC)(link)
Did you check the man pages on your system?
nic: (Default)

[personal profile] nic 2009-07-27 02:34 pm (UTC)(link)
I only know pieces of Unix, but:

- 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?

[identity profile] gmul.livejournal.com 2009-07-27 03:03 pm (UTC)(link)
You may have it sorted with the pointer towards 'man' from tejas. I'm not sure if you absolutely need the cat - you might be able to get enough with just something like
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...

[identity profile] cobrabay.livejournal.com 2009-07-27 03:35 pm (UTC)(link)
It is possible that your script is running under a different shell to the one you are using when you type the commands in yourself, resulting in commands having different effects.
How are you running your script, and what is the first line of the script? If you type
echo $SHELL
that will show what shell you are using on the command line. If you are using the Korn shell, that should give
/bin/ksh

The first line of your script should contain a line something like the following,
#!/bin/ksh

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,
grep [pattern to find] [file(s) to search]

So try this,
grep 'XXXX_PROD' O*|wc -l >> logfile

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.

Edited 2009-07-27 15:37 (UTC)

[identity profile] sugoll.livejournal.com 2009-07-27 07:26 pm (UTC)(link)
Since you said you wanted count of files, rather than count of lines, then:

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.