Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes Monday through Friday.


HPR4113: Today I Learnt, sed hold/pattern space use.

Hosted by Some Guy On The Internet on 2024-05-08 00:00:00
Download or Listen

Today I Learnt, sed hold/pattern space use.

Sgoti talks about using sed hold/pattern spaces.

  • Tags: TIL, sed

I fixed the ${ls} /usr/bin to ${ls} ${bindir} issue mentioned in the show.

#!/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 <https://www.gnu.org/licenses/>.

#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 "\nStep 0: $(${date} +%F), $(${date} +%T)";

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

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

# 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} > ${tmpdoc0} << "EOL0"
bindir=/usr/bin/

EOL0

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

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

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

iabbr here; ${cat} << _EOD_<CR>_EOD_<CR><ESC>2k0

iabbr func function NAME () {<CR><CR>}<UP>
iabbr if; if []; then<CR><ESC>Ielse<CR>${echo} "Good Heavens!"<CR><ESC>Ifi<ESC>4k0A
iabbr ali; alias NAME=''<ESC>B

iabbr ; ()<Left><Left>

EOL1

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

# Bash.vim here-document second pass.
${cat} >> ${tmpdoc1} << EOL1-5

iabbr vars;
bindir=/usr/bin/
<CR>
EOL1-5

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

${sed} -i '/{[}/d; /${bindir}[/d' ${tmpdoc1}

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

# Sed script here-document.
${cat} > ${tmpdoc2} << "EOL2"
#!/usr/bin/sed -f

EOL2

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

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

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

exit;

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

HPR Comments

Mastodon Comments



More Information...


Copyright Information

Unless otherwise stated, our shows are released under a Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license.

The HPR Website Design is released to the Public Domain.