{"id":2136,"date":"2021-05-29T23:35:53","date_gmt":"2021-05-29T23:35:53","guid":{"rendered":"https:\/\/dft.wiki\/?p=2136"},"modified":"2022-01-13T12:45:16","modified_gmt":"2022-01-13T12:45:16","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>reading the <strong>file,<\/strong>\u00a0<span style=\"color: #ff0000;\"><strong>look for a pattern<\/strong><\/span> then <span style=\"color: #0000ff;\"><strong>replacing it<\/strong><\/span> when there is 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>pipping 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>using <strong>global<\/strong> will not stop 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>changes the original file <strong>in-line<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>cat file.txt | sed &#8216;s\/[0-9]\/(&amp;)\/g&#8217;\n<ul>\n<li>percentage symbol represents the <strong>matching 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 other characters and <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>scaping special characters<\/strong> 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>replacing 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>applying <strong>multiple iterations<\/strong> in the same command (separated by <b>semicolon)<\/b>.<\/li>\n<\/ul>\n<\/li>\n<li>sed <strong>-f<\/strong> replacements.txt file.txt\n<ul>\n<li>loading a <strong>file with the patterns<\/strong> (one on each 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>disabling<\/strong> the 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>only outputs<\/strong> the 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>case <strong>insensitive<\/strong> for patterns.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;\/LOOKFOR\/<strong>d<\/strong>&#8216; file.txt\n<ul>\n<li><strong>deleting<\/strong> the matches.<\/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>skipping<\/strong> the first line.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;<strong>4 q<\/strong>&#8216; file.txt\n<ul>\n<li>only loads the first <strong>4 lines and then quit<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;<strong>5,$ d<\/strong>&#8216; file.txt\n<ul>\n<li>it starts from <strong>line 5 and deletes<\/strong> all the other lines.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;<strong>=<\/strong>&#8216; file.txt\n<ul>\n<li>printing the <strong>line number<\/strong> before printing the 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>printing 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>printing <strong>only line numbers<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;<strong>$<\/strong>=&#8217; file.txt\n<ul>\n<li>printing the line number of the <strong>last line<\/strong> (<strong>counting<\/strong> the number of lines).<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;<strong>n;d<\/strong>&#8216; file.txt\n<ul>\n<li>printing <strong>even lines<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;<strong>1!<\/strong>n;d&#8217; file.txt\n<ul>\n<li>printing <strong>odd 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>printing<\/strong> one line and <strong>skip<\/strong> the next two, and so on.<\/li>\n<\/ul>\n<\/li>\n<li>sed &#8216;\/&#8221;[a-z]\/ s\/&#8221;\/_\/g&#8217;\n<ul>\n<li>look for the pattern then apply the substitutions only on the lines with the match to the matched pattern.<\/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>parenthesis defined the capture groups<\/strong> 1 and 2 then the values of the groups were placed in reverse order.\n<ul>\n<li>Note: capture group &#8220;zero&#8221; (<strong>\\0<\/strong>) is the whole 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 functionalities of Sed [<a href=\"https:\/\/www.grymoire.com\/Unix\/Sed.html\">Link<\/a>]<\/p>\n<hr \/>\n<p>Pear is also able to 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 called <strong>jq<\/strong> in combination 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":11,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2136\/revisions"}],"predecessor-version":[{"id":2750,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2136\/revisions\/2750"}],"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}]}}