T Nation

UNIX Geeks?

Help! Are there any shell scripters around?

I am writing a simple script to return some basic statistics captured in a systems log; however, I am having the following issue:

first I define a variable as a list returned by a grep search:

VAR_LIST=grep "some_expression" some.log | sort -u

Output:

xxx aaa_bbb_ccc
yyy aaa_bbb_ccc
xxx ccc_ddd_eee

Next I use a for loop to sweep through the list to do a computation:

for VAR in $VAR_LIST
do
grep $VAR some.log | compute some value and return it
done

The problem is that instead of grepping “xxx aaa_bbb_ccc” it greps “xxx” and “aaa_bbb_ccc” separately. How do I just get each line and not every “word” in the line to be treated as a variable?

I am running bash v3.0 on Solaris v10.5

Thanks!!

grep “$VAR” some.log

I think wrapping the pattern in double quotes will give the desired behavior.

Yeah, I should have stated that I have tried all of the below patterns to no avail:

“$VAR”
{$VAR}
“{$VAR}”
{"VAR"} {VAR}
"{VAR}" {“VAR”}

I don’t think the problem is with the grep expression and how I am “protecting” the variable name. I think it has to do with the variable defintion statement.

Try this:

grep “some_expression” some.log | sort -u | while read VAR; do
grep $VAR some.log | compute some value and return it
done

Just out of curiosity are you wanting the info displayed on a screen from a log?

Normally I just type for example:

tail -f /var/log/maillog | grep User@domain.XXX

This for me returns a log to the screen of all mail traffic for that user and its live. Good for watching mail traffic on the fly. Or what ever.

And if I want it to store to a file you can just redirect the output for later use. Or further sorting.

[quote]dirtbag wrote:
Just out of curiosity are you wanting the info displayed on a screen from a log?

[/quote]

I am going to output it to a csv file so I can open it in a spreadsheet.

[quote]pookie wrote:
Try this:

grep “some_expression” some.log | sort -u | while read VAR; do
grep $VAR some.log | compute some value and return it
done

[/quote]

Close! have to use “$VAR”.

When you loop through your VAR_LIST variable the for loop is just going to break it into chunks based on whitespace, i.e. space or newline. Try splitting it into a list based on newlines first.

VAR_LIST=grep "some_expression" some.log | sort -u | split '\n'

You might have to use something different for the newline character. I seem to remember Solaris being weird in that regard.