Check for null values in a column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Check for null values in a column
# 1  
Old 04-03-2009
Data Check for null values in a column

Hi All,

I have a file with 10 columns and get the required data for nine columns properly except 8th. In 8th column i have both NULL and NON NULL values...i.e
certain records have values for all the columns including 8th column and certain records have 8th column as NULL.My requisite is,without opening or reading the file line by line i need to check if the file contains atleast one NULL value for 8th column .If NULL value exists then i need to reject the file else process the file.

for eg if my file contains records like this,

1,2,3,4,5,6,7,8,9,10
a,s,d,f,f,g,h,,k,l
aa,ss,bb,ee,ff,gg,bb,,kk,ii

how do i get my job done??
Thanks in Advance

Last edited by ganesh_248; 04-03-2009 at 08:53 AM..
# 2  
Old 04-03-2009
Does something like this work for you?
Code:
 nawk 'FS="," {if ($8 == "") print "Line", NR, "REJECTED" }' file

HTH,
n.
# 3  
Old 04-03-2009
Quote:
Originally Posted by nephros
Does something like this work for you?
Code:
 nawk 'FS="," {if ($8 == "") print "Line", NR, "REJECTED" }' file

HTH,
n.
Have you tried this solution?
# 4  
Old 04-03-2009
Quote:
Originally Posted by vgersh99
Have you tried this solution?
Uhm, yes, works here with the input data from the OP.
# 5  
Old 04-03-2009
Quote:
Originally Posted by nephros
Uhm, yes, works here with the input data from the OP.
Code:
nawk -F, '{if ($8 == "") print "Line", NR, "REJECTED" }' file

# 6  
Old 04-03-2009
Quote:
Originally Posted by vgersh99
Code:
nawk -F, '{if ($8 == "") print "Line", NR, "REJECTED" }' file

Which does the exact same thing unless I'm missing something.
(I do admit that
Code:
 nawk 'BEGIN { FS="," } {foo}'

would have been better.)

Anyway it's a matter of taste, I prefer to set FS inside the script because it's a bit more flexible wrt more complex separators. But it really does not matter for the CSV above.

EDIT: I stand corrected. Behaviour of my original version is highly dependant on the awk implementation used. I works in /usr/bin/nawk from Solaris 10.
Just checked with The One True awk, and my original version does not work there. vgersh99s version and the BEGIN block variation do.

Last edited by nephros; 04-03-2009 at 11:02 AM..
# 7  
Old 04-03-2009
Quote:
Originally Posted by nephros
Which does the exact same thing unless I'm missing something.
(I do admit that
Code:
 nawk 'BEGIN { FS=":" } {foo}'

would have been better.)

Anyway it's a matter of taste, I prefer to set FS inside the script because it's a bit more flexible wrt more complex seperators. But it really does not matter for the CSV above.
You are missing something...Smilie
The FS is changed after the line is read, so it doesn't affect the FS in the current line in the buffer. Your solution gives all the lines of the data from the OP. To get the proper output you have to rearrange the buffer with $0=$0:

Code:
nawk '{FS=",";$0=$0;if ($8 == "") print "Line", NR, "REJECTED" }' file

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check for null values in a columns. I have dozen of CSV files in a directory.

Hi Folks, I'm trying to write a simple file sanity check script. I have a directory with dozen CSV files containing id,edname,firstname,lastname,suffix,email. I like to write a awk script to check if first field contain a number and is not empty. and fields number 3,4 & 6 are not empty and... (3 Replies)
Discussion started by: dc34684
3 Replies

2. UNIX for Dummies Questions & Answers

Check for not null column in a pipe delimited file

Hi, I have a requirement where I have to check whether the mandatory columns in a pipe delimited file is null and print error message. For eg, I have to check if the 3rd,5th,6th,7th and 8th column are null and print the message "<column name> is null". The data file will have aroung 100,000... (6 Replies)
Discussion started by: reshma15193
6 Replies

3. Shell Programming and Scripting

Check increment values in column

Gents, I have a file where i would like to check the constant increment by 2 in column 2. 5450 1000 5450 1002 5450 1004 5450 1006 5465 1000 5465 1002 5465 1006 5465 1008 5550 1002 5550 1004 5550 1006 5550 1008 6830 1000 6830 1002 6830 1008 6830 1010 (6 Replies)
Discussion started by: jiam912
6 Replies

4. Shell Programming and Scripting

Script to check for null values in a column

Hi Guys, I am new to shell script.I need your help to write a shell script. I have a csv file which has 13 columns separated by , I have written below script to fetch required 5 columns. awk -F, '(NR==1){h3=$3;h4=$4;h8=$8;h9=$9;h13=$13;next}(NF>1) \ {print... (5 Replies)
Discussion started by: Vivekit82
5 Replies

5. Shell Programming and Scripting

Check null values column

hi, I had a small question.I had a file from which i need to extract data. I have written the below script to check if the file exists and if it exists extract requierd columns from the file. IFILE=/home/home01/Report_1.csv OFILE=/home/home01/name.csv.out1 if #Checks if file exists... (1 Reply)
Discussion started by: Vivekit82
1 Replies

6. Shell Programming and Scripting

Check to identify duplicate values at first column in csv file

Hello experts, I have a requirement where I have to implement two checks on a csv file: 1. Check to see if the value in first column is duplicate, if any value is duplicate script should exit. 2. Check to verify if the value at second column is between "yes" or "no", if it is anything else... (4 Replies)
Discussion started by: avikaljain
4 Replies

7. Shell Programming and Scripting

Check for null values in columns

Hi , I have below data with fixed with of 52 bytes having three columns value data. 01930 MA GLOUCESTER 02033 02025 COHASSET 01960 MA ... (3 Replies)
Discussion started by: sonu_pal
3 Replies

8. UNIX for Dummies Questions & Answers

shift values in one column as header for values in another column

Hi Gurus, I have a tab separated text file with two columns. I would like to make the first column values as headings for the second column values. Ex. >value1 subjects >value2 priorities >value3 requirements ...etc and I want to have a file >value1 subjects >value2 priorities... (4 Replies)
Discussion started by: Unilearn
4 Replies

9. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 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