• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

grep command to remove commented lines

June 25, 2014 by Sharad Chhetri Leave a Comment

In this post we will learn how to use grep command to remove commented lines. We can also use egrep command which is extended form of grep command.

As a Linux System Engineer, I mostly use some of the command. Most of the time when I am busy in playing on system, I generally meet with this requirement. That is reading long configuration file.

For an example, we want to read httpd.conf or php.ini file without commented lines. These are very long files and we only want to see what are the lines are not commented.

Commenting the lines different in various configuration files. In httpd.conf, we comment the file by putting hash(#) in front of line. Whereas in php.ini file, commenting the lines is done by using semicolon(;) in front of line.

So here is a simple logic. Remove the lines from display which starts with commented sign.Means in case of httpd.conf file, remove the lines start with hash(#) OR remove the commented lines in php.ini ,lines starting with semicolon(;).

Use the given below syntax:

grep -Ev '^comment-sign' /path/of/file

OR

egrep -v  '^comment-sign' /path/of/file

Example 1: In case of httpd.conf file where we use hash(#) for commenting ,use given below command

grep -Ev '^#' /etc/httpd/conf/httpd.conf

or

egrep -v '^#' /etc/httpd/conf/httpd.conf

You can also use remove blank lines along with removing commented lines. (Read our previous post on “grep command to remove blank lines from file“)

grep -Ev '^#|^$' /etc/httpd/conf/httpd.conf

or

egrep -v '^#|^$' /etc/httpd/conf/httpd.conf

Example 2: In case of php.ini file where we use semicolon(;) for commenting ,use given below command

grep -Ev '^;' /etc/php.ini

or

egrep -v '^;' /etc/php.ini

You can also use remove blank lines along with removing commented lines. (Read our previous post on “grep command to remove blank lines from file“)

grep -Ev '^;|^$' /etc/php.ini

or

egrep -v '^;|^$' /etc/php.ini

NOTE: If you see some file having some space in front of commented sign(special characters). Copy the space along with commented sign and use it in syntax.

example:

grep -Ev '^   #' /path/of/file

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. grep command to remove blank lines from file
  2. grep command to show lines after and before the keyword
  3. sed : remove lines between two keywords [ including keywords ]
  4. grep command to find multiple strings or keyword from file
  5. tr command to convert lines to space , tab and vertical tab
  6. Unix / Linux : How to print duplicate lines from file
  7. How to use grep command to get fixed pattern or exact keyword
  8. Do not show line haveing particular keyword by grep command
  9. search keyword in file with line number using grep command
  10. Print grep command output without seperator

Filed Under: Linux Commands Tagged With: grep, linux command

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

Agent admitted failure to sign using the key ssh

Terminal Recording with script and scriptreplay command

Change the default port number of ssh server

shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

Replace keyword with its filename without extension : bash script

Install libjpegtran by using yum command in CentOS 6.x

4 Different commands to find system uptime in linux

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy