WebsiteTemplate/cheatsheets/sed.html
2026-01-25 11:33:37 -04:00

225 lines
7.6 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
<meta http-equiv="X-Content-Type-Options" content="nosniff">
<link rel="stylesheet" href="../assets/css/style.css">
<link rel="icon" type="image/x-icon" href="../favicon.ico">
<script>
// Apply theme immediately to prevent flash
(function() {
const theme = localStorage.getItem('theme') ||
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
document.documentElement.setAttribute('data-theme', theme);
})();
</script>
<title>sed Cheatsheet - Cheatsheets - Launch Pad</title>
</head>
<body>
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
</button>
<br/><br/>
<div class="name">
__ _______________________ _________._________________________
\_ _____/ \______ \ / _ \ / _____/ / _____/ | | \_ _____/
| __) | _/ / /_\ \ / \ ___ / \ ___ | | | __)_
| \ | | \ / | \ \ \_\ \ \ \_\ \ | |___ | \
\___ / |____|_ / \____|__ / \______ / \______ / |_______ \ /_______ /
\/ \/ \/ \/ \/ \/ \/
</div>
<div class="blog-page-header">
<div class="blog-header-content">
<a href="/cheatsheets" class="back-link" title="Back to Cheatsheets">
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
</a>
<h1 class="blog-page-title">sed Cheatsheet</h1>
</div>
</div>
<div class="blog-post-container">
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
<div class="blog-post">
<div class="blog-post-content">
<p><a href="index.html">← Back to cheatsheets</a></p>
<p><a href="../index.html">← Home</a></p>
<hr>
<p>sed (stream editor) is a powerful text processing tool for filtering and transforming text. Edit files non-interactively, perform find-and-replace operations, and manipulate text streams efficiently.</p>
<hr>
<h2>Basic Usage</h2>
<ul>
<li>sed 's/old/new/' file - Substitute first occurrence per line</li>
</ul>
<ul>
<li>sed 's/old/new/g' file - Substitute all occurrences</li>
</ul>
<ul>
<li>sed 's/old/new/2' file - Substitute 2nd occurrence</li>
</ul>
<ul>
<li>sed -i 's/old/new/g' file - Edit file in-place</li>
</ul>
<ul>
<li>sed -i.bak 's/old/new/g' file - In-place with backup</li>
</ul>
<hr>
<h2>Substitution</h2>
<ul>
<li>sed 's/pattern/replacement/' - Basic substitution</li>
</ul>
<ul>
<li>sed 's/pattern/replacement/g' - Global substitution</li>
</ul>
<ul>
<li>sed 's/pattern/replacement/i' - Case-insensitive</li>
</ul>
<ul>
<li>sed 's/pattern/replacement/gi' - Global, case-insensitive</li>
</ul>
<ul>
<li>sed 's|old|new|g' - Use different delimiter</li>
</ul>
<ul>
<li>sed 's/old/new/; s/old2/new2/' - Multiple substitutions</li>
</ul>
<hr>
<h2>Line Selection</h2>
<ul>
<li>sed -n '5p' file - Print line 5</li>
</ul>
<ul>
<li>sed -n '5,10p' file - Print lines 5-10</li>
</ul>
<ul>
<li>sed -n '5,$p' file - Print from line 5 to end</li>
</ul>
<ul>
<li>sed -n '/pattern/p' file - Print matching lines</li>
</ul>
<ul>
<li>sed -n '/start/,/end/p' file - Print range between patterns</li>
</ul>
<ul>
<li>sed '5d' file - Delete line 5</li>
</ul>
<ul>
<li>sed '/pattern/d' file - Delete matching lines</li>
</ul>
<hr>
<h2>Insertion & Appending</h2>
<ul>
<li>sed '5i\new line' file - Insert before line 5</li>
</ul>
<ul>
<li>sed '5a\new line' file - Append after line 5</li>
</ul>
<ul>
<li>sed '/pattern/i\new line' file - Insert before match</li>
</ul>
<ul>
<li>sed '/pattern/a\new line' file - Append after match</li>
</ul>
<ul>
<li>sed '$a\new line' file - Append to end</li>
</ul>
<hr>
<h2>Advanced Patterns</h2>
<ul>
<li>sed 's/^old/new/' - Match start of line</li>
</ul>
<ul>
<li>sed 's/old$/new/' - Match end of line</li>
</ul>
<ul>
<li>sed 's/old.*/new/' - Match rest of line</li>
</ul>
<ul>
<li>sed 's/\(.*\)old\(.*\)/\1new\2/' - Capture groups</li>
</ul>
<ul>
<li>sed 's/old/new/; t; s/old2/new2/' - Conditional</li>
</ul>
<ul>
<li>sed -n 's/old/new/p' - Print only modified lines</li>
</ul>
<hr>
<h2>Multiple Commands</h2>
<ul>
<li>sed -e 's/old/new/' -e 's/old2/new2/' file - Multiple expressions</li>
</ul>
<ul>
<li>sed 's/old/new/; s/old2/new2/' file - Semicolon separator</li>
</ul>
<ul>
<li>sed -f script.sed file - Read from script file</li>
</ul>
<hr>
<h2>Common Examples</h2>
<h3>Replace Text</h3>
<pre><code>sed 's/old/new/g' file.txt</code></pre>
<p>Replace all occurrences.</p>
<h3>In-Place Edit</h3>
<pre><code>sed -i 's/old/new/g' file.txt</code></pre>
<p>Edit file directly.</p>
<h3>Delete Lines</h3>
<pre><code>sed '/pattern/d' file.txt</code></pre>
<p>Remove matching lines.</p>
<h3>Print Range</h3>
<pre><code>sed -n '10,20p' file.txt</code></pre>
<p>Print lines 10-20.</p>
<h3>Add Line</h3>
<pre><code>sed '5a\new content' file.txt</code></pre>
<p>Insert after line 5.</p>
<h3>Remove Empty Lines</h3>
<pre><code>sed '/^$/d' file.txt</code></pre>
<p>Delete blank lines.</p>
<h3>Remove Leading Spaces</h3>
<pre><code>sed 's/^[ \t]*//' file.txt</code></pre>
<p>Strip whitespace from start.</p>
<h3>Remove Trailing Spaces</h3>
<pre><code>sed 's/[ \t]*$//' file.txt</code></pre>
<p>Strip whitespace from end.</p>
<h3>Number Lines</h3>
<pre><code>sed = file.txt | sed 'N;s/\n/\t/'</code></pre>
<p>Add line numbers.</p>
<h3>Extract Field</h3>
<pre><code>sed 's/.*:\(.*\):.*/\1/' file.txt</code></pre>
<p>Extract between colons.</p>
<hr>
<h2>Tips</h2>
<ul>
<li>Use -i for in-place editing (be careful!)</li>
</ul>
<ul>
<li>Use -i.bak to create backup</li>
</ul>
<ul>
<li>Use -n with p to print only selected lines</li>
</ul>
<ul>
<li>Use g flag for global substitution</li>
</ul>
<ul>
<li>Use different delimiters (|, #) for complex patterns</li>
</ul>
<ul>
<li>Combine with other tools in pipelines</li>
</ul>
<ul>
<li>Test without -i first to verify changes</li>
</ul>
<ul>
<li>Essential for batch text processing</li>
</ul>
</div>
</div>
</div>
</div>
<script async type="text/javascript" src="../blog/analytics.js"></script>
<script src="../theme.js"></script>
</body>
</html>