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? |
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. |
Try the -w option to grep (for 'words'):
cat my_file | grep -wv example |
He want everything EXCEPT example---need negation somewhere
|
Yes, sorry, that would be what the -v is for :-)
|
negation is the -v:
-v, --invert-match Invert the sense of matching, to select non-matching lines. |
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/" |
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 |
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 Code:
grep -vw example file.txt |
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 Code:
sed -ne '/example[0-9]/p' file.txt |
Quote:
So i'll stick to that exmaple. Close but no cigar with the use of sed: Code:
$ cat new.txt 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 taken from this example: part: A Slightly More Complex Example |
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 Code:
$ cat sed_text.sh Does the same as command: sed -e '/foo/!p' -e '/bar/!d' text.txt|uniq Code:
$ ./sed_text.sh text.txt |uniq 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 03:51 AM. |