LinuxQuestions.org
Register a domain and help support LQ
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 01-18-2006, 06:33 AM   #1
keijok
LQ Newbie
 
Registered: Jan 2006
Posts: 1

Rep: Reputation: 0
Need help with grep


Hi,
I have a need to filter text output the following way: show all lines except those that contain string 'example'. If line contains string 'exmaple2' it should be shown though it contains string 'example'

Any suggestions?
 
Old 01-18-2006, 06:39 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I'm not at a Linux machine, so I can't test this, but it will be somthing like:

cat filename|grep {example!} or maybe grep {example}!
Look at the grep man page + one of the many on-line tutorials on regular expressions.
 
Old 01-18-2006, 06:43 AM   #3
w1tebear
LQ Newbie
 
Registered: Jan 2005
Location: Berryville, VA
Distribution: Redhat/Xandros
Posts: 2

Rep: Reputation: 0
Try the -w option to grep (for 'words'):

cat my_file | grep -wv example
 
Old 01-18-2006, 06:50 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
He want everything EXCEPT example---need negation somewhere
 
Old 01-18-2006, 06:59 AM   #5
w1tebear
LQ Newbie
 
Registered: Jan 2005
Location: Berryville, VA
Distribution: Redhat/Xandros
Posts: 2

Rep: Reputation: 0
Yes, sorry, that would be what the -v is for :-)
 
Old 01-18-2006, 07:02 AM   #6
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
negation is the -v:
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
 
Old 01-18-2006, 07:43 AM   #7
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
Remember though, he said he wanted to exclude lines that said "example", but NOT exclude lines that say "example2", even though they contain the string example. You need the concept of a word boundary in there. I don't know how to do it with grep, but i do with perl - like so:

Code:
chris@manta ~ $ echo "this example" | perl -ne "print unless m/\bexample\b/"
chris@manta ~ $ echo "this example2" | perl -ne "print unless m/\bexample\b/"
this example2
chris@manta ~ $
 
Old 01-18-2006, 09:01 AM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
w1tebear's method works on my box using -vw

cat file.txt
this is example stuff
this is example1 testing
this is example2 text

Code:
grep -vw example file.txt
this is example1 testing
this is example2 text
 
Old 01-18-2006, 09:34 AM   #9
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
Nice but then we still miss the second requirement: line with 'exmaple2' and example should not be filtered out.

So if the file looks like this:
cat file.txt
this is example stuff
this is example1 testing
this is exmaple2 text
this is exmaple2 example

we want output like:
Code:
this is example1 testing
this is example2 text
this is exmaple2 example
while your suggestion gives:
Code:
grep -vw example file.txt
this is example1 testing
this is exmaple2 text
 
Old 01-18-2006, 10:28 AM   #10
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Sorry, I missed that.

I'll repost the file.txt so peoples don't get confused by your typos.

cat file.txt
this is example stuff
this is example1 testing
this is example2 text
this is example2 example

Code:
grep "example[0-9]" file.txt
this is example1 testing
this is example2 text
this is example2 example
Code:
sed -ne '/example[0-9]/p' file.txt
this is example1 testing
this is example2 text
this is example2 example

Last edited by homey; 01-18-2006 at 10:34 AM.
 
Old 01-18-2006, 10:41 AM   #11
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
Quote:
Originally Posted by homey
Sorry, I missed that.

I'll repost the file.txt so peoples don't get confused by your typos.
i think the OP actually intended those.
So i'll stick to that exmaple.

Close but no cigar with the use of sed:
Code:
 $ cat new.txt
1  this is example stuff
2  this is example1 testing
3  this is exmaple2 text
4  this is exmaple2 example
 $ sed -e '/example/!p' -e '/exmaple2/!d' new.txt
3  this is exmaple2 text
3  this is exmaple2 text
4  this is exmaple2 example
The problem though is that is doubles up line 3, which is unwanted.
And it takes 'example' literal so that includes example1.
inspiration taken from here: http://www.student.northpark.edu/pem...sed1line52.txt

Another try (1. delete only foo 2.keep foo when bar in the same line)
Code:
 $ cat log
1 foo
2 foo bar
3 bar
4 bar foo
5 test
 $ sed -e '/bar/p' -n -e '/foo/d' log
2 foo bar
3 bar
4 bar foo
Does not work as well, since it does notshow line 5.
taken from this example: part: A Slightly More Complex Example

Last edited by muha; 01-18-2006 at 05:22 PM.
 
Old 01-20-2006, 05:09 PM   #12
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
I guess this would be called a hack but have a fix for the doubling up of the same lines.
Pipe it through uniq

Our text-file. 1. we want to delete lines with 'foo' except when they contain 'bar'
I put in yes/no to clarify which ones i want to keep or not.
Code:
$ cat text.txt
1  no  foo
2  no  foo text
3  no  foo another
4  yes bar
5  yes bar text
6  yes bar foo
7  yes foo bar
To be able to save the sed scripts, i put it in a text-file called sed_script.sh
Code:
$ cat sed_text.sh
#!/usr/bin/sed -f
# save your commands in this sed_text.sh; chmod u+x sed_text.sh; and then call it like so: $ ./sed_text.sh filename|uniq

# print only lines which do NOT match regexp (emulates "grep -v")
/foo/!p
# print only lines which match regular expression (emulates "grep")
/bar/!d

# do it in a commandline: sed -e '/foo/!p' -e '/bar/!d' text.txt|uniq
Running the script (first chmod as explained in the script):
Does the same as command: sed -e '/foo/!p' -e '/bar/!d' text.txt|uniq
Code:
$ ./sed_text.sh text.txt |uniq
4  yes bar
5  yes bar text
6  yes bar foo
7  yes foo bar
If somebody can enlighten me how to do this in a better way or with grep, yes please!
It's always fun to learn from these things ..

/edit: i guess your file needs to contain non-unique lines since we're dumping them with |uniq.
So if it contains a couple of the exact same lines my solution doesn't work.
You can number them beforehand with: $ nl <textfile>
to make them unique.

Last edited by muha; 01-23-2006 at 04:51 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
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 01:22 PM
What does rpm -qa |grep th* (as compared to rpm -qa |grep th) display? davidas Linux - Newbie 2 03-18-2004 02:35 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 04:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM
What is grep? Thaidog Linux - Newbie 8 07-24-2003 03:04 AM


All times are GMT -5. The time now is 09:29 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