LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I want to print a line if i dont find the pattern using grep.! how is this possible? (https://www.linuxquestions.org/questions/linux-newbie-8/i-want-to-print-a-line-if-i-dont-find-the-pattern-using-grep-how-is-this-possible-905437/)

ameylimaye 09-28-2011 10:34 AM

I want to print a line if i dont find the pattern using grep.! how is this possible?
 
i want to print " -- " if the grep command is not able to find a pattern!


how can i do it?

so instead of disturbing my list of names...i can put "--" in that row.

Thank you in advance! :)

anomie 09-28-2011 10:44 AM

Hmm, grep(1). I think I'd use awk(1) or perl(1) instead for this one.

Code:

$ cat names.txt
bill
barry
samantha
olga
sasha
nadya

Code:

$ awk '{ if($0 ~ /^sa/) print ; else print "--" }' names.txt
--
--
samantha
--
sasha
--


ameylimaye 09-28-2011 11:00 AM

suppose i have a file in which there is a pattern

0001p0d5: vswr : 26.5[dB]


but sumtyms der is a pattern

0001p0d5: vswr : value not generated


so in the second case i need to print "--" instead of the value..

then?

anomie 09-28-2011 11:10 AM

That's a simple extension of my first example.

Code:

$ cat names.txt
0001p0d5: vswr : 26.5[dB]
0001p0d5: vswr : 22.1[dB]
0001p0d5: vswr : value not generated
0001p0d5: vswr : 26.9[dB]

Code:

$ awk '{ if($0 ~ /value not generated/) print "--" ; else print }' names.txt
0001p0d5: vswr : 26.5[dB]
0001p0d5: vswr : 22.1[dB]
--
0001p0d5: vswr : 26.9[dB]

P.S. For the love of stout, frothy beer, please do not say things like "sumtyms der" here.

ameylimaye 09-28-2011 11:57 AM

ohh..! this will help me for sure.! :)
thanks a lot.!


All times are GMT -5. The time now is 11:45 PM.