The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting same exit status for empty and non empty file mavesum UNIX for Dummies Questions & Answers 4 12-03-2008 09:59 AM
Trying to empty file using > but the file size increasing when next append firdousamir UNIX for Dummies Questions & Answers 4 06-23-2008 03:37 AM
File is not empty? lesstjm Shell Programming and Scripting 7 07-12-2007 10:21 AM
empty file in hp-ux Mizi UNIX for Advanced & Expert Users 2 08-09-2006 06:29 AM
How will you list only the empty lines in a file (using grep) JosephGerard UNIX for Dummies Questions & Answers 1 07-21-2005 03:19 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-01-2009
plawr plawr is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 1
Grep for empty file

I have a program that takes a file in which I use a sed to remove some data that is not needed. If all of the data is removed. I want to grep for that in the file and print that there is no data left in the file. When I use sed, I replace a word with nothing but it still seems to leave a return carriage. Please give me some ideas on how to do this. Thanks!
  #2 (permalink)  
Old 10-01-2009
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
Code:
if egrep . file > /dev/null
then
    # File is not empty
else
    # File is empty (except for line feeds)
fi
  #3 (permalink)  
Old 10-02-2009
Scrutinizer Scrutinizer is online now
Registered User
  
 

Join Date: Nov 2008
Posts: 621
Code:
if [ -s file ]; then
  echo not empty
else
  echo empty
fi
  #4 (permalink)  
Old 10-02-2009
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
Quote:
Originally Posted by Scrutinizer View Post
Code:
if [ -s file ]; then
  echo not empty
else
  echo empty
fi
The OP says that sed leaves a \n in the "empty file". So its size will be 1 byte and your test will show not empty.

You could also use the stat command:
Code:
if [[ $(stat -c %s file) == 1 ]];then echo "kind of empty";fi
  #5 (permalink)  
Old 10-02-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by ripat View Post
The OP says that sed leaves a \n in the "empty file". So its size will be 1 byte and your test will show not empty.

You could also use the stat command:
Code:
if [[ $(stat -c %s file) == 1 ]];then echo "kind of empty";fi

The stat command is not standard, and there are different versions:

Code:
$ stat -c %s .bashrc 
stat: illegal option -- c
usage: stat [-FlLnqrsx] [-f format] [-t timefmt] [file ...]


---------- Post updated at 06:49 PM ---------- Previous update was at 06:48 PM ----------

Quote:
Originally Posted by plawr View Post
I have a program that takes a file in which I use a sed to remove some data that is not needed. If all of the data is removed. I want to grep for that in the file and print that there is no data left in the file. When I use sed, I replace a word with nothing but it still seems to leave a return carriage. Please give me some ideas on how to do this. Thanks!

Fix the sed command to delete the line instead of just the word.
  #6 (permalink)  
Old 10-02-2009
Scrutinizer Scrutinizer is online now
Registered User
  
 

Join Date: Nov 2008
Posts: 621
OK, I see.
-deleted-

Last edited by Scrutinizer; 10-02-2009 at 02:41 AM.. Reason: Misread (again;)
Reply

Bookmarks

Tags
empty, file, grep, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0