LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-30-2011, 01:44 AM   #1
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Rep: Reputation: Disabled
what happens when the file in which grep is searching for pattern is blank?


what happens when the file in which grep is searching for pattern is blank?

how do i tackle this problem?...i want to print "--" instead of skipping a line.
 
Old 09-30-2011, 01:53 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
I don't think grep can distinguish between an empty file and the pattern not being found. If you need to check for an empty file, in Bash, you can do so with -s, which returns true if the file is not empty:


Code:
myfile=/home/me/foo
if [ ! -s $myfile ] 
then 
  echo "$myfile is empty"
fi
See this for more on file tests in Bash.
 
Old 09-30-2011, 01:55 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
I don't understand your question. Could you explain in more detail?

Show us exactly what you're doing, with examples of the file(s), the commands you're using, the output you're getting, and what kind of output you want to get.
 
Old 09-30-2011, 02:53 AM   #4
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
Hi,
In my script I have to print a line in file using grep and then again grab data from the line.
But if the first grep does not find the matching pattern..it will not print anything in that file...so I want to read an empty file...and then instead of grabbing nothing from the empty file..i want to put "--" indicating the file was empty.

If this description helps!
 
Old 10-01-2011, 03:10 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
I think I understand the basic point, but it would help a lot more if we could see the actual code you're using, rather than just a vague description of what it does. The best solutions to use in cases like this often depend on the exact context of the problem.

As Nylex pointed out, at the very least you can store the grep results in a variable, and test that in order to determine what to print. You can also often use shell built-in features extract substrings if necessary.
Code:
line="$( grep "string" inputfile )"

if [[ "$line" ]]; then
	<extract and print substring>
else
	echo "--"
fi
But there may be even better options, depending on just what you need to do.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using Grep with Pattern File and PCRE bigbot Linux - Newbie 11 10-01-2010 10:33 AM
[SOLVED] how to make grep to search a pattern in only specific file type mq15 Linux - Newbie 7 03-07-2010 10:41 AM
searching pattern in tab delimited file using grep vaibhavs17 Programming 5 03-05-2010 02:02 AM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 11:51 AM


All times are GMT -5. The time now is 06:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration