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? |
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 Cheers, Josh |
Are you just trying to find files that don't have that pattern anywhere? What is your goal here?
|
Quote:
|
Quote:
If you think it is so clear, why not try to explain it to me? |
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.
|
Quote:
And you are somehow offended that I asked for more information in order to possibly give OP a better answer? Weird. |
Quote:
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. |
Quote:
|
Quote:
Code:
EXIT STATUS Code:
$ grep "string" foo |
Quote:
Anyway, before this is classified as thread jacking, what Nylex posted is also another way to do it. Multiple ways to do it.... |
thank you sir :)
|
@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. :-\ |
Can you post your script for us?
|
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. |