LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What if the I dont find the pattern using grep? (https://www.linuxquestions.org/questions/linux-newbie-8/what-if-the-i-dont-find-the-pattern-using-grep-905535/)

ameylimaye 09-28-2011 11:52 PM

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?

corp769 09-28-2011 11:57 PM

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

snooly 09-29-2011 12:14 AM

Are you just trying to find files that don't have that pattern anywhere? What is your goal here?

corp769 09-29-2011 12:18 AM

Quote:

Originally Posted by snooly (Post 4485098)
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.

snooly 09-29-2011 12:19 AM

Quote:

Originally Posted by corp769 (Post 4485102)
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?

corp769 09-29-2011 12:27 AM

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.

snooly 09-29-2011 12:38 AM

Quote:

Originally Posted by corp769 (Post 4485109)
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.

corp769 09-29-2011 12:40 AM

Quote:

Originally Posted by snooly (Post 4485118)
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.... :p

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.

snooly 09-29-2011 12:48 AM

Quote:

Originally Posted by corp769 (Post 4485121)
I honestly didn't know I was offended.... :p

Here you go, read this: "The OP already stated what he is trying to do. Please think before posting next time."

Nylex 09-29-2011 12:52 AM

Quote:

Originally Posted by ameylimaye (Post 4485071)
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 $?


corp769 09-29-2011 12:54 AM

Quote:

Originally Posted by snooly (Post 4485126)
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....

ameylimaye 09-29-2011 12:54 AM

thank you sir :)

ameylimaye 09-29-2011 01:19 AM

@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. :-\

corp769 09-29-2011 01:27 AM

Can you post your script for us?

ameylimaye 09-29-2011 01:42 AM

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


All times are GMT -5. The time now is 04:56 PM.