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 greps, 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?print '#';,print ''; # here we print,str='#'; printorstr='# print'? There is probably no 100% safe expression without partially rebuilding the language's parser.grep '^[^#]*\bprint\b' input.