<h1 id="today-i-learnt-sed-holdpattern-space-use.">Today I Learnt, sed
hold/pattern space use.</h1>
<h2 id="sgoti-talks-about-using-sed-holdpattern-spaces.">Sgoti talks
about using sed hold/pattern spaces.</h2>
<ul>
<li>Tags: TIL, sed</li>
</ul>
<h2
id="i-fixed-the-ls-usrbin-to-ls-bindir-issue-mentioned-in-the-show.">I
fixed the <code>${ls} /usr/bin</code> to <code>${ls} ${bindir}</code>
issue mentioned in the show.</h2>
<pre><code>#!/bin/bash
# License: GPL v3
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see &lt;https://www.gnu.org/licenses/&gt;.

#Name: grab-bin.sh
#Purpose: Link your binaries.
#Version: beta 0.07
#Author: SGOTI (Some Guy On The Internet)
#Date: 2023-12-17

#variables:

bindir=/usr/bin/

awk=${bindir}awk
cat=${bindir}cat
chmod=${bindir}chmod
date=${bindir}date
echo=${bindir}echo
find=${bindir}find
ls=${bindir}ls
mktemp=${bindir}mktemp
sed=${bindir}sed
uniq=${bindir}uniq

#start:

    ${echo} -e &quot;\nStep 0: $(${date} +%F), $(${date} +%T)&quot;;

# Create the /tmp/ directory to place the files.
function mkt (){
if [ -d /tmp/$(${date} +%F).* ]; then
    tmpdir1=$(ls -d /tmp/$(${date} +%F).*)
        ${echo} -e &quot;The directory already exists.\n${tmpdir1}&quot;
else
    tmpdir0=$(${mktemp} -d /tmp/$(${date} +%F).XXXXXXXX)
    tmpdir1=${tmpdir0}
    ${find} &quot;${tmpdir1}&quot; -type d -exec ${chmod} -R =700 {} +;
        ${echo} &quot;Had to create ${tmpdir1}&quot;
fi
}

mkt
    ${echo} -e &quot;\nStep 1: $(${date} +%F), $(${date} +%T)&quot;;

# Files created by this script.
tmpdoc0=${tmpdir1}/$(${date} +%Y%m%d)variables.txt
tmpdoc1=${tmpdir1}/$(${date} +%Y%m%d)bash.vim
tmpdoc2=${tmpdir1}/$(${date} +%Y%m%d)sed-script.sed

# Here-document to build the first document (variables.txt).
${cat} &gt; ${tmpdoc0} &lt;&lt; &quot;EOL0&quot;
bindir=/usr/bin/

EOL0

# variables.txt body.
${ls} -1 ${bindir}
| ${sed} -n &#39;
h
s/[^0-9a-zA-Z]//g
G
s/\n/ /
s/(.*) (.*)/1=${bindir}2/p
&#39; &gt;&gt; ${tmpdoc0}
${sed} -i &#39;/[/d&#39; ${tmpdoc0}

    ${echo} -e &quot;\nStep 2: $(${date} +%F), $(${date} +%T)&quot;;

# Bash.vim here-document.
${cat} &gt; ${tmpdoc1} &lt;&lt; &quot;EOL1&quot;
iabbr case;
case ${var_name} in
&lt;CR&gt;    [yY])
&lt;CR&gt;    ${echo} &#39;User said, &quot;Yes&quot;&#39;;
&lt;CR&gt;    ;;
&lt;CR&gt;
&lt;CR&gt;    [nN])
&lt;CR&gt;    ${echo} &#39;User said, &quot;No&quot;&#39;;
&lt;CR&gt;    ;;
&lt;CR&gt;
&lt;CR&gt;    [qQ])
&lt;CR&gt;    ${echo} &quot;Let&#39;s get outta here.&quot;;
&lt;CR&gt;    exit
&lt;CR&gt;    ;;
&lt;CR&gt;
&lt;CR&gt;    *)
&lt;CR&gt;    ${echo} &quot;Good Heavens! Someone broke the script I&#39;m writing.&quot;;
&lt;CR&gt;    exit
&lt;CR&gt;    ;;
&lt;CR&gt;esac

iabbr here; ${cat} &lt;&lt; _EOD_&lt;CR&gt;_EOD_&lt;CR&gt;&lt;ESC&gt;2k0

iabbr func function NAME () {&lt;CR&gt;&lt;CR&gt;}&lt;UP&gt;
iabbr if; if []; then&lt;CR&gt;&lt;ESC&gt;Ielse&lt;CR&gt;${echo} &quot;Good Heavens!&quot;&lt;CR&gt;&lt;ESC&gt;Ifi&lt;ESC&gt;4k0A
iabbr ali; alias NAME=&#39;&#39;&lt;ESC&gt;B

iabbr ; ()&lt;Left&gt;&lt;Left&gt;

EOL1

# bash.vim body.
${ls} -1 ${bindir}
| ${sed} -n &#39; {
h
s/[^0-9a-zA-Z]//g
G
s/\n/ /
s/(.*) (.*)/iabbr 1 ${2}/p
}
&#39; &gt;&gt; ${tmpdoc1}

# Bash.vim here-document second pass.
${cat} &gt;&gt; ${tmpdoc1} &lt;&lt; EOL1-5

iabbr vars;
bindir=/usr/bin/
&lt;CR&gt;
EOL1-5

# bash.vim body second pass.
${ls} -1 ${bindir}
| ${sed} -n &#39; {
h
s/[^0-9a-zA-Z]//g
G
s/\n/ /
s/(.*) (.*)/\&lt;CR&gt;1=${bindir}2/p
}
&#39; &gt;&gt; ${tmpdoc1}

${sed} -i &#39;/{[}/d; /${bindir}[/d&#39; ${tmpdoc1}

    ${echo} -e &quot;\nStep 3: $(${date} +%F), $(${date} +%T)&quot;;

# Sed script here-document.
${cat} &gt; ${tmpdoc2} &lt;&lt; &quot;EOL2&quot;
#!/usr/bin/sed -f

EOL2

# Sed script body.
${ls} -1 ${bindir}
| ${sed} -n &#39;
h
s/[^0-9a-zA-Z]//g
G
s/\n/ /
s/(.*) (.*)/s/\&lt;2\&gt;/${1}/g/p
&#39; &gt;&gt; ${tmpdoc2}
${sed} -i &#39;/[/d&#39; ${tmpdoc2}

${find} &quot;${tmpdir1}&quot; -type d -exec chmod -R =700 {} +;
${find} &quot;${tmpdir1}&quot; -type f -exec chmod -R =600 {} +;

    ${echo} -e &quot;\nStep 4: $(${date} +%F), $(${date} +%T)&quot;;

exit;
</code></pre>
<ul>
<li><strong>Source:</strong> <a
href="https://hackerpublicradio.org/series/0090.html">In-Depth Series:
Learning sed</a></li>
<li><strong>Source:</strong> <a
href="https://hackerpublicradio.org/series/0123.html">In-Depth Series:
Today I Learnt</a></li>
</ul>
<p>This work is licensed under a <a
href="https://creativecommons.org/licenses/by-sa/4.0/"
title="CC-BY-SA 4.0 International">Creative Commons
Attribution-ShareAlike 4.0 International License</a>.</p>

