¿Cómo puedo hacer esto sin "awk" en el script de shell
Quiero leer un archivo de texto con algunas condiciones.
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: Quiero un script que cuente las direcciones IP en el archivo de texto y me dé un resultado como este
111.196.10.1 8
111.196.10.2 8
111.196.10.3 7
por lo anterior, he escrito un guión
cat file | awk '{print $1}' | sort | uniq -c | sort -nr | awk '{print $2" "$1}'
funciona correctamente, pero quiero esto sin "awk"
2: mi segundo requisito es contar la dirección IP solo cuando el penúltimo número de la línea comience con 2. la salida será
111.196.10.1 7
111.196.10.3 7
111.196.10.2 5
para esto he escrito un guión
grep '^[^"]*"[^"]*" 2' file | cut -d' ' -f1 | sort | uniq -c | sort -nr | awk '{print $2" "$1}'
también funciona correctamente, pero quiero esto sin "awk"
Respuestas
Puede obtener solo la primera columna con Cortar, luego ordenar las IP resultantes y contarlas con Uniq.
cut -f1 -d' ' file | sort -n | uniq -cSi solo desea contar en la línea si el penúltimo número comienza con a
2, simplemente agregue un Grep.grep -E '2[0-9]* [0-9]+"$' t | cut -f1 -d' ' | sort -n | uniq -c
Probado en entrada de muestra.
Sin awk.
grep -oE '([0-9]+\.){3}[0-9]+' Input_file | sort | uniq -c
Con awk:
¿Podría intentar seguir, hacerlo en un solo 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
O si el primer campo es solo la dirección IP, entonces no necesitamos mencionar substren el índice de la matriz que podríamos usar directamente $1como sigue.
awk '
match($1,/([0-9]+\.){3}[0-9]+/){
arr[$1]++
}
END{
for(key in arr){
print key,arr[key]
}
}
' Input_file
Explicación: agregando una explicación detallada de lo anterior.
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.
Puede usar readpara leer dos campos de la entrada y luego repetirlos en un orden diferente.
Así que reemplaza
awk '{print $2" "$1}'
con
while read count ip; do
echo "$ip $count"
done