4

How can I grep the string which is only in uncommented line? I don't want the result to be included for string in commented line. For example: In file.tx

My name is Jane
#My name is Sara

I want to grep word "name" and print the output, like below: -

My name is Jane

But I don't want the commented line to be display to.

Thanks

daffodil
  • 89
  • 1
  • 7
  • 2
    Possible duplicate of [grep for a text in a line. But if it has a comment only at its beginning then exclude it from the grep](https://unix.stackexchange.com/questions/439691/grep-for-a-text-in-a-line-but-if-it-has-a-comment-only-at-its-beginning-then-ex) – n.st Nov 28 '18 at 09:39
  • @mosvy I was thinking along those lines too (but `grep '^[^#]*pattern'`, to prevent any `#` from creeping in anywhere before the pattern), may I suggest posting that as an answer? – Stephen Kitt Nov 28 '18 at 09:46
  • @StephenKitt Not that easy ... your command will filter this: `echo "# pattern"` which is not a comment. – pLumo Nov 28 '18 at 10:10
  • @RoVo true, the answer depends very much on the context. If the OP is trying to parse shell code, then the only reliable way to go about this is to write a shell parser ;-). I was thinking of configuration files myself. – Stephen Kitt Nov 28 '18 at 10:12
  • you're right, for most use cases this will be enough, but it's not a general solution. As the question stands, it is not clear, but OP says "uncommented **line**" which indicates that your solution would fit. – pLumo Nov 28 '18 at 10:18
  • @StephenKitt My first (single-regexp,`'^[^#].*pattern`) solution is broken -- it will miss lines where `pattern` should match at the beginning of the line. –  Nov 28 '18 at 15:41

3 Answers3

1

Without more details it is not possible to give a perfect answer. You can run something along the lines of:

remove the comments (and maybe literal strings)   | 
grep in the output

Removing comments (multiline or not) can be tricky... We need at least a language specific lexical analyzer to correctly deal with it.

Anyway we can write a quick hack (that is not so robust) Following an example with C comments:

   perl -0pe 's!/\*.*?\*/!!gs' x.c   | grep searchstring
JJoao
  • 11,887
  • 1
  • 22
  • 44
0

Something like this could work (assuming commented lines start with a # sign):

grep -v '^#' myfile.txt | grep string_to_search

You can replace # with any symbol that indicates a comment line (C, c, d, D, * or ! e.g.)

lauhub
  • 914
  • 2
  • 11
  • 24
  • Hi anonymous downvoter, can you explain ? – lauhub Nov 28 '18 at 09:51
  • I did not downvote, but this will print just any line which does not start with `#`, but OP wants to search a pattern when line is not commented. – pLumo Nov 28 '18 at 10:19
  • @Kusalananda You can replace `#` with any other character, hence my example and its explanation. And if the comments are multi-lines, `grep` will not be sufficient and you'll need a dedicated parser. – lauhub Nov 28 '18 at 11:06
  • That should all be part of your answer. – Kusalananda Nov 28 '18 at 11:09
  • Hi, thanks for the solution, but mine doesn't work. I use this in unix – daffodil Nov 29 '18 at 03:05
  • I also use Unix. Which flavor and which shell do you use ? (BSD, Solaris / bash, ksh, etc) – lauhub Nov 29 '18 at 06:47
0

If you want to print the line which is not commented out in a file, then below commands comes handy.

grep -v '^ *#' filename.txt  | grep searchstring

-v -> Inverse
-v "^ *#" -> Print the lines without "#" in the starting line (# ABC - This line is also avoided)

It has worked for me for a very long time & I use this to display the active crontab jobs.

Ruban Savvy
  • 8,409
  • 8
  • 29
  • 43