Count & Result

Hi All.

I'm trying to count with command

cat Count.log  |sort -t 1 | uniq -c

Input File.

Back-end  Invalid Id Password
Back-end  Invalid Id Password
Success
Success
Success
Back-end  Invalid Id Password
Back-end  User state is invalid
Back-end  User state is invalid
Success
Back-end  Invalid Id Password
Back-end  Invalid Id Password
Success
Balance amount is not enough 
User Not found 
Back-end  Invalid Id Password
Back-end  Invalid Id Password
User error 
Back-end  Invalid Id Password
Back-end  Invalid Id Password
Success 

Output

   9 Back-end  Invalid Id Password
   2 Back-end  User state is invalid
   1 Balance amount is not enough 
   5 Success
   1 User Not found 
   1 User error 

I want out put

Back-end  Invalid Id Password 	:	9
Back-end  User state is invalid 	:	2
Balance amount is not enough  	:	1
Success 				:	5
User Not found  			:	1
User error  			:	1
bash-3.00$ 

Please Hepl me.
Thank

$ awk '{
  A[$0]++
}

END {
  for( a in A )
    printf "%-40s : %5d\n", a, A[a]
}' file1
Back-end  User state is invalid          :     2
Back-end  Invalid Id Password            :     9
User Not found                           :     1
User error                               :     1
Balance amount is not enough             :     1
Success                                  :     6

It's a small modification to preserve the sort order.

1 Like

it work.
Thank you very much.

Similar to Scott's approach, but using Associative Arrays in KSH93:

#!/bin/ksh

typeset -A ARR
while read line
do
        (( ARR["$line"]++ ))
done < Count.log

for k in "${!ARR[@]}"
do
        printf "%-40s : %5d\n" "$k" ${ARR["$k"]}
done

This approach will also preserve the order of records:

Back-end  Invalid Id Password            :     9
Back-end  User state is invalid          :     2
Balance amount is not enough             :     1
Success                                  :     6
User Not found                           :     1
User error                               :     1
1 Like

No need for arrays, neither in awk nor in ksh :

$ cat Count.log |sort -t 1 | uniq -c | while read CNT LINE; do printf "%40s:%5s\n" "$LINE" "$CNT"; done
           Back-end  Invalid Id Password:    9
         Back-end  User state is invalid:    2
            Balance amount is not enough:    1
                                 Success:    5
                          User Not found:    1
                              User error:    1
1 Like

No arrays. Just lots of pipes, and commands and a UUOC :wink:

I just appended to the requestor's command string. You are absolutely right, UUOC. Make it read

$ sort -t 1 file | uniq -c | while read CNT LINE; do printf "%40s:%5s\n" "$LINE" "$CNT"; done 

I know, only joking :slight_smile: It's actually the most obvious solution.

Reading your proposal again, and more carefully, I see that you act on the original file, not on the result of all the pipes. That might indeed be most efficient by far.

awk '{print "|" $0}' logfile | sort | uniq -c | awk -F"|" '{printf("%40s:\t%5s\n", $2, $1)}'