Difference between revisions of "Removing Comments and Blank lines of a config file"

From RSWiki
Jump to navigation Jump to search
Line 6: Line 6:
 
Using 'grep' and 'sed' we can strip out comments and blank lines of any file. you can then redirect the output to a file if you so wish. In this example I will use it to remove the comments and blank lines from my apache2.conf file
 
Using 'grep' and 'sed' we can strip out comments and blank lines of any file. you can then redirect the output to a file if you so wish. In this example I will use it to remove the comments and blank lines from my apache2.conf file
  
grep -v "#" /etc/apache2/apache2.conf | sed -e '/^$/d'
+
<syntaxhighlight lang="bash">
 +
grep -v "#" /etc/apache2/apache2.conf | sed -e '/^$/d'
 +
</syntaxhighlight>
 
   
 
   
 
If you wish to leave the blank lines then simply omit the sed statement.
 
If you wish to leave the blank lines then simply omit the sed statement.
Line 12: Line 14:
 
Alternatively you can use egrep
 
Alternatively you can use egrep
  
egrep -v '(#|^\s*$)'
+
<syntaxhighlight lang="bash">
 +
egrep -v '(#|^\s*$)'
 +
</syntaxhighlight>
  
 
With both options you can of course redirect output to a new file. The default is stdout
 
With both options you can of course redirect output to a new file. The default is stdout
 
{{AdWords2}}
 
  
 
[[Category:Linux| ]] [[Category:Unix| ]]
 
[[Category:Linux| ]] [[Category:Unix| ]]

Revision as of 10:59, 27 February 2017

Template:AdWords


Remove blank lines and comments from a config file

Using 'grep' and 'sed' we can strip out comments and blank lines of any file. you can then redirect the output to a file if you so wish. In this example I will use it to remove the comments and blank lines from my apache2.conf file

grep -v "#" /etc/apache2/apache2.conf | sed -e '/^$/d'

If you wish to leave the blank lines then simply omit the sed statement.

Alternatively you can use egrep

egrep -v '(#|^\s*$)'

With both options you can of course redirect output to a new file. The default is stdout