selenay: (questions/comments)
[personal profile] selenay
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 :-(

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

Date: 2009-07-27 02:49 pm (UTC)
ext_3440: (Default)
From: [identity profile] tejas.livejournal.com
Not a problem. While I'm not a coder, I've worked on UNIX systems for years. Man pages are your friend. They're the online manual for UNIX. At the prompt type [man help] (or way, maybe it's help man, been a while). That should give you the necessary intro into how to use the manual. You might be able to find what you need there... or at least better search criteria for google.

Date: 2009-07-27 04:02 pm (UTC)
paranoidangel: PA (Default)
From: [personal profile] paranoidangel
Man's great, although at uni someone had written some code that meant that typing woman did the same thing :)

(I know how to use Unix since the Unix machines at uni were better than the Windows ones, I just don't know how to program in it)

Date: 2009-07-27 02:34 pm (UTC)
nic: (Default)
From: [personal profile] nic
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?

Date: 2009-07-27 03:03 pm (UTC)
From: [identity profile] gmul.livejournal.com
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...

Date: 2009-07-27 03:08 pm (UTC)
From: [identity profile] gmul.livejournal.com
No, forget the Or part of that at least for now...

Just grep 'XXXX_PROD' O* might do the trick - give you a list of files that have a match to the pattern in. Then you just have to count the number of lines somehow.

Date: 2009-07-27 03:22 pm (UTC)
From: [identity profile] gmul.livejournal.com
I'm still not sure cat is counting the right thing but that's probably my understanding.

Let me see who's in along the way, I'm sure my neighbour Dave will know and he was in earlier... Back soonish!

Date: 2009-07-27 04:36 pm (UTC)
From: [identity profile] gmul.livejournal.com
The committee here also came up with the wc mentioned below, we just spent too long talking about bikes for me to get back and post it. :-)

Date: 2009-07-27 03:35 pm (UTC)
From: [identity profile] cobrabay.livejournal.com
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 Date: 2009-07-27 03:37 pm (UTC)

Date: 2009-07-27 03:44 pm (UTC)
From: [identity profile] cobrabay.livejournal.com
Update: I forgot to add that this method only works of you only have one instance of XXXX_PROD per line.

I'm not sure why you are using
print cat O*
, as just
cat O*
should work.

Date: 2009-07-27 04:20 pm (UTC)
From: [identity profile] cobrabay.livejournal.com
The O'Reilly books are pretty good for this, "Unix in a Nutshell" is a classic. Their "Learning the bash Shell: Unix Shell Programming" is pretty good too if you have to write many shell scripts, n.b. bash is more feature rich and newer version of sh, available on most Unixes, and all Linuxes, (and I much prefer it to Korn shell), but there is enough overlap to make this book generally useful for shell programming.

Date: 2009-07-27 07:26 pm (UTC)
From: [identity profile] sugoll.livejournal.com
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.

Date: 2009-07-27 10:21 pm (UTC)
From: [identity profile] sugoll.livejournal.com
Good news that it's not a pattern with whitespace.

I trust all the files are in the same directory? If they're scattered through subdirectories, then "man find" is your friend.

Incidentally, on the subject of reference books: the basics of this stuff haven't changed much since 1970. There are lots of bells and whistles added, but the core points of shell programming are still valid. Which means you might be able to pick up a basic introductory reference that's a couple of decades old for next to nothing, in second-hand bookshops around an academic establishment. Once you have the basics, then you can dig around a lot on your current system using "man", which will save on the cost of a hefty new tome.

Profile

selenay: (Default)
selenay

December 2024

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930 31    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated May. 16th, 2025 11:57 am
Powered by Dreamwidth Studios