Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting grep output. Bash

trying to format output from grep to make it look better, code is

grep "$1" "$2" | grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" | sort | uniq -c
$ bash myScript.sh "Failed password for root" /home/user/auth.log 
      5 108.166.98.9
   1426 108.53.208.61
      1 113.108.211.131
      1 117.79.91.195
    370 122.224.49.124
   3480 144.0.0.32
     11 162.144.94.250
      6 162.253.66.74
      3 186.67.83.58
      1 222.190.114.98
    205 59.90.242.69
    705 60.172.228.226
      3 64.251.21.104

and want it to look more like

ip: xxx.xxx.xxx.xxx attempts: X
like image 229
moody Avatar asked Apr 17 '26 21:04

moody


1 Answers

Add the following command to the end of your pipe in your script, after uniq:

... | awk '{print "ip: " $2 " attempts: " $1}'

The output will be

ip: 108.166.98.9 attempts: 5
ip: 108.53.208.61 attempts: 1426
...
like image 54
Socowi Avatar answered Apr 19 '26 18:04

Socowi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!