Using SED in Linux to Edit Large Files

Recently I encountered a server that had an Apache configuration file that was over 500,000 lines. Changes needed to make were to comment out the CustomLog line for each domain in /etc/httpd/httpd.conf. For this command, I used SED.

NAME
sed – stream editor for filtering and transforming text

SYNOPSIS
sed [OPTION]… {script-only-if-no-other-script} [input-file]…

DESCRIPTION

Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

Change directory to the log file location. Use below to see what the command will do before doing it.

# sed -n 's|CustomLog|#CustomLog|gp' /etc/apache2/conf/httpd.conf

Use this to alter the file directly.

# sed -i 's|CustomLog|#CustomLog|g' /etc/apache2/conf/httpd.conf

Leave a Comment

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.