Count & Result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count & Result
# 1  
Old 05-23-2013
Wrench Count & Result

Hi All.

I'm trying to count with command
Code:
cat Count.log  |sort -t 1 | uniq -c

Input File.
Code:
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
Code:
   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
Code:
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
# 2  
Old 05-23-2013
Code:
$ 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.
This User Gave Thanks to Scott For This Post:
# 3  
Old 05-23-2013
it work.
Thank you very much.
# 4  
Old 05-23-2013
Similar to Scott's approach, but using Associative Arrays in KSH93:
Code:
#!/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:
Code:
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

This User Gave Thanks to Yoda For This Post:
# 5  
Old 05-23-2013
No need for arrays, neither in awk nor in ksh:
Code:
$ 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

This User Gave Thanks to RudiC For This Post:
# 6  
Old 05-23-2013
No arrays. Just lots of pipes, and commands and a UUOC Smilie
# 7  
Old 05-23-2013
I just appended to the requestor's command string. You are absolutely right, UUOC. Make it read
Code:
$ sort -t 1 file | uniq -c | while read CNT LINE; do printf "%40s:%5s\n" "$LINE" "$CNT"; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help with grep & count in ksh

Hello, I have input file with queue names No.esprd.Queue|No.esdev.Queue|||n|120|No_User||No_User| No.esdev.Queue|No.esdev.Queue|||n|120|No_User||No_User| I have to check if the input file contains word "esprd" and the number of times it occurs. I will have to do further... (22 Replies)
Discussion started by: Green_Star
22 Replies

2. Shell Programming and Scripting

Pattern matching & storing result in variable

Hi! i'm trying to parse textfiles against a pattern and storing the result in a variable. The strings i want to get are embraced by and can occur several times in one line, so e.g. some text anything else endwhat i have so far: #!/bin/bash for f in $* do exec 3<&0 exec 0<$f ... (2 Replies)
Discussion started by: thoni
2 Replies

3. Shell Programming and Scripting

Count number of files and use result as variable

Hi there I have a .ksh script that I am using on an AIX ( Actual Level 5.3.10.0, Maintenance Level 5.3.0.0) where I am logging into a windows box, doing a file count on that server and returning the output to the UNIX session. I would like to exit the script at this point in time if the... (10 Replies)
Discussion started by: jimbojames
10 Replies

4. Shell Programming and Scripting

Need to count files & create log of that.

Hi Friends, I need some help. First look at my files hierchachy /<level-1>/<level-2>/<level-3>/*.tif eg. : /2010-07-01/AFFIDAVIT-OF-SERVICE---FOR-SC/001/Babylon2_20100701012049_1278004849.49892_000.tif... (2 Replies)
Discussion started by: paragnehete
2 Replies

5. Shell Programming and Scripting

Beginner: Count & Sort Using Array's

Hi, I'm new to linux & bash so please forgive my ignorance, just wondering if anyone can help. I have a file (mainfile.txt) with comma deliminated values, like so: $1 $2 $3 613212, 36, 57 613212, 36, 10 613212, 36, 10 677774, 36, 57 619900, 10, 10 i need to split this file... (12 Replies)
Discussion started by: BigTOE
12 Replies

6. UNIX for Dummies Questions & Answers

syntax for counting & printing record count

Hi I have a complex script which outputs a text file for loading into a db. I now need to enhance this script do that I can issue an ‘lp' command to show the count of the number of records in this file. Can anybody give me the necessary syntax ? (2 Replies)
Discussion started by: malts18
2 Replies

7. UNIX for Dummies Questions & Answers

Grep char count & pipe to sed command

Hi I am having a 'grep' headache Here is the contents of my file: (PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1))) I would like to count out how many times 'PBZ' occurs and then place that number in the line above 3... (8 Replies)
Discussion started by: cavanac2
8 Replies

8. Shell Programming and Scripting

Edit field count result

Hi all, I'm trying to count the fields in a file and I'm not getting back the desired result. I was wondering if anyone can help. This is the code: #!/bin/bash a=$(awk '{print NF}' foobar) if then echo "this works" else echo "no it's $a instead" fi The foobar file contains... (1 Reply)
Discussion started by: nistleloy
1 Replies

9. UNIX for Dummies Questions & Answers

search& count for the occurence of a word

Greetings, I need to search and count all the occurences of a word in all the files in a directory. Any suggestions greatly appreciated. Thanks (1 Reply)
Discussion started by: skoppana
1 Replies

10. UNIX for Dummies Questions & Answers

Line Count & MAil Issues

Please help...I forgot how to do a linecount in Solaris. I'm list a report & I'm trying to count how many instances. Also, I need to read & clean up mail for root. What directory is that again. Thanks (3 Replies)
Discussion started by: Remi
3 Replies
Login or Register to Ask a Question