My Profile

Keep Up to Date:
Blog RSS
Blog
Forum RSS
Forum
Post New Topic Post Reply
Posted 4 Months, 3 Weeks ago
Elaine
Senior Boarder
Posts: 78
graphgraph
User Offline
 
I'm messing around with some simple shell scripts and I was hoping if someone can explain a couple of things to me. As a quick way to look at my access_log and error_log files while excluding junk I just made a couple scripts called hits and errors which contains:

cat /var/log/access_log grep -v favicon grep -v robots grep -v sumthin grep -v script

and

cat /var/log/error_log grep -v favicon grep -v robots grep -v sumthin grep -v script

Is there a way that I can move each of the key words I want to grep out onto a seperate line as an easier way to work with the file? I obviously can't do something like:

cat /var/log/access_log grep -v favicon grep -v robots grep -v sumthin grep -v script

because it'll treat each line as a seperate command. I've tried a few different ways to get this to work, but I haven't had any luck. Anyone know of a simple way of doing this?

I guess another neat thing would be to have all of the stuff I want to exclude in a seperate file which I can just add onto. For example, have a file called 'exclude' which contains all of the words and phrases i want to exclude. Then I can just feed that file into cat /var/log/access_log and cat /var/log/error_log.

Anyway, if anyone has a few quick tips I'd appreciate it. I bought 'Unix Shell Programming' by Stephen G. Kochan and Patrick H. Wood which so far I'm learning a lot from, but I haven't come across anything yet that would help me do the things I mentioned here.

Thanks a lot,
The administrator has disabled public write access.
Posted 4 Months, 3 Weeks ago
armyman
Senior Boarder
Posts: 71
graphgraph
User Offline
 
<snip> Hi, changing the above to :-

cat /var/log/access_log grep -v favicon grep -v robots grep -v sumthin grep -v script

would work, and is probably theeasiest way around this problem. The character at the end of the line escapes the newline character, making the shell treat the above as a single line.
The administrator has disabled public write access.
Posted 4 Months, 2 Weeks ago
dachs
Senior Boarder
Posts: 71
graphgraph
User Offline
 
You don't need to cat files to grep. Use the syntax

grep pattern file

Consider doing something like this,

#/bin/sh

p1=favicon p2=robots p3='pattern with spaces'

for i in /var/log/access_log /var/log/error_log do # note that the quotes are important, because if # any of the variables contain whitespace, the # second word will be the 'file' argument to # grep, and you'll get an error message saying # there is no such file. grep -v '${p1}' ${i} grep -v '${p2}' grep -v '${p3}' done

List your patterns one line at a time in file 'exclude',

favicon robots sumthin script patterns with spaces

Your script would then be something like,

#/bin/sh

for i in /var/log/access_log /var/log/error_log do grep -v -f ./exclude ${i} done

Read man pages too. Read the man page for *every* command you use in a shell script. There is just no end to the creative ideas you'll
The administrator has disabled public write access.
 
Copyright © 2006 - Nov 2008 My Linux Gang