count number of nonempty columns in row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count number of nonempty columns in row
# 1  
Old 01-09-2009
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 example, i want to convert

London,a,b,c,,,,->London,a,b,c,,,,,3
York,a,b,,,a,a ->York,a,b,,,a,a,4
# 2  
Old 01-09-2009
Code:
root@isau02:/data/tmp/testfeld> cat infile
London,a,b,c,,,,
York,a,b,,,a,a
root@isau02:/data/tmp/testfeld> awk -F, '{z=0; for (x=2; x<=NF; x++) {if ($x != "") {z++} } {print $0","z} }' infile
London,a,b,c,,,,,3
York,a,b,,,a,a,4

# 3  
Old 01-09-2009
Code:
#! /usr/bin/perl
open FH,"<a.txt";
while(<FH>){
	chomp;
	my @tmp=grep {$_} split(",",$_);
	print $_,",",$#tmp,"\n";
}

Code:
awk -F"," '{
        n=-1
        for(x=1;x<=NF;x++)
                if($x !="")
                        n++
        print $0","n
}' b

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. Shell Programming and Scripting

awk split columns to row after N number of column

I want to split this with every 5 or 50 depend on how much data the file will have. And remove the comma on the end Source file will have 001,0002,0003,004,005,0006,0007,007A,007B,007C,007E,007F,008A,008C Need Output from every 5 tab and remove the comma from end of each row ... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

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

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

5. Shell Programming and Scripting

count the number of instances in 2 columns using awk

Input A.1 Q.1 A.1 Q.2 A.1 Q.3 A.2 Q.4 Explanation: Final Output A.1 Q.1 s1 t1 A.1 Q.2 s1 t2 A.1 Q.3 s1 t3 A.2 Q.4 s5 t5 ---------- Post updated 09-28-12 at 03:38 AM ---------- Previous update was 09-27-12 at 09:10 AM ---------- Hi Guys, I was able to do until... (11 Replies)
Discussion started by: quincyjones
11 Replies

6. Shell Programming and Scripting

Count the number or row with same value in a column

This is the source file, we called it errorlist.out 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_d server_unix_2 CD 196 server_es server_win_1 CD 196 ... (14 Replies)
Discussion started by: sQew
14 Replies

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

8. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies

9. Shell Programming and Scripting

shell script(Preferably awk or sed) to print selected number of columns from each row

Hi Experts, The question may look very silly by seeing the title, but please have a look at it clearly. I have a text file where the first 5 columns in each row were supposed to be attributes of a sample(like sample name, number, status etc) and the next 25 columns are parameters on which... (3 Replies)
Discussion started by: ks_reddy
3 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