I have a java file and I want to list the number of times the word port occurs. But the only problem is I have many functions which have the work port in it too.Here is an example of a function : public void portNum(){  System.out.println(Your port number is ports port);  port();  //Reading your Port number  readPortNumber();}I want to get only those lines which have port or port within  .The output would be : System.out.println(Your port number is ports port);//Reading your Port numberSo far I have grep -i 'port'
grep to ignore special characters in a file
grep
If you want the lines that do not contain function declaration (as you have said in the comment):$ grep -iwP 'port(?!\(.*\))' file.txt  System.out.println(Your port number is ports port);  //Reading your port number