Tahir
Tahir

Reputation: 11

How can i do this without "awk" in shell script

I want to read a text file with some conditions

111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 123"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET /favicon.ico HTTP/1.1" 404 - "
111.196.10.3 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 206"
111.196.10.3 - - [20/Jan/2020:19:43:50 +0200] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 377"
111.196.10.3 - - [20/Jan/2020:07:00:50 +0100] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET /sit-3-shine.7.gif HTTP/1.1" 404 - "
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 375"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET /sit3-shine.7.gif HTTP/1.1" 200 15811"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 375"
111.196.10.3 - - [20/Jan/2020:07:00:50 +0100] "GET /sit3-shine.7.gif HTTP/1.1" 200 15811"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"
111.196.10.3 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 299"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET /sit3-shine.7.gif HTTP/1.1" 200 15811"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 299"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"
111.196.10.3 - - [20/Jan/2020:07:00:50 +0100] "GET /sit3-shine.7.gif HTTP/1.1" 200 15811"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET /favicon.ico HTTP/1.1" 404 -"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "HEAD / HTTP/1.1" 304 299"
111.196.10.3 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 302"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"

1: I want a script that counts IP addresses in the text file and gives me output like this

111.196.10.1  8
111.196.10.2  8
111.196.10.3  7

for the above, I have written a script

cat file | awk '{print $1}' | sort | uniq -c | sort -nr | awk '{print $2" "$1}'

it's properly working but I want this without "awk"

2:my 2nd requirement is, count IP address only when the second last number of the line starts with 2. output will be

111.196.10.1  7
111.196.10.3  7
111.196.10.2  5

for this, I have written a script

grep '^[^"]*"[^"]*" 2' file | cut -d' ' -f1 | sort | uniq -c | sort -nr | awk '{print $2" "$1}'

it's also properly working but I want this without "awk"

Upvotes: 0

Views: 78

Answers (3)

RavinderSingh13
RavinderSingh13

Reputation: 133620

Without awk.

grep -oE '([0-9]+\.){3}[0-9]+' Input_file | sort | uniq -c


With awk:

Could you please try following, to do it in a single awk.

awk '
match($1,/([0-9]+\.){3}[0-9]+/){
  arr[substr($0,RSTART,RLENGTH)]++
}
END{
  for(key in arr){
    print key,arr[key]
  }
}
' Input_file

OR if 1st field is only ip address then we need not to mention substr in array index we could directly use $1 like following.

awk '
match($1,/([0-9]+\.){3}[0-9]+/){
  arr[$1]++
}
END{
  for(key in arr){
    print key,arr[key]
  }
}
'  Input_file

Explanation: Adding detailed explanation for above.

awk '                                 ##Starting awk program from here.
match($1,/([0-9]+\.){3}[0-9]+/){      ##using match function to match IP address regex in current line.
  arr[substr($0,RSTART,RLENGTH)]++    ##Create array arr which has index as sub string of matched regex from RSTART to RLENGTH.
}
END{                                  ##Starting END block of this program from here.
  for(key in arr){                    ##Traversing through arr from here.
    print key,arr[key]                ##printing key and array value here.
  }
}
' Input_file                          ##Mentioning Input_file name here.

Upvotes: 2

Quasímodo
Quasímodo

Reputation: 4004

  1. You can get only the first column with Cut, then sort the resulting IPs and count them with Uniq.

    cut -f1 -d' ' file | sort -n | uniq -c
    
  2. If you only want to count in the line if the second-to-last number starts with a 2, then just add a Grep.

    grep -E '2[0-9]* [0-9]+"$' t | cut -f1 -d' ' | sort -n | uniq -c
    

Tested on sample input.

Upvotes: 2

Barmar
Barmar

Reputation: 781741

You can use read to read two fields from input, then echo them in a different order.

So replace

awk '{print $2" "$1}'

with

while read count ip; do
    echo "$ip $count"
done

Upvotes: 0

Related Questions