{"id":66,"date":"2006-11-30T02:17:10","date_gmt":"2006-11-30T05:17:10","guid":{"rendered":"http:\/\/www.sysop.ca\/?p=66"},"modified":"2018-10-01T06:30:25","modified_gmt":"2018-10-01T13:30:25","slug":"remove-comments-and-blank-lines-from-a-file","status":"publish","type":"post","link":"http:\/\/www.sysop.ca\/?p=66","title":{"rendered":"Remove comments and blank lines from a file"},"content":{"rendered":"<p>Want to make that httpd.conf look clean and tidy? Have a file that contains a lot of blank lines or comments that you don&#8217;t want? Here&#8217;s a quick trick to remove all the extra cruft.<\/p>\n<p>Say I have a file called config.conf. This file looks like this:<br \/>\n<code>-bash2.05b cswanson@helios ~ % cat config.conf<br \/>\n# Sample config file!<\/p>\n<p># Here is a sample comment. Note the blank lines.<\/p>\n<p>$config=\/etc\/blah<br \/>\necho $config $1 <\/p>\n<p># Here is some more sample comments.<br \/>\n<\/code><\/p>\n<p>Now we run egrep on the file to remove the commented and blank lines:<\/p>\n<p><code>-bash2.05b cswanson@helios ~ % egrep -v '^$|^#' config.conf<br \/>\n$config=\/etc\/blah<br \/>\necho $config $1<br \/>\n-bash2.05b cswanson@helios ~ %<br \/>\n<\/code><\/p>\n<p>If you want to save this output to a file just use a simple redirect:<\/p>\n<p><code>-bash2.05b cswanson@helios ~ % egrep -v '^$|^#' config.conf > config.conf-new<br \/>\n-bash2.05b cswanson@helios ~ % cat config.conf-new<br \/>\n$config=\/etc\/blah<br \/>\necho $config $1<br \/>\n-bash2.05b cswanson@helios ~ %<br \/>\n<\/code><\/p>\n<p>UPDATE: If you&#8217;re using vi you can use the following commands to achieve the same thing.<\/p>\n<p>Remove blank lines:<br \/>\n<code><esc>:g\/^$\/d<\/esc><\/code><\/p>\n<p>Remove commented lines:<br \/>\n<code><esc>:g\/^\\s*#\/d<\/esc><\/code><\/p>\n<p>Of course there are other ways to skin this cat using sed\/awk etc but the above two commands have proven to be quick and easy for me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Want to make that httpd.conf look clean and tidy? Have a file that contains a lot of blank lines or comments that you don&#8217;t want? Here&#8217;s a quick trick to remove all the extra cruft. Say I have a file called config.conf. This file looks like this: -bash2.05b cswanson@helios ~ % cat config.conf # Sample &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.sysop.ca\/?p=66\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Remove comments and blank lines from a file&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,7],"tags":[],"class_list":["post-66","post","type-post","status-publish","format-standard","hentry","category-general","category-unix"],"_links":{"self":[{"href":"http:\/\/www.sysop.ca\/index.php?rest_route=\/wp\/v2\/posts\/66","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.sysop.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sysop.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sysop.ca\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sysop.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=66"}],"version-history":[{"count":4,"href":"http:\/\/www.sysop.ca\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions"}],"predecessor-version":[{"id":446,"href":"http:\/\/www.sysop.ca\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions\/446"}],"wp:attachment":[{"href":"http:\/\/www.sysop.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sysop.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sysop.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}