Count of Multiple Fields(After Validation) in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count of Multiple Fields(After Validation) in UNIX
# 1  
Old 01-01-2013
Count of Multiple Fields(After Validation) in UNIX

Hi ,
i have scenario where i have to get count of each field ,if the field have value equal to "A"
For Example :

Code:
Field_01     Field_02 
   A               A 
   A               B 
   B               A 
   A               A

OUTPUT :

Code:
Field_01    Field_count    Filed_02     Filed_count 
A                  3                    A                3 
A                  3                    B                3 
B                  3                    A                3 
A                  3                    A                3

Thanks
Anudeep

Last edited by Scrutinizer; 01-01-2013 at 11:27 AM.. Reason: code tags
# 2  
Old 01-01-2013
try:
Code:
awk -v s="A" '
{for (i=1; i<=NF; i++) {a[NR,i]=$i ; if ($i==s) c[i]++}}
END {
  for (i=1; i<=NR; i++) {
    for (j=1; j<=NF; j++) {
       printf a[i,j] " " (c[j] ? c[j]:0) " ";
    }
    print "";
  }
}' input

# 3  
Old 01-01-2013
Pls use code tags as advised.
I'm not sure I interpret your requirement correctly, as your desired output does NOT relate to your req.- phrasing (B showing a count of three even though there are only 2 Bs in file, and, moreover, B should not have been included at all!), and you don't specify what to with the header.
However, taking some assumptions, this might do what you want:
Code:
awk -v s="A" '
     NR==1 {for (i=1; i<=NF; i++)  {printf "%s\t%s_count\t", $i, $i} printf "\n"; next}
     {for (i=1; i<=NF; i++) {a[NR,i]=$i ; c[$i,i]+=$i==s}}
     END {for (i=2; i<=NR; i++)
        {for (j=1; j<=NF; j++)
           {printf "%s\t%s\t", a[i,j], c[a[i,j],j];
           }
           print "";
        }
         }
    ' file
Field_01    Field_01_count    Field_02    Field_02_count    
A    3    A    3    
A    3    B    0    
B    0    A    3    
A    3    A    3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Finding total distinct count from multiple csv files through UNIX script

Hi All , I have multiple pipe delimited csv files are present in a directory.I need to find out distinct count on a column on those files and need the total distinct count on all files. We can't merge all the files here as file size are huge in millions.I have tried in below way for each... (9 Replies)
Discussion started by: STCET22
9 Replies

3. Shell Programming and Scripting

How To Count Fields For Cut?

I am new to cut and I want to use the field option with a space delimiter on an Apache log file. For example, if I wanted to find the 200 HTTP code using cut in this manner on the file below cat access_abc.log | cut -d' ' -f7 | grep "200" 157.55.39.183 - - "GET /content/696-news041305... (4 Replies)
Discussion started by: sharingsunshine
4 Replies

4. Shell Programming and Scripting

UNIX append field with comparing fields from multiple column

I have a csv dump from sql server that needs to be converted so it can be feed to another program. I already sorted on field 1 but there are multiple columns with same field 1 where it needs to be compared against and if it is same then append field 5. i.e from ANG SJ,0,B,LC22,LC22(0) BAT... (2 Replies)
Discussion started by: nike27
2 Replies

5. Shell Programming and Scripting

awk - count character count of fields

Hello All, I got a requirement when I was working with a file. Say the file has unloads of data from a table in the form 1|121|asda|434|thesi|2012|05|24| 1|343|unit|09|best|2012|11|5| I was put into a scenario where I need the field count in all the lines in that file. It was simply... (6 Replies)
Discussion started by: PikK45
6 Replies

6. Shell Programming and Scripting

sql loader for inserting the data from multiple fields from unix

Hi , I have my log file something like this (07/29/2009 00:02:24.467) 367518 (07/29/2009 00:02:26.214) 949384011 (07/29/2009 00:02:26.236) 367524 (07/29/2009 00:02:28.207) 949395117 (07/29/2009 00:02:28.240) 337710 (07/29/2009 00:02:30.621) 949400864 I am trying to insert the data... (3 Replies)
Discussion started by: rdhanek
3 Replies

7. UNIX for Dummies Questions & Answers

Unix sort on multiple fields

Hello. I've read a few threads on how to sort on multiple fields, but I still can't get my file to sort correctly. I have a comma delimited .csv file will over a hundred fields. I want to sort it by field 2, field 62 and then field 61 (integer fields). input looks like this well swap field... (2 Replies)
Discussion started by: happy_cow
2 Replies

8. Shell Programming and Scripting

How count number of fields in a record

Dear All , I have the query cat temp.txt |28-07-1997|IF_LEG_DCCT|TOV JV sdfsdfdsfdsfdsCLOSED* KIEV|381015280 I need to count the number of fields in this pipe-seperated file. I beleive this is possible via AWK command. The in above file, output of the count should be 5.... Can some-one... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

9. Shell Programming and Scripting

help me to count no of fields in a file

hi i am a new unix user i want to check whether a file contains spacefied no of fields if so i should delete last fields and then insert some fields in 2nd field please help me Thanks Regards babu :mad: (7 Replies)
Discussion started by: babu@shell
7 Replies

10. UNIX for Dummies Questions & Answers

count fields

Is there a way to count the no. of fields (columns) in a file? Actually I need to cut some fields starting from the middle to the end. How can I specify to cut till last field? thanks in advance :) (4 Replies)
Discussion started by: sskb
4 Replies
Login or Register to Ask a Question