Linux Shell Script: how to compare a specific field in a text document with specific text in an if statement

I have a file called transfer.log that has several Apache logs in it. I need to calculate how many GET requests each IP address registers. I know how to access the file and iterate over the lines in the file, but I am having problems comparing the 6th field in each line with "GET".

#!/bin/bash    

while read p; 
do
    name=( $(awk '{print $6}' p))
    echo $name
    if [ "$name" == "GET" ]
    then
            echo "yes"
    else
        echo "no"
    fi
done < transfer.log

Currently, when I run the script "no", it prints 5 times and I get an error message in which awk cannot open the file "p". When I change p to transfer.log in a variable declaration, I can get echo $ name to output "GET (with a quote), but it obviously never changes because it accesses the whole file, not the new line p .

, p , while. , , 5 , 6.

transfer.log :

140.211.167.27 - - [15/Oct/2012:23:11:38 +0000] "GET / HTTP/1.1" 200 2963 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4"
140.211.167.27 - - [15/Oct/2012:23:11:46 +0000] "GET /systems/ganeti/index HTTP/1.1" 200 5918 "https://wiki.osuosl.org/systems/index" "Mozilla/5.0(X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4"
140.211.167.9 - - [15/Oct/2012:23:17:33 +0000] "GET /resources/index HTTP/1.1" 200 3411 "https://wiki.osuosl.org/index" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1"
140.211.167.25 - - [15/Oct/2012:16:02:07 +0000] "GET /index HTTP/1.1" 200 2673 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1"
66.249.74.101 - - [15/Oct/2012:02:20:14 +0000] "GET /robots.txt HTTP/1.1" 404 2458 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
128.193.164.34 - - [15/Oct/2012:12:41:18 +0000] "POST /rpc/xmlrpc HTTP/1.0" 200 8328 "-" "PHP XMLRPC 1.0"

, , GET IP- GET.

+4
3

awk:

$ awk '{if($6=="\"GET")ip[$1]++; else ip[$1]+=0}END{for(elem in ip){print elem, ip[elem]}}' input.log | sort -k2nr
140.211.167.27 2
140.211.167.25 1
140.211.167.9 1
66.249.74.101 1
128.193.164.34 0

:

  • {if($6=="\"GET")ip[$1]++; else ip[$1]+=0} 6- , "GET, , ip; 6- "GET, 0 , ip, POST, , id.
  • ip GET
  • sort,
+3

:

name=( $(awk '{print $6}' p))

:

name=$(echo "$p" | awk '{print $6}')

p, , awk, . , .

+2

, :

#!/bin/bash
howmanyGET=0
loopcounter=0
while read line;do
    #echo "Line # $loopcounter: $line"
    ((loopcounter++))
    name=`echo $line | awk '{print $6}'`
    #name=( $(awk '{print $6}' p))
    #echo $name
    name=${name:1:3}
    echo $name
    if [ "$name" == "GET" ]
    then
        echo "yes"
        ((howmanyGET++))
    else
        echo "no"
    fi
done < transfer.log

echo "GET: $howmanyGET"
echo "loop: $loopcounter"

:

$ bash counter.sh 
GET
yes
GET
yes
GET
yes
GET
yes
GET
yes
POS
no
GET: 5
loop: 6

.

+1

Source: https://habr.com/ru/post/1694632/


All Articles