Count the number or row with same value in a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count the number or row with same value in a column
# 8  
Old 12-15-2010
Quote:
Originally Posted by sQew
Hi R0H0N,i rdcwayx,

Thanks for the reply, however when i run this script it it prompt for syntax error on line 2,3.
Try nawk instead of awk.
# 9  
Old 12-15-2010
Quote:
Hi R0H0N,

Thanks for the reply, i tried to use your solution here but i think something is missing. On every condition i test, all email is use subject less than 10. Means that it pass the first conditions, i tried to put greater then 10,20,30,40 with same input file but same email coming into my email.

Furthermore, the email contains all lines and i need only line started with 196. What modification can be made on your code here?
Thanks
Test following checkpoints.
1> Echo count before its going to check for if or else condition. Make sure that it is coming correctly.
2> Try to put uniq list in one file and than cat the file and read line by line.

If u are still unable to find out the bug then run this script in debug mode.

Code:
ksh -x scriptname

R0H0N
# 10  
Old 12-15-2010
Quote:
Originally Posted by sQew
Hi Pludi,

I also tested your method, how ever it only printed the total amount of line with 196, ie:19 in this case. Anyway to make it more complete?

Thanks
What do you mean by "more complete"? Send out all 196-lines out in an eMail? Just pipe a grep into your mail program. Only the last 10? Pipe a grep into tail, and then the mail program.
# 11  
Old 12-15-2010
Hi Pludi
I tried to do this first, but got an error:

Code:
bash-3.00# ./script
./script: syntax error at line 3: `errors=$' unexpected
bash-3.00# more script
#!/bin/sh

errors=$( awk '$1 == 196{total++} END{print total}' error_file )
if [ $errors -gt 20 ]
then
  cat error_file | grep "196"
fi
bash-3.00#

I tried to debug, seems its reading the value correctly:


Code:
bash-3.00# bash -x script
++ awk '$1 == 196{total++} END{print total}' error_file
+ errors=27
+ '[' 27 -gt 20 ']'
+ cat error_file
+ grep 196
196    server_a    server_unix_2    CD
196    server_b    server_win_1    CD
196    server_c    server_win_2    CD
196    server_bd    server_unix_2    CD
196    server_bd    server_unix_2    CD
196    server_bd    server_unix_2    CD
196    server_bd    server_unix_2    CD
196    server_bd    server_unix_2    CD
196    server_bd    server_unix_2    CD
196    server_bd    server_unix_2    CD
196    server_bd    server_unix_2    CD
196    server_d    server_unix_2    CD
196    server_es    server_win_1    CD
196    server_f    server_unix_2    CD
196    server_gs    server_win_2    CD
196    server_h    server_win_2    CD
196    server_1    server_unix_3    CD
196    server_ae    server_unix_2    CD
196    server_ca    server_unix_3    CD
196    server_az    server_win_2    CD
196    server_aw    server_win_1    CD
196    server_t    server_win_2    CD
196    server_uv    server_unix_2    CD
196    server_r    server_win_2    CD
196    server_rt    server_win_1    CD
196    server_ut    server_win_2    CD
196    server_zx    server_unix_3    CD

But where the syntax error is?
# 12  
Old 12-15-2010
Apparently, on your system /bin/sh isn't compatible with the newer POSIX specifications. Change #!/bin/sh to #!/usr/bin/bash (or whereever your version of bash resides), and it should work.
# 13  
Old 12-15-2010
Quote:
Originally Posted by sQew

Hi rdcwayx,

Thanks for the reply, however when i run this script it it prompt for syntax error on line 2,3.
run by nawk or /usr/xpg4/bin/awk
# 14  
Old 12-15-2010
Quote:
Originally Posted by rdcwayx
Code:
awk '{a[$1]++}
END{for (i in a) {if (a[i]>10) {c=(c=="")?i:c FS i}
                        else {b=(b=="")?i:b FS i}
                 }
       print "code than 10:", c |"mailx your@mail.com";
       print "code not than 10:", b|"mailx other@mail.com";
}' infile


Hello
I could not get few things in END.
like : c=(c=="")?i:c FS i and b=(b=="")?i:b FS i.
I know, ternary operator, but could not get this c=(c=="") and also what is the use of FS (field separator ) in this piece of code.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reseting row count every given number of rows

I have a file with 48 rows. I am counting 6 rows and adding 6 to that number and repeating the operation, and then output the value in column 1. For the second column, I would like to get sort of a binary output (1s and 2s) every 3rd row. This is what I have: awk '{print ++src +... (1 Reply)
Discussion started by: Xterra
1 Replies

2. UNIX for Beginners Questions & Answers

Copy columns from one file into another and get sum of column values and row count

I have a file abc.csv, from which I need column 24(PurchaseOrder_TotalCost) to get the sum_of_amounts with date and row count into another file say output.csv abc.csv- UTF-8,,,,,,,,,,,,,,,,,,,,,,,,, ... (6 Replies)
Discussion started by: Tahir_M
6 Replies

3. UNIX for Dummies Questions & Answers

I want count of number of records to be printed on each row.

we want the count of number of records to be printed on each row. For Ex: if there are 5 records on one unique id , the count "5'' should be printed on each record in other column. Please help for this. I am using unix & Cygwin. Below are sample records: KCZ0650473... (2 Replies)
Discussion started by: ElijaRajesh
2 Replies

4. Shell Programming and Scripting

how to add the number of row and count number of rows

Hi experts a have a very large file and I need to add two columns: the first one numbering the incidence of records and the another with the total count The input file: 21 2341 A 21 2341 A 21 2341 A 21 2341 C 21 2341 C 21 2341 C 21 2341 C 21 4567 A 21 4567 A 21 4567 C ... (6 Replies)
Discussion started by: juelillo
6 Replies

5. Linux

Serial terminal emulation - bad row column count ?

Hello, I connect to linux using serial cable from windows machine. I use putty as serial terminal emulator. Everything works fine except programs which scroll text - man, more, less, vi, etc.. These programs asumme my terminal size is 80cols x 24rows (my putty window size is more than that,... (1 Reply)
Discussion started by: vilius
1 Replies

6. UNIX for Dummies Questions & Answers

Adding a column with the row number using awk

Is there anyway to use awk to add a first column to my data that automatically goes from 1 to n , where n is the numbers of my rows?:confused: (4 Replies)
Discussion started by: cosmologist
4 Replies

7. UNIX for Dummies Questions & Answers

deleting a row if a certain column is below a certain number

How can you delete a row if a certain column is bigger than a certain number? I have the following input: 20080709 20081222 95750 1 0 0.02 94.88 20080709 20081222 95750 2 0 0.89 94.88 20080709 20081222 9575 1 0 0 94.88 20080709 20081222 9575 2 0 0 94.88 20080709 20081222 9587.5 1 0 0... (6 Replies)
Discussion started by: Pep Puigvert
6 Replies

8. UNIX for Dummies Questions & Answers

deleting a row if a certain column is below a certain number

How can you delete a row if a certain column is bigger than a certain number? I have the following input: 20080709 20081222 95750 1 0 0.02 94.88 20080709 20081222 95750 2 0 0.89 94.88 20080709 20081222 9575 1 0 0 94.88 20080709 20081222 9575 2 0 0 94.88 20080709 20081222 9587.5 1 0 0... (1 Reply)
Discussion started by: Pep Puigvert
1 Replies

9. Shell Programming and Scripting

count number of nonempty columns in row

Hi, Suppose i have a inputfile in csv format. How to use awk to count 'the number of nonempty columns in each row' minus one, and add the value as a new column in the end For cosmetic reason, it's even better to include a descriptive label for the last column in the first row. for... (2 Replies)
Discussion started by: grossgermany
2 Replies

10. UNIX for Dummies Questions & Answers

row count but only number part

hi i am pretty new to unix .i am ETL guy I need a unix script to take row count of a file and write it to another file the problem with wc-l is it include filename also wc -l abc.dat will give me like 1000 abc.dat i just want 1000 to be written can u just take 2 min to write a simple... (1 Reply)
Discussion started by: er_zeeshan05
1 Replies
Login or Register to Ask a Question