To find record having null value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To find record having null value
# 1  
Old 12-21-2016
To find record having null value

Hi All

My requirement is to find the null values in particular column of a file and reject it in case if it contains null values. But the challenge is that I want a common command which can be used across different file, as the position of the column we need to check for different file may get vary.

For ex. in the first file we need to check null values in 2nd & 3rd column, where as in the second file we need to check in 4th, 5th & 6th column.All my files are pipe delimited

Thanks in advance
# 2  
Old 12-21-2016
Hello ginrkf,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.


Regards,
Robin
# 3  
Old 12-21-2016
Hi Rbatte

Sorry for not providing much details on this. Please see my response below.

Is this homework/assignment? There are specific forums for these.
--> This is project requirement for me, where I need to implement for all the incoming files
What have you tried so far?
--> I tried different by using awk command
Code:
awk -F ',' $2!=''||$3!=''

But the problem is that the position of the column where I need to check null values vary.More over I am not that expert in UNIX.

What output/errors do you get?

--> I got my desired o/p, but which can used for only for specific file.I am not able to come up with a shared command which can be used across all the files in such a way that, we can pass the column position as parameter.
What OS and version are you using?

--> I am using UNIX

What are your preferred tools? (C, shell, perl, awk, etc.)

--> awk

Hopes this helps..Kindly let me know if you need more detail
# 4  
Old 12-22-2016
Please, could you provide a realistic input sample of your file?
Please, could you provide a sample of the kind of output you want based on that input file?
# 5  
Old 12-22-2016
Hi Please find my sample file

File1
Code:
123,asd,fgh,nmb,pok
134,,fg,mnm,mnb

For the above file I need to reject the second record as the second column contain null value.

File2

Code:
1235,afg,fgdf,nmbs,posk
1345,,fg,,mnsb

For file 2 by requirement is different where I need to reject a record where 2nd and 4th column is null

So I want to write a unique command where I can pass the column position to check where it is null or not.For ex. I want to pass column position as 2 for the first file and column position as 2&4 for the second file
# 6  
Old 12-22-2016
Hello ginrkf,

From this awk -F ',' $2!=''||$3!='' I can see that you are separating fields on the comma but then I get confused. I think you test that the 2nd field is not null OR the 3rd field is not null.

What is the end goal? I'm wondering if a grep with an extended regular expression might be better, something like this:-
Code:
grep -Ev "^[0-9]*,," file1

This is excluding (-v) lines that match the pattern "Any line starting with zero or more digits followed by two commas (i.e. a null value)"

If that's okay, then we could do something similar for file2, but I'd rather not leap into that if the above is not what you want. The rules can be adjusted to take account of your real data, of course.

Does this help?



Robin
# 7  
Old 12-22-2016
Try:
Code:
awk -F, 'FNR==1{split(c,C)} {p=0; for(i in C) if ($C[i]!="") p=1} p' c=2 file1 c=2,4 file2

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Identify empty file with null record

Hi Team, I have a file abc.dat which is a empty file. But it has null record in first line. I need to identify this unique file and handle it separately. scenario 1: abc/dw> wc abc.dat 1 0 1 abc.dat abc/dw> cat abc.dat abc/dw> scenario 2: abc/dw> wc pqr.dat 0 0 0 pqr.dat... (3 Replies)
Discussion started by: kmanivan82
3 Replies

2. Shell Programming and Scripting

How to find null column?

hi Gurus, I need find the null column in a file. my file like below abc, ,cde,def abc,ded,cdd,def abc, ,ddd,ccd aaa,bbb,ccc,ddd basic, I need to find the lines which second column is null Thanks in advance (3 Replies)
Discussion started by: ken6503
3 Replies

3. Shell Programming and Scripting

Egrep ERE to find null

Hello All, My apologies if a similar question has already been posted. I searched for "grep", "egrep" and "null" I'm having an issue with egrep and an extended regular expression. I'm doing a test for a job name. I would like to have the user enter a job name with no spaces and also test to... (7 Replies)
Discussion started by: sydox
7 Replies

4. Shell Programming and Scripting

how to find null column

Hi, everyone I have a requirement as following: source file 1, abc, def, caaa 2, , cde, aaa 3, bcd, , adefefg I need find columns which contains null value, in above example, I need get two rows 2, , cde, aaa 3, bcd, , adefefg anybody has idea how to achive this ... (5 Replies)
Discussion started by: ken002
5 Replies

5. Shell Programming and Scripting

Find out if few fields in a file are null

Hi, I've a pipe delimited file where I want to find out a number of lines where 1st 2nd and last field are null using awk/sed. Is it possible? Thanks (5 Replies)
Discussion started by: rudoraj
5 Replies

6. UNIX for Dummies Questions & Answers

Using /dev/null with grep and find

Hi, I am trying to display the filename in which a string was found after using find and grep. For this after some googling I found that this works: find -name "*.java" -exec grep "searchStr" {} /dev/null \; I wanted to know the difference between the above and the following: find -name... (0 Replies)
Discussion started by: gaurav_s
0 Replies

7. Shell Programming and Scripting

How can find Null value in If condition

Hi, i wrote If Conditions in my script, it's returns null and some values. but i am unable to find when Null value getting. bec we need modification according null vales. pls help me on this. (2 Replies)
Discussion started by: koti_rama
2 Replies

8. Shell Programming and Scripting

Find null fields in file

Hi All, I have some csv files out of which i want to find records which have empty values in either the 14th or 16th fields. The following is a sample. $cut -d',' -f14,16 SPS* | head -5 VOIP_ORIG_INFO,VOIP_DEST_INFO sip:445600709315@sip.com,sip:999@sip.com... (2 Replies)
Discussion started by: rahulrathod
2 Replies

9. Shell Programming and Scripting

If statement falling over on a null record. Help please.

Okay i've got some code which reads a text file and loops through it and there a few if statements inside where one is failing (the one bolded). Basically the acc column contains a list of three digit access codes, some though have null records (i.e nothing in the field) so what I want to do is... (3 Replies)
Discussion started by: TonyR
3 Replies

10. UNIX for Dummies Questions & Answers

Find files which contain a null character

Hi, I would like to use grep to find files which contain NULL characters. I'm not sure how to represent the null character in the grep statement. Could anyone help please? Thanks! Helen :confused: (5 Replies)
Discussion started by: Bab00shka
5 Replies
Login or Register to Ask a Question