LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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-28-2011, 10:34 AM   #1
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Rep: Reputation: Disabled
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!
 
Old 09-28-2011, 10:44 AM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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
--
 
Old 09-28-2011, 11:00 AM   #3
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
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?
 
Old 09-28-2011, 11:10 AM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.
 
Old 09-28-2011, 11:57 AM   #5
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
ohh..! this will help me for sure.!
thanks a lot.!
 
  


Reply



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
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 05:30 PM
Want to print fouth line after resulted line using grep with tail command saurabhmehan Linux - Newbie 10 08-04-2010 11:38 PM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
find | grep <pattern> not working duvalr Linux - Software 4 09-17-2006 04:07 AM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 10:51 AM

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

All times are GMT -5. The time now is 09:05 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
Open Source Consulting | Domain Registration