Introduction to sed
sed
, short for stream editor, is a powerful command-line utility used for parsing and transforming text. It processes input line-by-line and performs specified operations on those lines using regular expressions. This tutorial will guide you through replacing entire lines in a file based on specific conditions or line numbers using sed
.
Basic Concepts of sed
- Pattern Matching:
sed
uses regular expressions to match patterns within text. - Substitutions: The most common operation is substitution, which replaces matched patterns with specified text.
Syntax
The general syntax for a substitution command in sed
is:
s/pattern/replacement/[flags]
pattern
: A regular expression that identifies the text to be replaced.replacement
: The text that will replace the matched pattern.[flags]
: Options likeg
(global replacement) ori
(case-insensitive).
Replacing an Entire Line with a Specific Pattern
To replace a line containing a specific string, you can use the change (c
) command in sed
. Here’s how to do it:
Example Scenario
Suppose you have a file named example.txt
and want to replace any line containing "TEXT_TO_BE_REPLACED" with "This line is removed by the admin."
Using GNU sed (with -i
for in-place editing):
sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' example.txt
/TEXT_TO_BE_REPLACED/
: Matches lines containing "TEXT_TO_BE_REPLACED".c\...
: Changes the matched line to what follows after\
.
Notes:
- On some systems, like Red Hat 5, you might not need the backslash (
\
) afterc
if the replacement is a single line. - Use double quotes around your replacement text when using variables.
Handling Escaped Characters
If your replacement text includes slashes or other special characters, ensure they are properly escaped:
function escape_slashes {
sed 's/\//\\\//g'
}
function change_line {
local OLD_LINE_PATTERN=$1; shift
local NEW_LINE=$1; shift
local FILE=$1
local NEW=$(echo "${NEW_LINE}" | escape_slashes)
sed -i.bak '/'"${OLD_LINE_PATTERN}"'/s/.*/'"${NEW}"'/' "${FILE}"
mv "${FILE}.bak" /tmp/
}
change_line "TEXT_TO_BE_REPLACED" "This line is removed by the admin." example.txt
Replacing Lines Based on Line Numbers
If you know the exact line number of text to replace, sed
can be used without needing pattern matching:
Replace a Specific Line Number
To replace line 17 with specific text:
sed -i '17s/.*/REPLACEMENT-TEXT/' example.txt
This command will:
- Target line 17 (
'17s/...
). - Match the entire line (
.*
). - Replace it with "REPLACEMENT-TEXT".
Summary
In this tutorial, we explored how to use sed
for replacing lines based on patterns and specific line numbers. Whether you need to change text using patterns or replace exact lines by number, sed
offers flexible options that can be tailored to your requirements.
Remember:
- Use the
c
command withsed
for replacing entire lines containing a pattern. - Escape special characters in replacement strings when necessary.
- Utilize line numbers for direct replacements without pattern matching.
With these techniques, you’ll effectively manipulate text files on the command line using sed
.