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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check if the file is empty or has blank space.?
# 8  
Old 07-18-2014
Quote:
Originally Posted by Sharma331
You are correct i am getting some data and deleting it so its coming as 1 while the file is empty.

Can't we do anything about it??
We can, open the file with vi and delete the line, type dd and save the file
# 9  
Old 07-18-2014
How about:

Code:
if [ $(tr -d ' \r\n\t' < filename | wc -c ) -eq 0 ]
then
        echo "File is empty"
else
        echo "File has non-blank characters"
fi

# 10  
Old 07-19-2014
Code:
read x1 < $file
[[ -z "$x1" ]]

This tests the first line.
Or
Code:
awk 'NF {exit 1}' $file
[[ $? -eq 0 ]]


Last edited by MadeInGermany; 07-19-2014 at 04:45 AM..
# 11  
Old 07-21-2014
Quote:
Originally Posted by Corona688
How about:

Code:
if [ $(tr -d ' \r\n\t' < filename | wc -c ) -eq 0 ]
then
        echo "File is empty"
else
        echo "File has non-blank characters"
fi

Thanks alot!! Smilie it is working now.. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to identify blank space in a file

Hello, I have a dictionary of over 400,000 words with the following structure source=target The database contains single words as well as phrases. To train the data, I need only mappings with out a space i.e. where both source and target do not have any space in between. I use Ultraedit as... (4 Replies)
Discussion started by: gimley
4 Replies

2. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

3. Shell Programming and Scripting

Empty file check

Hi gurus , I have two files and i want to perform different action based on the condition if both or either is empty If then Do something elif then do something elif then do something else do something fi I have tried the below bt its not... (4 Replies)
Discussion started by: r_t_1601
4 Replies

4. Shell Programming and Scripting

Removing blank space in file

TT0000013101257 | JCJMMUJMMUB018 ... (0 Replies)
Discussion started by: sususa
0 Replies

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

6. Shell Programming and Scripting

Check if a text file is empty or not (using ls -s)

Hello, I want to make a script which says if a text file is empty or not. I tried two ways of making it, but I have problems with both of them. Now I think that the better way is the ls -s solution (considering that an empty text file has a 0 weight, because "cat file.txt" fails when file is... (4 Replies)
Discussion started by: Link_
4 Replies

7. Shell Programming and Scripting

check if file is empty

How do I check if a file is empty in a sh script I want to test in my shell script if the output file is empty and if it is do one thing and if it isnt empty do another? any ideas? (8 Replies)
Discussion started by: stolz
8 Replies

8. UNIX for Dummies Questions & Answers

to check if file is empty or not and return a non zero value

Hi All, I am new to unix worldd . I need to check a file1 if its empty or not. If its empty then return a non zero value say 99 could you pls let me know the perl script for this. (2 Replies)
Discussion started by: mavesum
2 Replies

9. UNIX for Dummies Questions & Answers

How to check if a file is empty?

Hi Masters..... I have problem !!! I need to check number of records in a file and if it is zero or file is empty i need to do some task. if ; then echo "File s empty" else echo "Not empty" fi so how to check this condition. I used wc -l < filename.txt => 1 for zero records same result... (1 Reply)
Discussion started by: shreekrishnagd
1 Replies

10. Shell Programming and Scripting

how to check weather file is blank or not?

Dear All I want to do following task. Kindly suggest. In my script every hour one file is genarated. say xyz.txt. Now if this file contain some data then i want to do task A and if file is blank then i want to do nothing. Kindly help me to do this. regards jaydeep (5 Replies)
Discussion started by: jaydeep_sadaria
5 Replies
Login or Register to Ask a Question