2

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

  • 2
  • @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 9: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
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.

  • Updated the answer – Ruban Savvy Nov 28 '18 at 11:20
  • 1
    Hi, thanks for the solution, but mine doesn't work. I use this in unix – daffodil Nov 29 '18 at 3:14
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
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.)

  • Hi anonymous downvoter, can you explain ? – lauhub Nov 28 '18 at 9: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 3:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.