Operating System - HP-UX
1832169 Members
3046 Online
110038 Solutions
New Discussion

script/cmd required for avoiding the lines containing a search word

 
SOLVED
Go to solution
Michael Schulte zur Sur
Honored Contributor

Re: script/cmd required for avoiding the lines containing a search word

Hi Karthik,


grep -E -v ('^mathi ' or ' mathi$' or '^mathi$' or ' mathi ')

means: exclude all lines, which have a mathi at the start and a blank following, a mathi at the end, preceeded by a blank, mathi alone on a line and mathi preceeded and followed by a blank.

greetings,

Michael
Jean-Louis Phelix
Honored Contributor

Re: script/cmd required for avoiding the lines containing a search word

Hi,

If grep -v ' mathi ' displays the whole file ... then perhaps you don't really have spaces between words, or at least not only spaces.

You could check using 'cat -t file' to make tabs visible. if you have a mix of tabs and spaces, rather use :

grep -v -E '^mathi[[:blank:]]|[[:blank:]]mathi[[:blank:]]|[[:blank:]]mathi$' /tmp/a | awk '

or replace [[:blank:]] which means 'space or tabs' class (I've only found description of classes in 'man tr') by [] which means a space or a tab (of course you really put a space and a tabs but I can't write them, they would disapoear in the post :^) ...

Regards.
It works for me (© Bill McNAMARA ...)
Karthikeyan_5
Frequent Advisor

Re: script/cmd required for avoiding the lines containing a search word

Hi Jean-Louis,

You please consider my I/p file.....in that the word "mathi" is surrounded by a single blank space only......and not tab......that is why I am very very much confused why "grep -v -E ' mathi ' i/p" is not giving me the correct o/p.....

Also can u pls give some GOOD links for learning Sh Scripting, awk & sed...or if u have any DOC can u pls attach it.....so that I can start learning......

regards,
karthik
Michael Schulte zur Sur
Honored Contributor

Re: script/cmd required for avoiding the lines containing a search word

Hi Karthik,

grep -v -E ' mathi ' i/p is not giving the right output because it only eliminates line with mathi and a blank on both sides, which means ' mathi' at the end of the line is not excluded. With the option -E you can specify more that one pattern. They must be separated by |. These patterns are evaluated with logical or, meaning that if any one of them is in the line, the line qualifies for the grep and when you specify -c, then this means instead of inluding those lines, exclude them.

greetings,

Michael
Karthikeyan_5
Frequent Advisor

Re: script/cmd required for avoiding the lines containing a search word

Hi Michael Schulte,

Thank you you very very much.....I got it....

Can you pls give me some GOOD DOC links for learning Sh Scripting, awk & sed...etc

regards,
karthik
H.Merijn Brand (procura
Honored Contributor

Re: script/cmd required for avoiding the lines containing a search word

Not a book for scripting, but for regular expressions in general (covering perl, sed, awk, vi, jave, .net and more)

Mastering Regular Expressions
2nd edition
Jeffrey E.F.Friedl
O'Reilly ISBN 0-596-00289-0

http://www.oreilly.com/catalog/regex2/index.html

Enjoy, Have Fun! H.Merijn
Enjoy, Have FUN! H.Merijn