how to scan for the empty or blank line

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to scan for the empty or blank line
# 1  
Old 06-29-2011
how to scan for the empty or blank line

I am having a text file sample.txt

The contents of the sample.txt is as follows.


first line contains aaa,bbb
second line is blank
third line contains ccc,ddd

Now I need to write a script to search in the file sample.txt. If it contains blank as in second line. It have to throw the error message "invalid file" else nothing.

I have achieved this by reading line by line. But due to performance issue, I need to scan in the whole file whether it contains blank line. If so i need to throw the error message "invalid file".

Can anyone help me out.

Thanks in advance.

Krishnakanth Manivannan
# 2  
Old 06-29-2011
Here is one solution, using awk:
awk '!NF{exit 1}' sample.txt || echo 'Invalid File'

# 3  
Old 06-30-2011
grep -v . sample.txt && echo "Invalid File" || echo "Valid File"

---------- Post updated at 09:01 AM ---------- Previous update was at 08:59 AM ----------

And if your grep supprot -m option, then you can try the below

grep -m 1 -v . sample.txt && echo "Invalid File" || echo "Valid File"

so, whenever the first empty line (means no space also) was captured, it will not go further and scan your text file.
This User Gave Thanks to itkamaraj For This Post:
# 4  
Old 06-30-2011
Next solution is answer for question "Which files include empty lines or only spaces ?"
# line length 0 or line include only spaces
emptylines=$(  grep -l -e "^$" -e "^ *$"      *.txt   )
for f in $emptylines
        echo "Invalid File:$f"

This User Gave Thanks to kshji For This Post:
# 5  
Old 06-30-2011
"^ .*"

The above pattern will match " abc" also
This User Gave Thanks to itkamaraj 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

In a file, replace blank line by the last line not blank above

Dear All, In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read. For example, consider the extract below: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

2. Shell Programming and Scripting

How to check if the file is empty or has blank space.?

Hi, I am using KSH. I am trying to check if the output file is empty or not. I tried with ] but what i see is my file is empty but still manages to have a size of 1 instead of 0. But my file doesnot have anything its empty. I am not sure how to check this. can any one help? (10 Replies)
Discussion started by: Sharma331
10 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

5. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

6. Shell Programming and Scripting

awk - remove row if specific field is empty/blank

I have this text.filecharles darwin sam delight george washington johnson culper darwin sam delight micheal jackson penny lite and would like to remove the row, if the first field is blank. so the result would be: result.filecharles darwin sam ... (4 Replies)
Discussion started by: charles33
4 Replies

7. Shell Programming and Scripting

Delete the last empty/blank line of the text file

Hi All, I have a file.txt which seems like having three lines. wc -l file.txt 3 file.txt In fact, once it is open in text editor, this file has four lines where the last line is empty. how can i delete this last empty line of the file.txt? I tried the codes below so far but they... (6 Replies)
Discussion started by: senayasma
6 Replies

8. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

9. Shell Programming and Scripting

Replace two blank line with a single blank line

Hi Guys, I have a file in which each set of records are separated by two blank line. I want to replace it with a single blank line. Can you guys help me out? Regards, Magesh (9 Replies)
Discussion started by: mac4rfree
9 Replies

10. Shell Programming and Scripting

sed: delete regex line and next line if blank

Hi, I want to write a sed script which from batiato: batiato/giubbe: pip_b.2.txt pip_b.3.txt pip_b.3mmm.txt bennato: bennato/peterpan: 123.txt consoli: pip_a.12.txt daniele: (2 Replies)
Discussion started by: one71
2 Replies
Login or Register to Ask a Question