LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help with grep (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-grep-404852/)

keijok 01-18-2006 05:33 AM

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?

pixellany 01-18-2006 05:39 AM

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.

w1tebear 01-18-2006 05:43 AM

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

cat my_file | grep -wv example

pixellany 01-18-2006 05:50 AM

He want everything EXCEPT example---need negation somewhere

w1tebear 01-18-2006 05:59 AM

Yes, sorry, that would be what the -v is for :-)

muha 01-18-2006 06:02 AM

negation is the -v:
-v, --invert-match
Invert the sense of matching, to select non-matching lines.

puffinman 01-18-2006 06:43 AM

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 ~ $


homey 01-18-2006 08:01 AM

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


muha 01-18-2006 08:34 AM

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


homey 01-18-2006 09:28 AM

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


muha 01-18-2006 09:41 AM

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. :p
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

muha 01-20-2006 04:09 PM

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

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 :p 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.


All times are GMT -5. The time now is 06:35 AM.