Skip to content
Advertisement

Loop to filter out lines from apache log files

I have several apache access files that I would like to clean up a bit before I analyze them. I am trying to use grep in the following way:

JavaScript

I have several terms that I want to grep, so I am piping every grep action as follow:

JavaScript

Until here my rudimentary script works as expected! But I have many apache access logs, and I don’t want to do that for every file. I have started to write a bash script but so far I couldn’t make it work. This is my try:

JavaScript

Could anyone point me out what I am doing wrong?

Advertisement

Answer

If you have a variable and you append _clean to its name, that’s a new variable, and not the value of the old one with _clean appended. To fix that, use curly braces:

JavaScript

Without it, your pipeline tries to redirect to the empty string, which results in an error. Note that "$file"_clean would also work.

As for your pipeline, you could combine that into a single grep command:

JavaScript

No cat needed, only a single invocation of grep.

Or you could stick all your terms into a file:

JavaScript

and then use the -f option:

JavaScript

If your terms are strings and not regular expressions, you might be able to speed this up by using -F (“fixed strings”):

JavaScript

I think GNU grep checks that for you on its own, though.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement