views:

50

answers:

3

I want to find a string such as "qwertty=" in a file with "awk" or "grep" but I don't want to see the lines with #. Please see the example

grep -ni "qwertty"   /aaa/bbb
798:#  * qwertty - enable/disable 
1222:#qwertty=1
1223:qwertty=2  
1224:#qwertty=3

I want to find the line 1223. What should be the search query for this purpose?

+2  A: 
grep -ni '^qwertty=' /aaa/bbb

If you want more flexibility (no hash anywhere in the line, for example, and perhaps blanks before the keyword, and maybe the keyword embedded inside a bigger word, and perhaps blanks around the '=' sign), then you need a more complex expression:

grep -ni '^[^#]*qwertty[^#]*=[^#]*$' /aaa/bbb
Jonathan Leffler
thank you. How can I use awk for this purpose?
ephieste
@ephieste: `awk '/^[^#]*qwertty[^#]*=[^#]*$/' /aaa/bbb`?
Jonathan Leffler
When I create a new file and write the above lines, the command works. It is strange but it doesn't work for some files and strings on my server. I think I am missing a point. Such as it couldn't find the following line:pm3gppXmlEnabled=true
ephieste
@ephieste: the example you give does not contain 'qwertty', so it would not find it...but how did you adapt the regexes to the new requirement? Note that the `awk` pattern is case-sensitive; you have to replace `q` with `[qQ]` to make it case-insensitive. Or check for a GNU extension that makes regexes case-insensitive (I haven't looked to see if it exists, but it might).
Jonathan Leffler
A: 

awk '/^#/ {next} /qwertty/' afile

glenn jackman
ghostdog74
A: 
grep -ni "qwertty"   /aaa/bbb|grep -v '^#'
Vijay Sarathi