Counting columns in a delimited record with NULLs


 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Counting columns in a delimited record with NULLs
# 1  
Old 07-20-2009
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 although I was expecting 5 (based on tab delimiters. So actually what I want to do is add the tabs and add 1 to determine the number of words (including NULLs).

0000000 141 142 011 061 062 011 172 171 011 011 071 070 012
a b \t 1 2 \t z y \t \t 9 8 \n

Thanks.
# 2  
Old 07-20-2009
Code:
 
> cat t1.txt
a b      1 2     z y             9 8 
c b      1 2     z y             9 8 
> awk -F "\t" '{x=NF+x} END {print x}' t1.txt
10

# 3  
Old 07-20-2009
Quote:
Originally Posted by honglus
Code:
 
> cat t1.txt
a b      1 2     z y             9 8 
c b      1 2     z y             9 8 
> awk -F "\t" '{x=NF+x} END {print x}' t1.txt
10

This prints a count of records by line... Like:
Line1=4
Line2=4
Line3=5
...
Code:
> cat t1.txt
a b      1 2     z y             9 8 
c b      1 2     z y             9 8 
> nawk -F"\t" '{ print "Line"NR"="NF } ' t1.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a header record (tab delimited) in multiple files

Hi Forum. I'm struggling to find a solution for the following issue. I have multiple files a1.txt, a2.txt, a3.txt, etc. and I would like to insert a tab-delimited header record at the beginning of each of the files. This is my code so far but it's not working as expected. for i in... (2 Replies)
Discussion started by: pchang
2 Replies

2. Shell Programming and Scripting

Counting elements in each record

Hello, I have a file such as below: 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 1I want to count the number of 0 and 1 in each line (. represents no data) and print them into two columns, that is:... (3 Replies)
Discussion started by: Homa
3 Replies

3. Shell Programming and Scripting

Quick Question: counting columns?

Hi everyone I have a quick question... I have an ascii file that looks like the one below, and I wanted to find a way to make a listing of how many columns there is in each row, similar to the example below. Anyone has any ideas of how I can do this? Thanks!! 6 Sep 2008 -158.535 33.6617... (2 Replies)
Discussion started by: lucshi09
2 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

How to make delimited record ????

Can anyone suggest how to make delimited data for below report format Member Nbr Member Name Date Date Number Miles Rejected Reason Purge 1000000000 BROWNS N/B 10121998 01121998 377393930 500 INVALID CUSTOMER NUM issue is that my column name, data and between column i have variable... (7 Replies)
Discussion started by: ns64110
7 Replies

7. UNIX for Advanced & Expert Users

Problem while counting number of fields in TAB delimited file

I'm facing a strange problem, please help me out. Here we go. I want to count number of fields in particular file. filename and delimiter character will be passed through parameter. On command prompt if i type following i get 27 as output (which is correct) cat customer.dat | head -1 | awk... (12 Replies)
Discussion started by: vikanna
12 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. Shell Programming and Scripting

help with awk delimited by |~| in a record

I have a file which contains 1 million records of the following format. Each field is delimited by a pipe tilda pipe "|~|" show below. I would like to print only one column ie the CARDDESC value. for example here it says CARDDESC=A8T1. so anything after CARDDESC= and before |~|CARDTYPE is what... (11 Replies)
Discussion started by: knijjar
11 Replies

10. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question