-1

The log file is as below:-

Source=Mobile
IP=189.23.45.01
STATUS=SUCCESS
TIME=10 sec

Source=Desktop
IP=189.23.34.23
STATUS=FAIL
TIME=101 sec

Source=Mobile
IP=189.23.34.23
STATUS=FAIL
TIME=29 sec

File keep going so on.

Questions:

  1. Find IP where status is FAIL?
  2. Find Ave time taken by all request where status is "success"?
  3. List how many logins were via Mobile and how much time did it took ?
Machine
  • 99
  • 2
  • 8
  • If the entries of the log are consistent (four lines, third being status), then you can list only the failed ones by utilising greps context line controls. e.g. grep -B2 -A1 STATUS=FAIL – steeling Jan 05 '19 at 17:51
  • @Machine Could you please clarify what you mean with "...and how much time did it took"? Do you want to know the overall time involved in Mobile-logins, successful and failed? An average? A overal or average calculation for only successful or only failed mobile logins? – ozzy Jan 05 '19 at 18:16

3 Answers3

1

In an unpolished version, a Bash-script could look like this, assuming that your data is contained in datafile:

#!/bin/bash

printf "IPs where status is fail:\n"
grep -z -oP 'IP=\K.*\n(?=STATUS=FAIL)' datafile

printf "Avg time taken by all requests where status is 'success':\n"
grep -z -oP 'STATUS=SUCCESS\nTIME=\K\d+' datafile | \
  awk '{ total += $1; count++ } END { print ( count == 0 ? "NaN" : total/count); }'

printf "Number of logins (successful and failed) via Mobile:\n"
grep -c 'Source=Mobile' datafile

A brief elucidation:

  • Q2) Calculation of the Average time: the grep command extracts the time values (which are assumed to be all in seconds). These values are piped into the awk command, which calculates their average, and then prints that average.
ozzy
  • 845
  • @Machine What did you mean with "...and how time did they sent" ? – ozzy Jan 05 '19 at 17:56
  • can u explain what exactly u doing in second command to find Avg? – Machine Jan 05 '19 at 17:59
  • @Machine Of course, if you elucidate your question :-) – ozzy Jan 05 '19 at 18:01
  • 1
    @steeldriver Sorry. It seems I spoiled the fun... Perhaps we should leave it to Machine to figure out what the commands do precisely. Specific questions will be answered then... – ozzy Jan 05 '19 at 18:07
1

You're really asking 3 questions - I'll get you started with the first one, and you should make an effort to solve the other two yourself using the same basic structure (there are plenty of examples on this site for using Awk to do numerical processing such as averaging):

Use Awk in paragraph mode (by unsetting the record separator, RS), splitting the record into fields using = and \n (newline):

$ awk -vRS= -F'[=\n]' '/STATUS=FAIL/{print $4}' file.log
189.23.34.23
189.23.34.23
steeldriver
  • 81,074
-2
read -p "Lets Give File Name , placed in the same dir: " file ; 
echo " Ques : Find the Number of IP which failed "
echo "Ans: "

cat "${file}".txt | grep -i STATUS=FAIL -B1 | grep -i IP | awk -F '=' '{print $NF}'
#cat "${file}".txt | grep -i STATUS=SUCCESS -A1 | grep -i Time | awk -F '=' '{print $NF}' &> clear2.txt

echo "Ques : Find the avg of success time"
echo "Ans : "

cat "${file}".txt | grep -i STATUS=SUCCESS -A1 | grep -i Time | awk -F '=' '{print $2}' | awk '{print $1}' &> clear2.txt
avgtime=0
i=0
for x in `cat clear2.txt`
do  
    i=$(($i + 1))
    avgtime=$(($avgtime +$x))
    echo "avg time after ${i} iteration is :${avgtime}"
    y=$(($x))
done
#echo "${x}"
#echo "${y}"
avgtime=$(($avgtime/$i))

echo "THe avg time is : ${avgtime}"

echo "Ques : What is the Number of time mobile was tried to login"

echo "Ans :"

cat testfile.txt | grep -i Mobile | wc -l