{"id":2136,"date":"2021-05-29T23:35:53","date_gmt":"2021-05-29T23:35:53","guid":{"rendered":"https:\/\/dft.wiki\/?p=2136"},"modified":"2026-06-08T22:45:18","modified_gmt":"2026-06-09T02:45:18","slug":"sed-cheat-sheet-stream-editor-with-regex","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=2136","title":{"rendered":"SED Cheat Sheet &#8211; Stream Editor with RegEx"},"content":{"rendered":"<p>Sed is a text\/string editor command-line tool.<\/p>\n<p>Follow the most basic commands:<\/p>\n<ul>\n<li>sed &#8216;<strong>s\/<span style=\"color: #ff0000;\">LOOKFOR<\/span>\/<span style=\"color: #0000ff;\">REPLACE<\/span>\/<\/strong>&#8216; <strong>file.txt<\/strong>\n<ul>\n<li>reads the <strong>file,<\/strong> <span style=\"color: #ff0000;\"><strong>looks for a pattern<\/strong><\/span> and <span style=\"color: #0000ff;\"><strong>replaces it<\/strong><\/span> on a <strong>match<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li><strong>cat file.txt |<\/strong> sed &#8216;s\/LOOKFOR\/REPLACE\/&#8217;\n<ul>\n<li><strong>piping<\/strong> the input to sed.<\/li>\n<\/ul>\n<\/li>\n<li>cat file.txt | sed &#8216;s\/LOOKFOR\/REPLACE\/&#8217; <strong>output.txt<\/strong>\n<ul>\n<li><strong>piping the input<\/strong> and redirecting the <strong>output to a file<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;s\/LOOKFOR\/REPLACE\/&#8217; <strong>file.txt &gt; output.txt<\/strong>\n<ul>\n<li>file <strong>input<\/strong> and <strong>output.<\/strong><\/li>\n<\/ul>\n<\/li>\n<li>cat file.txt | sed &#8216;s\/LOOKFOR\/REPLACE\/<strong>g<\/strong>&#8216;\n<ul>\n<li>the <strong>global<\/strong> flag prevents stopping on the first match.<\/li>\n<\/ul>\n<\/li>\n<li>sed <strong>-i<\/strong> &#8216;s\/LOOKFOR\/REPLACE\/&#8217; file.txt\n<ul>\n<li>edits the original file <strong>in-place<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>cat file.txt | sed &#8216;s\/[0-9]\/(&amp;)\/g&#8217;\n<ul>\n<li><strong>&amp;<\/strong> represents the <strong>matched string<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>cat file.txt | sed &#8216;s<strong>_<\/strong>[0-9]<strong>_<\/strong>(&amp;)<strong>_<\/strong>g&#8217;\n<ul>\n<li>using alternative <strong>delimiters<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>cat file.txt | sed &#8216;s\/<strong>\\\/<\/strong>\/*\/g&#8217;\n<ul>\n<li><strong>escaping special characters<\/strong> by adding a backslash before them.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;s\/[<strong>^<\/strong>0-9]\/REPLACE\/&#8217; file.txt\n<ul>\n<li>replaces anything that is <strong>not a number<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;s\/LOOKFOR\/REPLACE\/g<strong>;s\/LOOKFOR\/REPLACE<\/strong>\/g&#8217; file.txt\n<ul>\n<li>applies <strong>multiple substitutions<\/strong> in one command, separated by a <strong>semicolon<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>sed <strong>-f<\/strong> replacements.txt file.txt\n<ul>\n<li>loads <strong>patterns from a file<\/strong> (one per line).<\/li>\n<\/ul>\n<\/li>\n<li>sed <strong>-n<\/strong> &#8216;s\/LOOKFOR\/REPLACE\/g&#8217; file.txt\n<ul>\n<li><strong>suppresses<\/strong> screen output.<\/li>\n<\/ul>\n<\/li>\n<li>sed -n &#8216;s\/LOOKFOR\/REPLACE\/<strong>p<\/strong>g&#8217; file.txt\n<ul>\n<li><strong>outputs only<\/strong> lines that had <strong>matches<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;s\/LOOKFOR\/REPLACE\/<strong>I<\/strong>g&#8217; file.txt\n<ul>\n<li><strong>case-insensitive<\/strong> pattern matching.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;\/LOOKFOR\/<strong>d<\/strong>&#8216; file.txt\n<ul>\n<li><strong>deletes<\/strong> matching lines.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;<strong>1!{<\/strong>s\/LOOKFOR\/REPLACE\/g<strong>;}<\/strong>&#8216; file.txt\n<ul>\n<li><strong>skips<\/strong> the first line.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;<strong>4 q<\/strong>&#8216; file.txt\n<ul>\n<li>reads only the first <strong>4 lines, then quits<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;<strong>5,$ d<\/strong>&#8216; file.txt\n<ul>\n<li><strong>deletes<\/strong> all lines from <strong>line 5<\/strong> onward.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;<strong>=<\/strong>&#8216; file.txt\n<ul>\n<li>prints the <strong>line number<\/strong> before each line.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;=&#8217; file.txt | sed &#8216;<strong>N; s\/\\n\/ \/<\/strong>&#8216;\n<ul>\n<li>prints the <strong>line number<\/strong> at the <strong>beginning<\/strong> of each line.<\/li>\n<\/ul>\n<\/li>\n<li>sed <strong>-n<\/strong> &#8216;<strong>=<\/strong>&#8216; file.txt\n<ul>\n<li>prints <strong>only line numbers<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;<strong>$<\/strong>=&#8217; file.txt\n<ul>\n<li>prints the line number of the <strong>last line<\/strong> (i.e. the <strong>total line count<\/strong>).<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;<strong>n;d<\/strong>&#8216; file.txt\n<ul>\n<li>prints <strong>even-numbered lines<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;<strong>1!<\/strong>n;d&#8217; file.txt\n<ul>\n<li>prints <strong>odd-numbered lines<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>sed -n &#8216;<strong>p;n;n<\/strong>&#8216; file.txt\n<ul>\n<li><strong>prints<\/strong> one line then <strong>skips<\/strong> the next two, and repeats.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;\/&#8221;[a-z]\/ s\/&#8221;\/_\/g&#8217;\n<ul>\n<li>finds a pattern, then applies the substitution only on lines that match.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;s\/\\<span style=\"color: #ff6600;\"><strong>(<\/strong><\/span>[0-9]\\<span style=\"color: #ff6600;\"><strong>)<\/strong><\/span>-\\<span style=\"color: #00ccff;\"><strong>(<\/strong><\/span>[0-9]\\<span style=\"color: #00ccff;\"><strong>)<\/strong><\/span>\/<span style=\"color: #00ccff;\"><strong>\\2<\/strong><\/span>&#8211;<span style=\"color: #ff6600;\"><strong>\\1<\/strong><\/span>\/g&#8217;\n<ul>\n<li>The <strong>parentheses define capture groups<\/strong> 1 and 2; their values are then placed in reverse order.\n<ul>\n<li>Note: capture group &#8220;zero&#8221; (<strong>\\0<\/strong>) represents the entire match.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<p><strong>SOURCES<\/strong><\/p>\n<p>Excellent deep dive into all Sed functionality [<a href=\"https:\/\/www.grymoire.com\/Unix\/Sed.html\">Link<\/a>]<\/p>\n<hr \/>\n<p>Perl can also perform similar tasks with the following syntax:<\/p>\n<pre>perl -p -i -e 's#LOOKFOR#R#' myfile1 myfile2<\/pre>\n<hr \/>\n<p><strong>BONUS<\/strong><\/p>\n<p>Command-line JSON processor <strong>jq<\/strong> combined with <strong>sponge<\/strong>:<\/p>\n<pre>jq '.\"attributeName\" = \"value\"' \/PATH\/fileName.json | sponge \/PATH\/fileName.json<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sed is a text\/string editor command-line tool. Follow the most basic commands: sed &#8216;s\/LOOKFOR\/REPLACE\/&#8216; file.txt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-2136","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2136"}],"version-history":[{"count":12,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2136\/revisions"}],"predecessor-version":[{"id":5729,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2136\/revisions\/5729"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}