Checking for null value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking for null value
# 1  
Old 04-21-2009
Question Checking for null value

Dear All,

I'm glad that I've also joined in this forum. Good work fellas... Smilie
Keep going...! Smilie

I am having a .csv file where I am reading the values with comma(,) as delimiter. After reading each and every values, I would like to check whether it is empty (say null or blank spaces). How to do this using awk command?
Code:
awk 'BEGIN {FS=","}
{ n=split($0,col_value,",")
for(i=1;i<=n;i++) {
if(length(col_value[i]) == 0) {
BLANK_COUNT = BLANK_COUNT+1
}
}
} END {print BLANK_COUNT}' sample.csv



Input File (sample.csv):
RollNo,Name,Mark1,Mark2,Mark3,Mark4
1,Tom,78,,67,98,56
2,Steve,85, ,56,65,67
3,John,96, , ,98

My code is returning 1 blank space for rollno:1 only. Whereas my requirement is to calculate the blank count which includes spaces also.
i.e.,
Rollno, Blank
1,1
2,1
3,2

Thanks in advance...! Smilie

Last edited by ganapathi.t; 04-21-2009 at 06:57 AM..
# 2  
Old 04-21-2009
You have a space in some fields, try this:

Code:
awk -F, '{
  for(i=1;i<=NF;i++){
    if($i==" " || $i==""){
      s++
    }
  }
{
  print NR, s;s=0}
}' file

Regards
# 3  
Old 04-21-2009
It is working fine, if I have only one space. But in my input file, I might have more than single space say " " or " " or " " etc. But I dont know the exact number of spaces.
# 4  
Old 04-21-2009
Quote:
Originally Posted by ganapathi.t
It is working fine, if I have only one space. But in my input file, I might have more than single space say " " or " " or " " etc. But I dont know the exact number of spaces.
In that case:

Code:
awk -F, '{
  for(i=1;i<=NF;i++){
    gsub(" ","",$i)
    if($i==""){
      s++
    }
  }
{
  print NR, s;s=0}
}' file

Regards
# 5  
Old 04-21-2009
Code:
awk -F"," '{j=0;for(i=1;i<=NF;i++){ if($i~/^[ ]+$/){j++}} print j;}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking for null condition in a UNIX variable

i have this code for i in `cat sql_output.txt` do -- some script commands done sql_output.txt has 1 column with employee_ids If the sql_output.txt is null then the do loop should not execute. How can i implement this. for i in `cat sql_output.txt` If i is null or empty then ... (5 Replies)
Discussion started by: rafa_fed2
5 Replies

2. SCO

Stop boot system at "Checking protected password and checking subsystem databases"

Hi, (i'm sorry for my english) I'm a problem on boot sco unix 5.0.5 open server. this stop at "Checking protected password and checking subsystem databases" (See this image ) I'm try this: 1) http://www.digipedia.pl/usenet/thread/50/37093/#post37094 2) SCO: SCO Unix - Server hangs... (9 Replies)
Discussion started by: buji
9 Replies

3. Shell Programming and Scripting

Redirecting standard out to /dev/null goes to file "/dev/null" instead

I apologize if this question has been answered else where or is too elementary. I ran across a KSH script (long unimportant story) that does this: if ; then CAS_SRC_LOG="/var/log/cas_src.log 2>&1" else CAS_SRC_LOG="/dev/null 2>&1" fithen does this: /usr/bin/echo "heartbeat:... (5 Replies)
Discussion started by: jbmorrisonjr
5 Replies

4. Shell Programming and Scripting

NULL checking

Hi I need to check the output of a string to null. For eg the output I get for $KIT is empty. So want to echo something when the output is empty. How can I do that? Thanks in advance (6 Replies)
Discussion started by: Ananthdoss
6 Replies

5. Programming

Checking an array for NULL in C++

Hello All, I have this question that how to check for an array for NULL? For eg. #include<iostream.h> using namespace std; int main() { char arr; if(arr == NULL) { cout<<"NULL"; } } Thanks a lott in advance!!! (3 Replies)
Discussion started by: mind@work
3 Replies

6. UNIX for Dummies Questions & Answers

/dev/null 2>&1 Versus /dev/null 2>1

How are these two different? They both prevent output and error from being displayed. I don't see the use of the "&" echo "hello" > /dev/null 2>&1 echo "hello" > /dev/null 2>1 (3 Replies)
Discussion started by: glev2005
3 Replies

7. Shell Programming and Scripting

Insert string 'NULL' where there is a null value

I have an input file having 7 fields delimited by , eg : 1,ABC,hg,1,2,34,3 2,hj,YU,2,3,4, 3,JU,kl,4,5,7, 4,JK,KJ,3,56,4,5 The seventh field here in some lines is empty, whereas the other lines there is a value. How do I insert string NULL at this location (7th loc) for these lines where... (8 Replies)
Discussion started by: zilch
8 Replies

8. Programming

C++ segmentation fault while checking for null pointer

void disptree(node *ptr) { if ((ptr->left) !=NULL) disptree(ptr->left); cout<<"Position:"<<ptr->pos<<" Data:"<<ptr->data<<endl; if ((ptr->right)!=NULL; disptree(ptr->right); } i'm getting a segmentation fault at the red line. i cannot understand what's the problem.... (3 Replies)
Discussion started by: vijaymrt
3 Replies

9. Shell Programming and Scripting

compare null with non-null

I've got a very peculiar situation. I'm trying to find out if we can compare null fields with non-null. I've output csv files from SQL and Oracle. I need to compare each field from the files, and then find out any differences. The files usualy have over 500 fields, and send the resule to DBA.... (8 Replies)
Discussion started by: nitin
8 Replies

10. UNIX for Advanced & Expert Users

Null Value

Hello All, I have a file of the format **** 123 abc ABC 456 bcd BCD 789 def 112 ghi GHI 223 jkl 344 mno MNO **** I am trying to extract the lines that have no values in the third field (in this case, this would be 789 def 223 jkl Can anyone please help??... (1 Reply)
Discussion started by: Khoomfire
1 Replies
Login or Register to Ask a Question