sed - dwilson2547/wiki_demo GitHub Wiki
Here’s a comprehensive sed
cheat sheet with common commands and practical examples:
sed
(stream editor) is a powerful Linux command for parsing and transforming text.
sed
Cheat Sheet- 1. Basic Syntax
-
2. Common Commands
- 2.1. Substitution (
s
) - 2.2. Global Substitution (
g
) - 2.3. Case-Insensitive Substitution (
i
) - 2.4. Delete Lines (
d
) - 2.5. Print Lines (
p
) - 2.6. Line Numbers
- 2.7. Insert (
i
) and Append (a
) - 2.8. Change Line (
c
) - 2.9. In-Place Editing (
-i
) - 2.10. Multiple Commands (
-e
) - 2.11. Address Ranges
- 2.12. Regular Expressions
- 2.13. Read from a File (
r
) - 2.14. Write to a File (
w
) - 2.15. Quit (
q
)
- 2.1. Substitution (
- 3. Advanced Examples
- 4. Common Use Cases
- 5. Tips and Tricks
sed [options] 'command' [input-file]
-
Options:
-
-n
: Suppress automatic printing of pattern space. -
-i
: Edit files in-place (use with caution!). -
-e
: Specify multiple commands. -
-f
: Read commands from a file.
-
Replace text patterns.
sed 's/old-text/new-text/' file.txt
- Replace the first occurrence of
old-text
withnew-text
in each line.
Example:
sed 's/foo/bar/' file.txt
- Replaces
foo
withbar
infile.txt
.
Replace all occurrences of a pattern in a line.
sed 's/old-text/new-text/g' file.txt
Example:
sed 's/apple/orange/g' file.txt
- Replaces all instances of
apple
withorange
.
sed 's/old-text/new-text/gi' file.txt
Example:
sed 's/cat/dog/gi' file.txt
- Replaces
cat
,Cat
,CAT
, etc., withdog
.
Delete lines matching a pattern.
sed '/pattern/d' file.txt
Example:
sed '/error/d' logfile.txt
- Deletes all lines containing
error
.
Print lines matching a pattern.
sed -n '/pattern/p' file.txt
Example:
sed -n '/success/p' logfile.txt
- Prints only lines containing
success
.
Print or delete lines by number.
sed -n '5p' file.txt # Print line 5
sed '3d' file.txt # Delete line 3
sed -n '1,10p' file.txt # Print lines 1 to 10
Example:
sed -n '10,20p' data.txt
- Prints lines 10 to 20.
Insert or append text before/after a line.
sed '3i\new line' file.txt # Insert before line 3
sed '5a\new line' file.txt # Append after line 5
Example:
sed '1i\# Header' file.txt
- Inserts
# Header
at the beginning of the file.
Replace an entire line.
sed '3c\new line' file.txt
Example:
sed '2c\This is the new second line' file.txt
- Replaces the second line with
This is the new second line
.
Edit files directly (use with caution!).
sed -i 's/old/new/' file.txt
Example:
sed -i 's/windows/linux/g' config.txt
- Replaces
windows
withlinux
inconfig.txt
and saves changes.
Execute multiple commands.
sed -e 's/foo/bar/' -e 's/baz/qux/' file.txt
Example:
sed -e 's/red/blue/' -e 's/small/large/' colors.txt
- Replaces
red
withblue
andsmall
withlarge
.
Apply commands to a range of lines.
sed '3,6s/old/new/' file.txt
Example:
sed '10,20s/yes/no/' survey.txt
- Replaces
yes
withno
in lines 10 to 20.
Use regex for complex pattern matching.
sed -n '/^[A-Z]/p' file.txt # Print lines starting with a capital letter
sed 's/[0-9]\{3\}/NUM/g' file.txt # Replace 3-digit numbers with "NUM"
Example:
sed -n '/^[A-Za-z]/p' names.txt
- Prints lines starting with a letter.
Insert the contents of a file.
sed '3r newfile.txt' file.txt
Example:
sed '1r header.txt' data.txt
- Inserts the contents of
header.txt
after line 1 ofdata.txt
.
Write matching lines to a file.
sed -n '/error/w errors.txt' logfile.txt
Example:
sed -n '/404/w 404_errors.txt' access.log
- Writes all lines containing
404
to404_errors.txt
.
Exit after processing a specific line.
sed '10q' file.txt
Example:
sed '50q' largefile.txt
- Prints the first 50 lines and exits.
sed '/^$/d' file.txt
sed 's/#.*//' config.txt
sed G file.txt
sed = file.txt | sed 'N;s/\n/\t/'
sed -n 's/.*\([a-zA-Z0-9._-]@[a-zA-Z0-9._-]\{2,\}.[a-zA-Z]\{2,\}\).*/\1/p' file.txt
Task | Command |
---|---|
Replace text | sed 's/old/new/g' file.txt |
Delete lines | sed '/pattern/d' file.txt |
Print specific lines | sed -n '5,10p' file.txt |
Insert/append text | sed '3i\new line' file.txt |
In-place editing | sed -i 's/old/new/g' file.txt |
Use regex | sed -n '/^[A-Z]/p' file.txt |
Extract content | sed -n 's/.*\(pattern\).*/\1/p' file.txt |
-
Backup Files: Always back up files before using
-i
:sed -i.bak 's/old/new/g' file.txt
-
Test First: Use
sed
without-i
to preview changes. -
Combine Commands: Use
-e
for multiple operations. -
Debugging: Use
echo
and pipes to test patterns:echo "test" | sed 's/test/hello/'