How to find the null member or blank in the text file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to find the null member or blank in the text file?
# 1  
Old 03-06-2014
How to find the null member or blank in the text file?

Hello All,

I am new to unix scripting and wanted to know, is it possible if we find any null value or blank record in the text file.
For example we have a text file with only one column and there are 90 records.
But some times we will have a null value or blank row record in the text file.
I want to write a unix script if the text file is having a null value or blank record then it should send a email saying "Text file contains null value".
Also let me know if we have to use variable, then how to define the variable in the unix scripting.

Thanks in advance
Ram

Example of text file having records is given below:
Code:
4166711
4166712
5351223
5351234
3394415
3394436
3394457
3394478
3394489


Last edited by Scrutinizer; 03-09-2014 at 05:25 PM.. Reason: code tags
# 2  
Old 03-06-2014
GNU grep:
Code:
if grep -q "^ *$" file
then
         # Send mail here
fi

Other grep:
Code:
if grep "^ *$" file 1> /dev/null
then
         # Send mail here
fi

# 3  
Old 03-06-2014
Use -m1 so that grep doesn't parse the entire file.
Code:
...
grep -m1 "^ *$" file
...

If your grep supports -q, then you can avoid using /dev/null
Code:
...
grep -q -m1 "^ *$" file
...

HTH
--ahamed
# 4  
Old 03-08-2014
As already said, not all grep have -q.
And only GNU/Linux grep has -m1 (stop after first match).
Some grep can search for "blank" i.e. SPACE or TAB in ASCII,
the following lists the line numbers:
Code:
if output=`grep -n "^[[:blank:]]*$" file`
then
   echo "Empty lines found:
$output" | mail ...
fi

# 5  
Old 03-09-2014
Quote:
Originally Posted by ahamed101
If your grep supports -q, then you can avoid using /dev/null
Code:
...
grep -q -m1 "^ *$" file
...

-q exits immediately if a match is found, so -m1 serves no purpose.

Regards,
Alister
This User Gave Thanks to alister 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

How to sort a text file if certain columns are blank?

Dear all, I am trying to sort a text file based on column 3, 10, 11 and 12. But certain column are blank for some lines. Column 3 has to be in ascending order after sorting. Part of my input file is as follows: CN727990 1 A01 4703 5083 73.28 - A_scaffold000011 4365605 4365985 73.28 +... (10 Replies)
Discussion started by: huiyee1
10 Replies

2. Shell Programming and Scripting

How to search for blank fields in a text file from a certain position?

Sample txt file : OK00001111112| OK00003443434|skjdaskldj OK32812983918|asidisoado OK00000000001| ZM02910291029|sldkjaslkjdasldjk what would be the shell script to figure out the blank space (if any) after the pipe sign? (4 Replies)
Discussion started by: chatwithsaurav
4 Replies

3. Shell Programming and Scripting

Verify the null filed of the text file

Here is my sample data Test.txt column 1|columne 2|columne 3|columne 4 test|test||test test|test|test| test||test|test test|test|test|test |test|test|test In that example having NULL value of the row 2-column 3,row 3-column 4,row 4 - column 2,row 6- column 1 How i can validate... (5 Replies)
Discussion started by: krish2014
5 Replies

4. Shell Programming and Scripting

How to remove blank line from a text file?

Hi All, I am creating a text file using perl. The first record I am writing as "$line" and all the other as "\n$line". At the end the file is having N number of lines. I am using this file for MLOAD (Teradata), which is reading N+1 lines in the file and failing.I am not able to find new line... (2 Replies)
Discussion started by: unankix
2 Replies

5. Shell Programming and Scripting

Awk script to replace null columns with blank

I have a file with contents "08011"||20080812 "23348"|20080827|20080924 "23387"|20080829|20080915 "23581"|20081003|20081028 "23748"|20081017|20090114 "24095"|20080919|20081013 "24105"|20070723|20070801 "24118"|20080806|20081013 "24165"|20080820|20080912 "24221"|20080908|20080929 i... (3 Replies)
Discussion started by: sonam273
3 Replies

6. Shell Programming and Scripting

Replace blank spaces with semicolon - text file

Hi all, Been trying to find a solution to this, I'm sure its a sed 1 liner, but I don't know sed well enough to work it out... I have a text file in the following format: 431 666 1332 2665 0.24395 432 670 ... (3 Replies)
Discussion started by: mpcengineering
3 Replies

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

8. Shell Programming and Scripting

Remove blank spaces in a text file...

Hi, I have this problem that there are blank spaces in my text file... i want to remove them line 1 line 2 line 3 I want to remove the space between line 2 and line 3... I tried sed... it work but it prints the whole text file at the command prompt which i dont want.... sde i tried was... (4 Replies)
Discussion started by: bhagya2340
4 Replies

9. Shell Programming and Scripting

KSH script -text file processing NULL issues

I'm trying to strip any garbage that may be at the end of my text file and that part is working. The problem only seems to be with the really long lines in the file. When the head command is executed I am directing the output to a new file. The new file always get a null in the 4096 position but... (2 Replies)
Discussion started by: geauxsaints
2 Replies

10. 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
Login or Register to Ask a Question