In debugging, I use a lot of 'print' and commenting out it with '#print'. How can I use grep to find the line without '#' before 'print'?
# print <- not detect
#print <- not detect
abc # print <-- not detect
print <- detect
Classic solution:
grep -v '^#' <input |grep 'print'
The easiest approach is probably going to be to use two grep
s, piped together.
$ grep 'print' <input | grep -v '#[[:space:]]*print'
With the file input
containing your examples, that gives:
print <- detect
That works for all of your examples. Which is probably good enough, but as manatwork and I point out in comments, its going to be very difficult to defeat all the edge cases with grep
.
I'm still learning but wouldn't the ff work as well?
grep -v '#[ ]*print' input_file
foo # bar print
? – derobert Mar 13 '13 at 15:37print '#';
,print ''; # here we print
,str='#'; print
orstr='# print'
? There is probably no 100% safe expression without partially rebuilding the language's parser. – manatwork Mar 13 '13 at 15:38grep '^[^#]*\bprint\b' input
. – manatwork Mar 13 '13 at 15:52