{"id":286,"date":"2026-10-13T08:52:06","date_gmt":"2026-10-13T08:52:06","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2010\/11\/23\/unix-sed-quick-reference\/"},"modified":"2026-10-13T08:52:06","modified_gmt":"2026-10-13T08:52:06","slug":"unix-sed-quick-reference","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/unix-sed-quick-reference\/","title":{"rendered":"UNIX sed Quick Reference: Print and Delete Lines with Address and Pattern Ranges"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><em>Originally published on 2010-11-23; fully revised and updated.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>sed<\/strong> is a non-interactive stream editor used to process text line by line. This guide is for DevOps engineers and sysadmins who need fast, repeatable ways to select or remove lines in logs, config files, and shell pipelines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2010\/11\/unix-sed-quick-reference.png\" alt=\"unix-sed-quick-reference\" width=\"600\" height=\"400\" \/><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What sed does in Unix automation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">sed reads from standard input or a file, applies commands, and writes to standard output. Internally, it works with a <strong>pattern space<\/strong> for the current line and an optional <strong>hold space<\/strong> for temporary storage across cycles.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Use <code>p<\/code> to print selected lines (usually with <code>-n<\/code>).<\/li><li>Use <code>d<\/code> to delete selected lines from output.<\/li><li>Use addresses (line numbers, ranges, regex) to target exactly what should be processed.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How a sed execution cycle works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">sed processes input one cycle at a time: read a line into pattern space, run commands, print (unless suppressed), then continue. If <code>d<\/code> is executed, sed skips printing for that cycle and immediately moves to the next line.<\/p>\n\n\n\n<pre class=\"mermaid\">\nflowchart TD\n    A[Start cycle] --> B[Read next line into pattern space]\n    B --> C[Apply addressed commands]\n    C --> D{Did d command run}\n    D -- yes --> E[Skip printing and continue]\n    D -- no --> F{Is auto print active}\n    F -- yes --> G[Print pattern space]\n    F -- no --> H[Only explicit p prints]\n    E --> I{More input lines}\n    G --> I\n    H --> I\n    I -- yes --> B\n    I -- no --> J[Exit]\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Create the sample file used in these examples<\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript language-bash\">cat &gt; sample.txt &lt;&lt;<span class=\"hljs-string\">'EOF'<\/span>\n<span class=\"hljs-number\">1.<\/span> Linux - Sysadmin, Scripting etc.\n<span class=\"hljs-number\">2.<\/span> Databases - Oracle, mySQL etc.\n<span class=\"hljs-number\">3.<\/span> Hardware\n<span class=\"hljs-number\">4.<\/span> Security (Firewall, Network, Online Security etc)\n<span class=\"hljs-number\">5.<\/span> Storage\n<span class=\"hljs-number\">6.<\/span> Cool gadgets and websites\n<span class=\"hljs-number\">7.<\/span> Productivity (Too many technologies to explore)\n<span class=\"hljs-number\">8.<\/span> Website Design\n<span class=\"hljs-number\">9.<\/span> Software Development\n<span class=\"hljs-number\">10.<\/span>Windows- Sysadmin, reboot etc.\nEOF\n\nnl -ba sample.txt<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Print targeted lines with <code>-n<\/code> and <code>p<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When using <code>p<\/code>, pass <code>-n<\/code> to suppress automatic printing; otherwise matching lines can appear twice.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Address form<\/th><th>Example<\/th><th>What it selects<\/th><th>Portability<\/th><\/tr><\/thead><tbody><tr><td><code>N<\/code><\/td><td><code>sed -n '3p' sample.txt<\/code><\/td><td>Only line N<\/td><td>POSIX<\/td><\/tr><tr><td><code>M~N<\/code><\/td><td><code>sed -n '3~2p' sample.txt<\/code><\/td><td>Every Nth line starting at M<\/td><td>GNU extension<\/td><\/tr><tr><td><code>M,N<\/code><\/td><td><code>sed -n '4,8p' sample.txt<\/code><\/td><td>Inclusive numeric range<\/td><td>POSIX<\/td><\/tr><tr><td><code>$<\/code><\/td><td><code>sed -n '$p' sample.txt<\/code><\/td><td>Last line<\/td><td>POSIX<\/td><\/tr><tr><td><code>N,$<\/code><\/td><td><code>sed -n '4,$p' sample.txt<\/code><\/td><td>From line N to end<\/td><td>POSIX<\/td><\/tr><tr><td><code>\/PATTERN\/<\/code><\/td><td><code>sed -n '\/Sysadmin\/p' sample.txt<\/code><\/td><td>Lines matching regex<\/td><td>POSIX<\/td><\/tr><tr><td><code>\/PATTERN\/,N<\/code><\/td><td><code>sed -n '\/Hardware\/,6p' sample.txt<\/code><\/td><td>From first regex match through line N<\/td><td>POSIX<\/td><\/tr><tr><td><code>N,\/PATTERN\/<\/code><\/td><td><code>sed -n '3,\/Security\/p' sample.txt<\/code><\/td><td>From line N through first later regex match<\/td><td>POSIX<\/td><\/tr><tr><td><code>\/PATTERN\/,$<\/code><\/td><td><code>sed -n '\/Website\/,$p' sample.txt<\/code><\/td><td>From regex match to end<\/td><td>POSIX<\/td><\/tr><tr><td><code>\/PATTERN\/,+N<\/code><\/td><td><code>sed -n '\/Storage\/,+2p' sample.txt<\/code><\/td><td>Matched line plus next N lines<\/td><td>GNU extension<\/td><\/tr><tr><td><code>\/P1\/,\/P2\/<\/code><\/td><td><code>sed -n '\/Storage\/,\/Design\/p' sample.txt<\/code><\/td><td>Range between two regex matches<\/td><td>POSIX<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php language-bash\"><span class=\"hljs-comment\"># Print exactly line 3<\/span>\nsed -n <span class=\"hljs-string\">'3p'<\/span> sample.txt\n<span class=\"hljs-comment\"># 3. Hardware<\/span>\n\n<span class=\"hljs-comment\"># Print every second line starting from line 3 (GNU sed)<\/span>\nsed -n <span class=\"hljs-string\">'3~2p'<\/span> sample.txt\n<span class=\"hljs-comment\"># 3. Hardware<\/span>\n<span class=\"hljs-comment\"># 5. Storage<\/span>\n<span class=\"hljs-comment\"># 7. Productivity (Too many technologies to explore)<\/span>\n<span class=\"hljs-comment\"># 9. Software Development<\/span>\n\n<span class=\"hljs-comment\"># Print lines containing Sysadmin<\/span>\nsed -n <span class=\"hljs-string\">'\/Sysadmin\/p'<\/span> sample.txt\n<span class=\"hljs-comment\"># 1. Linux - Sysadmin, Scripting etc.<\/span>\n<span class=\"hljs-comment\"># 10.Windows- Sysadmin, reboot etc.<\/span>\n\n<span class=\"hljs-comment\"># Print from Storage through Design (inclusive)<\/span>\nsed -n <span class=\"hljs-string\">'\/Storage\/,\/Design\/p'<\/span> sample.txt\n<span class=\"hljs-comment\"># 5. Storage<\/span>\n<span class=\"hljs-comment\"># 6. Cool gadgets and websites<\/span>\n<span class=\"hljs-comment\"># 7. Productivity (Too many technologies to explore)<\/span>\n<span class=\"hljs-comment\"># 8. Website Design<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Delete lines with <code>d<\/code> using the same address patterns<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>d<\/code> is effectively the inverse of <code>p<\/code> for many workflows: it removes matching lines from output and immediately starts the next sed cycle.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Goal<\/th><th>Print selector<\/th><th>Delete selector<\/th><\/tr><\/thead><tbody><tr><td>Nth line<\/td><td><code>sed -n '3p' sample.txt<\/code><\/td><td><code>sed '3d' sample.txt<\/code><\/td><\/tr><tr><td>Range of lines<\/td><td><code>sed -n '4,8p' sample.txt<\/code><\/td><td><code>sed '4,8d' sample.txt<\/code><\/td><\/tr><tr><td>Last line<\/td><td><code>sed -n '$p' sample.txt<\/code><\/td><td><code>sed '$d' sample.txt<\/code><\/td><\/tr><tr><td>Regex matches<\/td><td><code>sed -n '\/Sysadmin\/p' sample.txt<\/code><\/td><td><code>sed '\/Sysadmin\/d' sample.txt<\/code><\/td><\/tr><tr><td>Match to end of file<\/td><td><code>sed -n '\/Website\/,$p' sample.txt<\/code><\/td><td><code>sed '\/Website\/,$d' sample.txt<\/code><\/td><\/tr><tr><td>Blank lines<\/td><td><code>sed -n '\/^$\/p' sample.txt<\/code><\/td><td><code>sed '\/^$\/d' sample.txt<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php language-bash\"><span class=\"hljs-comment\"># Delete line 3<\/span>\nsed <span class=\"hljs-string\">'3d'<\/span> sample.txt\n\n<span class=\"hljs-comment\"># Delete lines 4 through 8<\/span>\nsed <span class=\"hljs-string\">'4,8d'<\/span> sample.txt\n\n<span class=\"hljs-comment\"># Delete all lines containing Sysadmin<\/span>\nsed <span class=\"hljs-string\">'\/Sysadmin\/d'<\/span> sample.txt\n\n<span class=\"hljs-comment\"># Delete from Website to end of file<\/span>\nsed <span class=\"hljs-string\">'\/Website\/,$d'<\/span> sample.txt\n\n<span class=\"hljs-comment\"># Delete blank lines<\/span>\nsed <span class=\"hljs-string\">'\/^$\/d'<\/span> sample.txt<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">GNU sed extensions and portability notes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Most basic address forms are portable, but some compact selectors are GNU-only. If your scripts run across Linux and macOS\/BSD systems, validate syntax before production rollout.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>POSIX sed<\/th><th>GNU sed<\/th><th>BSD\/macOS sed<\/th><\/tr><\/thead><tbody><tr><td>Basic addresses (<code>N<\/code>, <code>M,N<\/code>, <code>$<\/code>, <code>\/re\/<\/code>)<\/td><td>Yes<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td><code>M~N<\/code> step addressing<\/td><td>No<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td><code>\/re\/,+N<\/code> relative range<\/td><td>No<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td><code>-i<\/code> in-place edit without suffix<\/td><td>Not standardized<\/td><td>Yes<\/td><td>No (use empty or explicit suffix)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php language-bash\"><span class=\"hljs-comment\"># Cross-platform friendly in-place edit with backup<\/span>\nsed -i.bak <span class=\"hljs-string\">'\/^$\/d'<\/span> sample.txt\n\n<span class=\"hljs-comment\"># Portable fallback without -i<\/span>\nsed <span class=\"hljs-string\">'\/^$\/d'<\/span> sample.txt &gt; sample.txt.tmp &amp;&amp; mv sample.txt.tmp sample.txt<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Start with read-only commands (<code>-n<\/code> + <code>p<\/code>) before applying destructive edits.<\/li><li>Prefer single-quoted sed scripts in shell to avoid accidental variable expansion.<\/li><li>Remember sed uses basic regular expressions by default.<\/li><li>Use backups (<code>-i.bak<\/code>) or temp files for safe bulk edits.<\/li><li>Document GNU-only syntax in scripts to prevent cross-platform surprises.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Use this sed quick reference to print or delete exact lines, ranges, and regex matches without opening an editor. It also highlights GNU vs POSIX differences and safer in-place editing patterns.<\/p>\n","protected":false},"author":1,"featured_media":4353,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4957],"tags":[4084,2540,938,4080,169,4081,4088,4086,4082,4085,4083,4087],"class_list":["post-286","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-introduction-of-unix-sed","tag-quick","tag-reference","tag-sed","tag-unix","tag-unix-sed","tag-unix-sed-command","tag-unix-sed-guide","tag-unix-sed-introduction","tag-unix-sed-overview","tag-unix-sed-working-method","tag-what-is-unix-sed"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=286"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/286\/revisions"}],"predecessor-version":[{"id":79123,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/286\/revisions\/79123"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/4353"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}