Counting elements in each record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting elements in each record
# 1  
Old 11-08-2013
Ubuntu Counting elements in each record

Hello,

I have a file such as below:

Code:
0    0    .    .      0    0
0    0    0    0    0    0
1    1    1    1    1    1
1    1    1    1    1    1
0    0    0    1    0    1

I want to count the number of 0 and 1 in each line (. represents no data) and print them into two columns, that is:

Code:
4 0
6 0
0 6
0 6
4 2

I appreciate your help very much.

---------- Post updated at 08:59 AM ---------- Previous update was at 08:55 AM ----------

By the way, this is the wrong code I have come up with Smilie
Code:
awk '{for (i=1; i<=NF; i++)
    {if ($i == "0") {zero[NR]++}
    if ($i == "1") {one[NR]++}
    }
    {for (j=1; j<=NR; j++)
    print zero[NR], one[NR]
    }
    }' file


Last edited by Homa; 11-08-2013 at 11:04 AM..
# 2  
Old 11-08-2013
Waaay to complicated! Try
Code:
awk '{CNT[0]=CNT[1]=0; for (i=1; i<=NF; i++) CNT[$i]++; print CNT[0], CNT[1]}' file
4 0
6 0
0 6
0 6
4 2

This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-08-2013
Or even simpler:
Code:
awk '{print gsub(/0/,""), gsub(/1/,"")}' file


Last edited by Subbeh; 11-08-2013 at 12:26 PM..
# 4  
Old 11-08-2013
Corrections in your awk code:
Code:
awk '
        {
                for ( i = 1; i <= NF; i++ )
                {
                        if ( $i == "0" )
                                zero[NR]++
                        if ( $i == "1" )
                                one[NR]++
                }
        }
        END {
                for ( j = 1; j <= NR; j++ )
                        print zero[j] ? zero[j] : "0", one[j] ? one[j] : "0"
        }
' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need code for updating second record to first record in shell scripting

Hi,, I have requirement that i need to get DISTINCT values from a table and if there are two records i need to update it to one record and then need to submit INSERT statements by using the updated value as a parameter. Here is the example follows.. SELECT DISTINCT ID FROM OFFER_GROUP WHERE... (1 Reply)
Discussion started by: Samah
1 Replies

2. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

3. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

4. Shell Programming and Scripting

[AWK script]Counting the character in record and print them in condition

.......... (1 Reply)
Discussion started by: Antonlee
1 Replies

5. Shell Programming and Scripting

counting particular record format in a file using AWK

I am trying to count records of particular format from a file and assign it to a variable. I tried below command br_count=wc -l "inputfile.dat"| awk -F"|" '{if (NF != "14") print }' but I amnot able to get it done. Please share me some idea how to get it done. Thanks in advance (7 Replies)
Discussion started by: siteregsam
7 Replies

6. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

7. UNIX for Dummies Questions & Answers

Help with counting string elements

Hi All, I hv several files which have hundreds of lines each for example>XYZ.abc01 NNNTCGGTNNNNNCCACACACMYACACACCCACACCCACSCARCAC I'd like to exculde the first line beginning with ">" and then for the rest of the lines get a count for each string element. So for the above example I would like... (8 Replies)
Discussion started by: pawannoel
8 Replies

8. 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

9. Red Hat

Counting columns in a delimited record with NULLs

I am trying to count the number of columns in a delimited flat file. The record is tab delimited. When I use the command wc -w, I am getting unexpected results when the record contains a NULL value. I believe it is because there is a "word" missing. The below example will return 4 words... (2 Replies)
Discussion started by: nmalencia
2 Replies

10. UNIX for Advanced & Expert Users

Print Full record and substring in that record

I have i got a requirement like below. I have input file which contains following fixed width records. 00000000000088500232007112007111 I need the full record and concatenated with ~ and characters from 1to 5 and concatenated with ~ and charactes from 10 to 15 The out put will be like... (1 Reply)
Discussion started by: ukatru
1 Replies
Login or Register to Ask a Question