{"id":12318,"date":"2026-03-06T10:31:50","date_gmt":"2026-03-06T05:01:50","guid":{"rendered":"https:\/\/www.youstable.com\/blog\/?p=12318"},"modified":"2026-03-06T10:31:52","modified_gmt":"2026-03-06T05:01:52","slug":"sed-command-linux","status":"publish","type":"post","link":"https:\/\/www.youstable.com\/blog\/sed-command-linux","title":{"rendered":"SED Command in Linux | Ultimate User Guide (Examples + Tips)"},"content":{"rendered":"\n<p><strong>The SED command in Linux is<\/strong> a non interactive stream editor that reads input line by line, applies text transformations with patterns and rules, and writes the result to standard output or back to files.<\/p>\n\n\n\n<p>Use sed for find and replace, line selection, deletion, insertion, and fast automation in scripts, pipelines, and server administration.<\/p>\n\n\n\n<p>If you\u2019re new to the sed command in Linux, this user guide walks you through everything from basic syntax to real world one liners used in hosting, DevOps, and sysadmin tasks.<\/p>\n\n\n\n<p>You\u2019ll learn sed fundamentals, advanced techniques, and safe in place editing, explained with simple examples you can run immediately.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"what-is-the-sed-command-in-linux\">What Is the SED Command in Linux?<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"2496\" height=\"1664\" src=\"https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2025\/12\/image-25.png\" alt=\"What Is the sed Command in Linux?\" class=\"wp-image-12447\" srcset=\"https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2025\/12\/image-25.png 2496w, https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2025\/12\/image-25-150x100.png 150w\" sizes=\"auto, (max-width: 2496px) 100vw, 2496px\" \/><\/figure>\n\n\n\n<p>sed (short for stream editor) is a powerful text processing tool for filtering and transforming text. It\u2019s ideal for batch modifications, automating configuration changes, cleaning logs, extracting data, and integrating into shell scripts.<\/p>\n\n\n\n<p>Unlike a text editor, sed edits streams, data flowing from files or commands\u2014making it perfect for pipelines and non interactive operations.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"how-sed-works-stream-model-and-core-syntax\">How <strong>SED<\/strong> Works: Stream Model and Core Syntax<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"basic-syntax\">Basic Syntax<\/h3>\n\n\n\n<p><strong>SED<\/strong> processes input line by line, applies commands to each line that matches an address or pattern, and outputs the result. The general form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sed &#91;OPTIONS] 'address(es) command(s)' file(s)\n# Common usage examples\nsed 's\/old\/new\/' file.txt          # Replace first 'old' on each line\nsed -n '1,10p' file.txt            # Print only lines 1 through 10\nsed '\/error\/d' app.log             # Delete lines that match 'error'\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>-n: <\/strong>suppress automatic printing (use with p to control output)<\/li>\n\n\n\n<li><strong>-i[SUFFIX]: <\/strong>edit files in place (optionally with a backup suffix, e.g., -i.bak)<\/li>\n\n\n\n<li><strong>-E: <\/strong>use extended regular expressions (preferred over legacy -r)<\/li>\n\n\n\n<li><strong>-f script.sed:<\/strong> load commands from a file<\/li>\n\n\n\n<li><strong>-e &#8216;cmd&#8217;:<\/strong> add multiple commands inline<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"addresses-and-ranges\">Addresses and Ranges<\/h3>\n\n\n\n<p>Addresses let you target specific lines. You can use line numbers, regex patterns, or ranges:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Line numbers\nsed -n '5p' file          # Print line 5\nsed -n '5,12p' file       # Print lines 5 through 12\n\n# Regex addresses (delimited by \/...\/)\nsed -n '\/ERROR\/p' log     # Print lines containing 'ERROR'\nsed '\/^#\/d' config        # Delete commented lines\n\n# Mixed ranges and address negation\nsed '\/START\/,\/END\/p' file   # Print between lines matching START and END\nsed '\/^$\/!p' file           # Print all non-empty lines (! negates the address)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"essential-sed-commands-with-practical-examples\">Essential SED Commands with Practical Examples<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"selecting-and-printing-lines-p-n-q\">Selecting and Printing Lines (p, -n, q, =)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Print only matching lines (-n + p)\nsed -n '\/nginx\/p' \/etc\/nginx\/nginx.conf\n\n# Show line numbers for matches\nsed -n '\/PermitRootLogin\/=' \/etc\/ssh\/sshd_config\n\n# Stop after first match (faster on huge files)\nsed '\/CRITICAL\/q' app.log\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"find-and-replace-text-s\">Find and Replace Text (s\/\/\/)<\/h3>\n\n\n\n<p>The substitute command s\/regex\/repl\/flags finds matches and replaces them. Use a different delimiter to avoid heavy escaping.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Replace first match per line\nsed 's\/foo\/bar\/' file\n\n# Replace all matches per line (global)\nsed 's\/foo\/bar\/g' file\n\n# Case-insensitive replace (GNU sed)\nsed 's\/linux\/Linux\/Ig' notes.txt\n\n# Use an alternate delimiter to avoid escaping slashes (e.g., paths)\nsed 's#\/var\/www\/html#\/srv\/www#' vhost.conf\n\n# Replace only on lines that match an address\nsed '\/^server_name\/s\/example.com\/example.org\/' site.conf\n\n# Replace only the 2nd occurrence per line\nsed 's\/foo\/bar\/2' input.txt\n\n# Use captured groups and the &amp; (whole match)\nsed -E 's\/(&#91;0-9]{3})-(&#91;0-9]{2})-(&#91;0-9]{4})\/***-**-\\3\/g' data.txt\nsed -E 's\/error:&lt;(&#91;^&gt;]+)&gt;\/issue:&amp;&#91;\\1]\/' log.txt\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"insert-append-and-change-lines-i-a-c\">Insert, Append, and Change Lines (i, a, c)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Insert a line before matches\nsed '\/^\\&#91;mysqld\\]\/i # Managed by automation' my.cnf\n\n# Append a line after matches\nsed '\/listen 80;\/a listen 443 ssl;' nginx.conf\n\n# Change matching lines entirely\nsed '\/^DocumentRoot\/c DocumentRoot \"\/srv\/www\/example\"' httpd.conf\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"delete-lines-d\">Delete Lines (d)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Remove comments and empty lines\nsed -E '\/^\\s*($|#)\/d' file\n\n# Delete a known range\nsed '10,20d' file\n\n# Delete everything outside a block (negate the range)\nsed '\/BEGIN\/,\/END\/!d' file\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"multiple-edits-in-one-run-e-f\">Multiple Edits in One Run (-e, ;, -f)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Chain commands with -e\nsed -E -e 's\/\\s+$\/\/' -e 's\/^\\s+\/\/' input.txt\n\n# Or separate with semicolons\nsed -E 's\/\\s+$\/\/; s\/^\\s+\/\/' input.txt\n\n# Load from a script file\nsed -f fixup.sed input.txt\n# Example fixup.sed:\n#   s\/localhost\/127.0.0.1\/g\n#   \/^#\/d\n#   \/^\\s*$\/d\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"in-place-editing-safely-i-and-portability-tips\">In Place Editing Safely (-i) and Portability Tips<\/h2>\n\n\n\n<p>Editing files in place is convenient but risky if you skip backups. Prefer dry runs first, then apply in-place with a backup suffix.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Dry run: preview changes\nsed -E 's\/http:\/https:\/g' site.conf | diff -u site.conf -\n\n# Linux (GNU sed): create a backup\nsed -i.bak 's\/PermitRootLogin no\/PermitRootLogin prohibit-password\/' \/etc\/ssh\/sshd_config\n\n# macOS\/BSD sed: empty string means no backup\nsed -i '' -E 's#&lt;Directory \/var\/www&gt;#&lt;Directory \/srv\/www&gt;#' httpd.conf\n<\/code><\/pre>\n\n\n\n<p><strong>Portability notes:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use -E for extended regex. On older GNU sed, -r also works; on BSD\/macOS prefer -E.<\/li>\n\n\n\n<li>Case-insensitive flag I is GNU sed. For portability, pre-normalize case or use character classes.<\/li>\n\n\n\n<li>macOS requires -i &#8221; for no-backup; GNU sed accepts -i without an argument.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"advanced-sed-regex-multi-line-and-hold-space\">Advanced SED: Regex, Multi\u2011Line, and Hold Space<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"extended-regex-and-backreferences\">Extended Regex and Backreferences<\/h3>\n\n\n\n<p>With -E, you get modern regex features like +, ?, |, and grouping without backslashes. Backreferences let you reuse captured groups in replacements. The &amp; symbol expands to the whole match.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Normalize repeated whitespace to a single space\nsed -E 's\/&#91;&#91;:space:]]+\/ \/g' file\n\n# Swap first and last name\nsed -E 's\/^(&#91;A-Za-z]+)\\s+(&#91;A-Za-z]+)$\/\\2 \\1\/' names.txt\n\n# Keep only digits from each line\nsed -E 's\/&#91;^0-9]+\/\/g' data.txt\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"multi-line-editing-n-and-line-joins\">Multi Line Editing (N) and Line Joins<\/h3>\n\n\n\n<p>By default sed works per line. Use N to append the next line to the pattern space and operate across line boundaries.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Join pairs of lines with a space\nsed 'N; s\/\\n\/ \/' file\n\n# Remove blank lines that follow another blank line (collapse multiple blanks)\nsed -E ':a; \/^\\s*$\/N; s\/^\\s*\\n+\/\\n\/; ta' file\n\n# Replace a token that may be split across two lines\nsed 'N; s\/TOKEN\\nCONTINUED\/TOKEN CONTINUED\/; P; D' file\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"hold-space-tricks-h-h-g-g-x\">Hold Space Tricks (h, H, g, G, x)<\/h3>\n\n\n\n<p><strong>SED has two buffers:<\/strong> pattern space (current line) and hold space (scratchpad). Move data between them for advanced logic.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Print each line along with the previous line\nsed '1!G; h; $!p' file\n\n# Swap current and previous lines\nsed 'x; 1!p; h' file\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"branching-and-conditional-replacements-b-t\">Branching and Conditional Replacements (b, t)<\/h3>\n\n\n\n<p>Branch to labels to build conditional flows. The t command branches only if a successful substitution occurred.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># If we replaced 'alpha', also replace 'beta' on the same line\nsed -E ':start; s\/alpha\/A\/g; t next; b end; :next s\/beta\/B\/g; :end' file\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"real-world-sed-one-liners-for-sysadmins-and-devops\">Real World SED One Liners for Sysadmins and DevOps<\/h2>\n\n\n\n<p>These practical examples solve common tasks on <a href=\"https:\/\/www.youstable.com\/blog\/configure-ci-cd-on-linux\/\">servers and in CI\/CD<\/a> pipelines.<\/p>\n\n\n\n<p><strong>1) Strip comments and trailing spaces from configs:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sed -E 's\/\\s+$\/\/; \/^\\s*($|#)\/d' config.conf<\/code><\/pre>\n\n\n\n<p><strong>2) Bulk HTTPS upgrade in Nginx vhosts:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/etc\/nginx\/sites-available -type f -name '*.conf' -print0 \\\n | xargs -0 sed -i.bak 's\/http:\/https:\/g'<\/code><\/pre>\n\n\n\n<p><strong>3) Rotate logs by keeping the last 1000 lines:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sed -n -e :a -e '$q;N;101,$D;ba' app.log &gt; app.log.tail<\/code><\/pre>\n\n\n\n<p><strong>4) Mask email usernames for privacy in logs:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sed -E 's\/(&#91;A-Za-z0-9._%+-])&#91;A-Za-z0-9._%+-]*(@&#91;A-Za-z0-9.-]+\\.&#91;A-Za-z]{2,})\/\\1***\\2\/g' access.log<\/code><\/pre>\n\n\n\n<p><strong>5) Convert CSV delimiter from comma to tab (not inside quotes \u2013 keep it simple):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sed 's\/,\/\\t\/g' data.csv &gt; data.tsv<\/code><\/pre>\n\n\n\n<p><strong>6) Update a key\u2019s value only within a specific block:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sed -E '\/&lt;VirtualHost \\*:80&gt;\/, \/&lt;\\\/VirtualHost&gt;\/ s\/ServerName .*\/ServerName example.org\/' vhost.conf<\/code><\/pre>\n\n\n\n<p><strong>7) Remove duplicate adjacent lines (simple dedupe):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sed '$!N; \/^\\(.*\\)\\n\\1$\/!P; D' file<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"sed-vs-awk-vs-grep-when-to-use-which\">SED vs awk vs grep: When to Use Which<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use grep to filter lines by pattern. It\u2019s fast, simple, and best for pure matching.<\/li>\n\n\n\n<li>Use sed to transform text streams: find\/replace, delete\/insert lines, small logic, and quick one-liners.<\/li>\n\n\n\n<li>Use awk for column-aware processing, calculations, and structured outputs (think CSV\/TSV reports).<\/li>\n<\/ul>\n\n\n\n<p>In many pipelines, you combine them: grep narrows data, sed cleans\/formats, awk aggregates or computes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"performance-and-best-practices\">Performance and Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Test with -n and p before in-place edits. Pair with diff for confidence.<\/li>\n\n\n\n<li>Quote commands with single quotes to prevent shell expansion: &#8216;s\/$HOME\/\u2026\/&#8217; not &#8220;s\/$HOME\/\u2026\/&#8221;.<\/li>\n\n\n\n<li>Choose smart delimiters (| # ~) to avoid escaping slashes in paths and URLs.<\/li>\n\n\n\n<li>Anchor patterns with ^ and $ to reduce false positives and speed up matching.<\/li>\n\n\n\n<li>Prefer -E for clearer regex syntax. Keep expressions readable.<\/li>\n\n\n\n<li>For large jobs, store rules in a .sed file (-f). This improves maintainability and speed.<\/li>\n\n\n\n<li>Set LC_ALL=C for faster byte-wise processing when Unicode rules aren\u2019t required.<\/li>\n\n\n\n<li>Beware of portability: GNU vs BSD differences for -i and regex flags.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"practice-safely-on-a-staging-server\">Practice Safely on a Staging Server<\/h2>\n\n\n\n<p>When you batch-edit configs or logs with sed, test on a staging environment first. With a YouStable VPS, you get full root access, snapshot backups, and SSH out of the box\u2014so you can trial sed one-liners, roll back instantly, and push changes to production with confidence. It\u2019s a simple <a href=\"https:\/\/www.youstable.com\/blog\/secure-dedicated-server\/\">way to avoid risky edits on live servers<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"sed-cheat-sheet-commands-youll-use-daily\">SED Cheat Sheet: Commands You\u2019ll Use Daily<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Print:<\/strong> -n &#8216;addr p&#8217; file<\/li>\n\n\n\n<li><strong>Find &amp; replace:<\/strong> &#8216;s\/old\/new\/g&#8217;<\/li>\n\n\n\n<li><strong>Delete:<\/strong> &#8216;addr d&#8217;<\/li>\n\n\n\n<li><strong>Insert\/Append: <\/strong>&#8216;addr i text&#8217; \/ &#8216;addr a text&#8217;<\/li>\n\n\n\n<li><strong>Change line:<\/strong> &#8216;addr c text&#8217;<\/li>\n\n\n\n<li><strong>Line numbers:<\/strong> &#8216;=&#8217;<\/li>\n\n\n\n<li><strong>Multiple commands:<\/strong> -e &#8216;cmd1&#8217; -e &#8216;cmd2&#8217; or &#8216;cmd1; cmd2&#8217;<\/li>\n\n\n\n<li><strong>Extended regex:<\/strong> -E<\/li>\n\n\n\n<li><strong>In-place edit:<\/strong> -i[.bak]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"common-pitfalls-and-how-to-avoid-them\">Common Pitfalls and How to Avoid Them<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Accidental global changes:<\/strong> Start with a narrow address, then widen scope.<\/li>\n\n\n\n<li><strong>Quoting issues:<\/strong> Use single quotes; escape only what sed needs.<\/li>\n\n\n\n<li><strong>Overwriting without backup:<\/strong> Always use -i.bak or version control.<\/li>\n\n\n\n<li><strong>Regex greediness:<\/strong> Use anchors and character classes to match precisely.<\/li>\n\n\n\n<li><strong>Platform mismatch: <\/strong>Adjust -i and regex flags on macOS vs GNU\/Linux.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"faqs\">FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1765512109130\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"what-is-the-sed-command-used-for-in-linux\">What is the SED command used for in Linux?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>SED is a stream editor that automates text transformations such as search-and-replace, deleting or inserting lines, and formatting. It\u2019s widely used in shell scripts, CI\/CD pipelines, log cleanup, and configuration management because it\u2019s fast, scriptable, and works line by line without opening an editor.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1765512116282\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"how-do-i-replace-text-in-all-files-recursively-with-sed\">How do I replace text in all files recursively with SED?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Combine find with sed -i for safe, recursive edits:<\/p>\n<p>find . -type f -name &#8216;*.conf&#8217; -print0 \\<br \/> | xargs -0 sed -i.bak &#8216;s\/old.domain\/new.domain\/g&#8217;<\/p>\n<p>This backs up each file with .bak. Inspect changes with git diff or diff before removing backups.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1765512154196\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"whats-the-difference-between-gnu-sed-and-bsd-macos-sed\">What\u2019s the difference between GNU SED and BSD\/macOS SED?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Key differences include in place editing syntax (-i &#8221; on macOS vs -i on GNU) and some flags (GNU supports I for case-insensitive s\/\/\/). Extended regex is -E on BSD\/macOS and also supported on modern GNU sed. Test portability when writing cross platform scripts.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1765512159915\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"how-can-i-edit-a-file-in-place-without-creating-a-backup\">How can I edit a file in place without creating a backup?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>On GNU\/Linux, use -i without a suffix. On macOS\/BSD, pass an empty string:<\/p>\n<p># GNU\/Linux<br \/>sed -i &#8216;s\/http:\/https:\/g&#8217; file<\/p>\n<p># macOS\/BSD<br \/>sed -i &#8221; &#8216;s\/http:\/https:\/g&#8217; file<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1765512177731\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"how-do-i-use-extended-regex-with-sed\">How do I use extended regex with SED?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use -E to enable extended regular expressions so you can write cleaner patterns without excessive escaping:<\/p>\n<p>sed -E &#8216;s\/([A-Za-z]+)\\s+([A-Za-z]+)\/\\2 \\1\/&#8217; names.txt<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1765512196515\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"how-do-i-replace-only-the-nth-occurrence-on-a-line\">How do I replace only the nth occurrence on a line?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Add the occurrence number as a flag to the s command. This example replaces only the second instance per line:<\/p>\n<p>sed &#8216;s\/error\/warning\/2&#8217; log.txt<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1765512250397\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \" class=\"rank-math-question \" id=\"is-sed-faster-than-awk-or-python-for-simple-replacements\">Is SED faster than awk or Python for simple replacements?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>For basic find and replace or line filtering, sed is typically faster and uses less memory than awk or Python due to minimal startup overhead and streaming execution. For structured parsing or computations, awk\/Python may be more suitable despite a performance tradeoff.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>With these techniques and best practices, you can confidently use the SED <a href=\"https:\/\/www.youstable.com\/blog\/grep-command-in-linux\/\">command in Linux<\/a> to automate text processing, refactor configs, and streamline server workflows. If you want a safe playground with snapshots and full SSH, consider testing on a <a href=\"https:\/\/www.youstable.com\/vps-hosting\/\">YouStable VPS<\/a> before rolling changes into production.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The SED command in Linux is a non interactive stream editor that reads input line by line, applies text transformations [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":14603,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[350,1195],"tags":[],"class_list":["post-12318","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledgebase","category-blogging"],"acf":[],"featured_image_src":"https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2025\/12\/SED-Command-in-Linux-Ultimate-User-Guide.jpg","author_info":{"display_name":"Prahlad Prajapati","author_link":"https:\/\/www.youstable.com\/blog\/author\/prahladblog"},"_links":{"self":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts\/12318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/comments?post=12318"}],"version-history":[{"count":8,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts\/12318\/revisions"}],"predecessor-version":[{"id":19207,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts\/12318\/revisions\/19207"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/media\/14603"}],"wp:attachment":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/media?parent=12318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/categories?post=12318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/tags?post=12318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}