LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   GREP (https://www.linuxquestions.org/questions/linux-general-1/grep-929655/)

ekcr 02-16-2012 04:14 AM

GREP
 
Hi Linux Gurus
Could someone tell me how I can grep all uncommented lines in a file on linux?

Thank you in advance.

fukawi1 02-16-2012 04:25 AM

What have you done so far?

You should look at the "-v" option of grep, and the "^" regex pattern.

ekcr 02-16-2012 04:29 AM

I have used 'egrep -v '^#' file.conf' but the problem is that it has so many blank lines/spaces. Any suggstion?

fukawi1 02-16-2012 04:43 AM

So, what is your problem?
What are you REALLY trying to do?

Are you saying that you want to remove blank lines as well?

Code:

grep -v ^# filename | sed '/^$/d'
Will show only uncommented lines, with no blank lines.

colucix 02-16-2012 04:44 AM

This will exclude comment lines with leading spaces, TABs and so on (if any) and empty lines with spaces (if any). It will preserve lines of code with trailing comments:
Code:

grep -E -v '^[[:space:]]*#|^[[:space:]]*$' file


All times are GMT -5. The time now is 12:02 PM.