Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need help with this script This script analyses apache_access.log through a filter.txt, if exist the term in filter the script will do something. My problem i think is in the first command "for", I am not able to use the filter line by line.

can someone help me?

for term  in `tail -n $LINESTOSEARCH $IPEATERFILTER`
    for ip in `tail -n $LINESTOSEARCH $LOGFILE | grep "$TERM" | awk "{print \\$1}" | sort | uniq -c | sort -rn | head -20 | awk "{if (\\$1 > $THRESHOLD) print \\$2}"`
        # Look in iptables to see if this IP is already banned
        if ! iptables -L INPUT -n | grep -q $ip

     # Do something


share|improve this question
up vote 1 down vote accepted

You don't want to read lines with for. Use while and read instead. The problem is that the shell will split arguments on whitespace, so any space in the log file's line will actually cause the loop to run another time.

tail -n $LINESTOSEARCH $IPEATERFILTER | while read -r line; do
    # do something with "$line"

Repeat the same pattern for inner loops.

share|improve this answer
Thank you for explanation. Your command only read the first line in filter. – Rui Branco Jul 9 '14 at 14:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.