LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-29-2011, 12:52 AM   #1
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Rep: Reputation: Disabled
What if the I dont find the pattern using grep?


i need to search a pattern "0001p0d"
but in some of the files this pattern does not exist.
how will i know that and how can i print something like "pattern not found" instead of nothing.

Please If any one can?
 
Old 09-29-2011, 12:57 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Hello,

You could always easily do this by assigning a variable the output of your regex expression, regardless if you are using grep, sed, etc. Say for example:
Code:
#!/bin/bash
var1=$(cat file1.txt | grep asdasdasd)
if $var1 = "" then
echo "not found"
fi
Of course, this would have to be modified to work as advertised. I'm not using linux as we speak, so you can go ahead and fix that. And also of course, you will need to incorporate that into your script.

Cheers,

Josh
 
Old 09-29-2011, 01:14 AM   #3
snooly
Member
 
Registered: Sep 2011
Posts: 124

Rep: Reputation: Disabled
Are you just trying to find files that don't have that pattern anywhere? What is your goal here?
 
Old 09-29-2011, 01:18 AM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by snooly View Post
Are you just trying to find files that don't have that pattern anywhere? What is your goal here?
The OP already stated what he is trying to do. Please think before posting next time.
 
Old 09-29-2011, 01:19 AM   #5
snooly
Member
 
Registered: Sep 2011
Posts: 124

Rep: Reputation: Disabled
Quote:
Originally Posted by corp769 View Post
The OP already stated what he is trying to do. Please think before posting next time.
I did think. I read the post. It is not clear to me what the OP's real goal is. People often say one thing but mean another.

If you think it is so clear, why not try to explain it to me?
 
Old 09-29-2011, 01:27 AM   #6
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Well he is searching for this certain string within files.... And if he can not find the string, he wants to know that it was not found. Giving him the information he needs, he then can do whatever he needs to do with the code. Simple as that. He could mean anything by this, but the fact is that he wants what he asked for, and I answered his question.
 
Old 09-29-2011, 01:38 AM   #7
snooly
Member
 
Registered: Sep 2011
Posts: 124

Rep: Reputation: Disabled
Quote:
Originally Posted by corp769 View Post
Well he is searching for this certain string within files.... And if he can not find the string, he wants to know that it was not found. Giving him the information he needs, he then can do whatever he needs to do with the code. Simple as that. He could mean anything by this, but the fact is that he wants what he asked for, and I answered his question.

And you are somehow offended that I asked for more information in order to possibly give OP a better answer? Weird.
 
Old 09-29-2011, 01:40 AM   #8
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by snooly View Post
And you are somehow offended that I asked for more information in order to possibly give OP a better answer? Weird.
I honestly didn't know I was offended....

But it is always good to ask for more information, yes. The fact that the OP didn't say anything else, I gave him the best answer until he says otherwise.
 
Old 09-29-2011, 01:48 AM   #9
snooly
Member
 
Registered: Sep 2011
Posts: 124

Rep: Reputation: Disabled
Quote:
Originally Posted by corp769 View Post
I honestly didn't know I was offended....
Here you go, read this: "The OP already stated what he is trying to do. Please think before posting next time."
 
Old 09-29-2011, 01:52 AM   #10
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by ameylimaye View Post
i need to search a pattern "0001p0d"
but in some of the files this pattern does not exist.
how will i know that and how can i print something like "pattern not found" instead of nothing.

Please If any one can?
From the man page:

Code:
EXIT STATUS
       Normally, the exit status is 0  if  selected  lines  are  found  and  1
       otherwise.   But  the exit status is 2 if an error occurred, unless the
       -q or --quiet or --silent option is used and a selected line is  found.
       Note,  however,  that  POSIX  only mandates, for programs such as grep,
       cmp, and diff, that the exit status in case of error be greater than 1;
       it  is  therefore  advisable, for the sake of portability, to use logic
       that tests for  this  general  condition  instead  of  strict  equality
       with 2.
In Bash, you can check the return value by using the $? variable. For example

Code:
$ grep "string" foo
$ echo $?
 
Old 09-29-2011, 01:54 AM   #11
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by snooly View Post
Here you go, read this: "The OP already stated what he is trying to do. Please think before posting next time."
That's not being offended......

Anyway, before this is classified as thread jacking, what Nylex posted is also another way to do it. Multiple ways to do it....
 
Old 09-29-2011, 01:54 AM   #12
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
thank you sir
 
Old 09-29-2011, 02:19 AM   #13
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
@corp769 Sir

I am not able to use a variable in the sh script.
as soon as i run the script
it says.
var1 not found
or
error line 1 : "(" unexpected ---> although i have put both parenthesis still..

the file from which i need to search is a log file.
and the grabbed pattern gets added to a file X. So if in some files the pattern is not found then it does not print anything in the file X...making it difficult for me to identify at which point the sequence is disturbed.


so i want to print "--" instead of nothing.

If this information can help. :-\
 
Old 09-29-2011, 02:27 AM   #14
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Can you post your script for us?
 
Old 09-29-2011, 02:42 AM   #15
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
cd /home/mscgnr/project
rm -rf logs

cat blank > X_RL
cat blank > Y_RL
cat blank > Z_RL
mkdir logs
cat site.txt > VSWR_LIST
mobatch /home/mscgnr/project/site.txt 'lt all;lh ru fui get vswr;rbs' /home/mscgnr/project/logs

cat site.txt > /home/mscgnr/project/logs/site1.txt

cd /home/mscgnr/project/logs
rm X_lines
rm Y_lines
rm Z_lines
sitecount=`grep -c . site1.txt`
j=1

while ( test $j -le $sitecount )
do
site=`sed -n "$j"p site1.txt`

grep "0001p0d" $site.log > X
grep "0009p0d" $site.log >> X ##-------->> here i need to add the print "not found"

## if this pattern is not found.

awk '{ if($0 ~ /[dB]/) print ; else print "--" }' X >> X_lines
#awk '{ if($0 ~ /0009p0d/) print ; else print "--" }' X >> X_lines


j=`expr $j + 1`

done

Last edited by ameylimaye; 09-29-2011 at 02:43 AM.
 
  


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
I want to print a line if i dont find the pattern using grep.! how is this possible? ameylimaye Linux - Newbie 4 09-28-2011 12:57 PM
grep till second pattern rattlesnakejoe Programming 1 11-22-2009 09:09 AM
grep exact pattern kapila Linux - Newbie 11 08-06-2009 03:58 AM
find | grep <pattern> not working duvalr Linux - Software 4 09-17-2006 05:07 AM
using grep when the pattern contains a ! farmerjoe Programming 9 03-16-2005 12:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:43 PM.

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