Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Using external variables in awk

See: Accessing external variable in AWK and SED
var="BASH"; echo "unix scripting" | awk '{gsub(/unix/,"'"${var}"'"); print}'
var="BASH"; echo "unix scripting" | awk '{gsub(/unix/,"'"$(echo ${var})"'"); print}'
var="BASH"; echo "unix scripting" | awk -v v="$var" '{sub(/unix/,v); print}'

Remove leading & trailing whitespace from a Bash variable

# Taken from: http://www.jlaforums.com/viewtopic.php?t=1427403
# Author: Chris F.A. Johnson

var='   space test   '
echo $var | sed -n -e 'l'
echo "$var" | sed -n -e 'l'

printf "%s" "${var#"${var%%[![:space:]]*}"}" | sed -n -e 'l'
printf "%s" "${var%"${var##*[![:space:]]}"}" | sed -n -e 'l'


# Space characters include: tab, newline, vertical tab, form feed, carriage return, and space.
# cf. "POSIX character classes" at http://en.wikipedia.org/wiki/Regular_expression

var="${var#"${var%%[![:space:]]*}"}"   # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}"   # remove trailing whitespace characters

echo "$var" | sed -n -e 'l'

add2path

# version 1

function add2path() {

   declare defaultpath path pathvar

   # add the new path at the beginning of $PATHVARIABLE
   if [[ "${1}" == '-b'  ]]; then
      if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi
      path="${3}:${2}"
      #path="${path// /}"   # remove spaces
      printf "%s" "${path//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' 

   # add the new path at the end of $PATHVARIABLE
   elif [[ "${1}" == '-e'  ]]; then
      if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi
      # first remove identical path from $PATHVARIABLE (path identical with ${3}) if necessary
      pathvar="$(printf "%s" "${2//:/$'\n'}" | /usr/bin/egrep -v "^${3}$" | /usr/bin/tr '\n' ':')"
      path="${pathvar}:${3}"
      path="${path//::/:}"
      #path="${path// /}"   # remove spaces
      printf "%s" "${path//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' 

   # default: add the new path at the beginning of $PATHVARIABLE
   elif [[ $# -eq 2 ]]; then
      if [[ ! -d "${2}" ]]; then printf "%s" "${1}"; return 1; fi
      path="${2}:${1}"
      #path="${path// /}"       # remove spaces
      printf "%s" "${path//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':'
 
   else

defaultpath='
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin 
/usr/local/sbin
/usr/local/lib
/usr/local/include
/usr/local/man
/opt/local/bin
/opt/local/sbin
/opt/local/lib
/opt/local/include
/opt/local/man
/usr/X11R6
/usr/X11R6/bin
/usr/X11R6/include
/usr/X11R6/lib
/usr/X11R6/man
'

      defaultpath="${defaultpath// /}"        # remove spaces
      defaultpath="${defaultpath//$'\n'/:}"   # convert newlines into colons
      defaultpath="${defaultpath#:}"          # cut off leading colon
      defaultpath="${defaultpath%:}"          # cut off trailing colon
      printf "%s" "${defaultpath}"
      return 1   

   fi

   return 0
}

# usage: 
# add2path [$PATHVARIABLE] /path/to/dir
# add2path [-b|-e] [$PATHVARIABLE] /path/to/dir


# store the original $PATH
OPATH="${PATH}"

# create test directories
testdir="${HOME}/Desktop/testdir"
/bin/mkdir -p "${testdir}"
for ((i=0;i<=5;i++)) { /bin/mkdir -p "${testdir}${i}"; }

# list test directories
echo "${testdir}"
for ((i=0;i<=5;i++)) { echo "${testdir}${i}"; }


add2path "${PATH}" "${testdir}" | tr ':' '\n' | nl
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

export PATH="$(add2path "${PATH}" "${testdir}")"
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}")"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path -b "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


add2path | tr ':' '\n' | nl

add2path abc | tr ':' '\n' | nl

add2path -b "${PATH}" "${testdir}" | tr ':' '\n' | nl
add2path -e "${PATH}" "${testdir}" | tr ':' '\n' | nl


# restore original $PATH
export PATH="${OPATH}"
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


#--------------------------------------------------------------


# version 2

function add2path() {

   # add the new path at the beginning of $PATHVARIABLE
   if [[ "${1}" == '-b'  ]]; then
      if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi
      printf "%s" "${3}:${2}" | /usr/bin/sed -E -e '/^:+/s/^:+//' -e '/:+$/s/:+$//' -e '/::+/s/::+/:/g' | /usr/bin/tr ':' '\n' | \
         /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' | /usr/bin/sed -E -e '/:+$/s/:+$//' 

   # add the new path at the end of $PATHVARIABLE
   elif [[ "${1}" == '-e'  ]]; then
      if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi

      # first remove identical path from $PATHVARIABLE (path identical with ${3}) if necessary
      pathvar="$(printf "%s" "${2}" | /usr/bin/tr ':' '\n' | /usr/bin/egrep -v "^${3}$" | /usr/bin/tr '\n' ':')"

      printf "%s" "${pathvar}:${3}" | /usr/bin/sed -E -e '/^:+/s/^:+//' -e '/:+$/s/:+$//' -e '/::+/s/::+/:/g' | /usr/bin/tr ':' '\n' | \
         /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' | /usr/bin/sed -E -e '/:+$/s/:+$//' 

   # default: add the new path at the beginning of $PATHVARIABLE
   elif [[ $# -eq 2 ]]; then
      if [[ ! -d "${2}" ]]; then printf "%s" "${1}"; return 1; fi
      printf "%s" "${2}:${1}" | /usr/bin/sed -E -e '/^:+/s/^:+//' -e '/:+$/s/:+$//' -e '/::+/s/::+/:/g' | /usr/bin/tr ':' '\n' | \
         /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' | /usr/bin/sed -E -e '/:+$/s/:+$//' 

   else

path='
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin 
/usr/local/sbin
/usr/local/lib
/usr/local/include
/usr/local/man
/opt/local/bin
/opt/local/sbin
/opt/local/lib
/opt/local/include
/opt/local/man
/usr/X11R6
/usr/X11R6/bin
/usr/X11R6/include
/usr/X11R6/lib
/usr/X11R6/man
'

      printf "%s" "${path// /}" | /usr/bin/tr '\n' ':' | /usr/bin/sed -E '/^:|:$/s/^:|:$//g'

      return 1   

   fi

   return 0
}


# usage: 
# add2path [$PATHVARIABLE] /path/to/dir
# add2path [-b|-e] [$PATHVARIABLE] /path/to/dir


OPATH="${PATH}"

testdir="${HOME}/Desktop/testdir"
/bin/mkdir -p "${testdir}"
for ((i=0;i<=5;i++)) { /bin/mkdir -p "${testdir}${i}"; }


printf "%s\n" "${PATH}" | tr ':' '\n' | nl
export PATH="$(add2path "${PATH}" "${testdir}" )"
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path -b "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


export PATH="${OPATH}"
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


#---------------------------------------------------------------


function remove_from_path() {

   declare defaultpath newpath

   if [[ $# -ne 2 ]]; then

defaultpath='
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin 
/usr/local/sbin
/usr/local/lib
/usr/local/include
/usr/local/man
/opt/local/bin
/opt/local/sbin
/opt/local/lib
/opt/local/include
/opt/local/man
/usr/X11R6
/usr/X11R6/bin
/usr/X11R6/include
/usr/X11R6/lib
/usr/X11R6/man
'

      defaultpath="${defaultpath// /}"        # remove spaces
      defaultpath="${defaultpath//$'\n'/:}"   # convert newlines into colons
      defaultpath="${defaultpath#:}"          # cut off leading colon
      defaultpath="${defaultpath%:}"          # cut off trailing colon
      printf "%s" "${defaultpath}"

      return 1
   fi 

   if [[ ! -d "${2}" ]]; then printf "%s" "${1}"; return 1; fi

   newpath="$(printf "%s" "${1//:/$'\n'}" | /usr/bin/egrep -v "^${2}$" | /usr/bin/tr '\n' ':')"
   #newpath="${newpath// /}"        # remove spaces
   newpath="${newpath#:}"          # cut off leading colon
   newpath="${newpath%:}"          # cut off trailing colon
   #printf "%s" "${newpath//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' 
   printf "%s" "${newpath}"

   return 0
}

printf "%s\n" "${PATH}" | tr ':' '\n' | nl

export PATH="$(remove_from_path "${PATH}" "/opt/local/bin" )"
export PATH="$(remove_from_path "${PATH}" "/opt/local/xyz" )"
export PATH="$(remove_from_path "${PATH}" "" )"
export PATH="$(remove_from_path "${PATH}" )"

printf "%s\n" "${PATH}" | tr ':' '\n' | nl


#---------------------------------------------------------------


man bash 2>/dev/null | less -p "Functions are executed"

# Functions are executed in the context of the current shell; no new process is created to 
# interpret  them  (contrast  this with the execution of a shell script).

Linking to articles using the article’s weblog in ExpressionEngine

I have an index template that displays articles from several sections (weblogs), but when linking to an article we must specify the template_group and template to use. I have different template groups for each section, and want to use them as they define the article’s URL. However, the obvious way of using {title_permalink="{weblog_short_name}/index"} doesn’t work (although we can use {segment_#} like this).

You can do this by passing the {weblog_short_name} and {entry_id} of each article to an embedded template using embedded variables:

<!-- the index template that calls the embedded template -->
{exp:weblog:entries weblog="not pages" orderby="date" sort="desc" limit="10" disable="categories|comments|member_data|pagination|trackbacks"}
<div class="article">
{date_heading}<h3 class="date">{entry_date}</h3>{/date_heading}
<h3>{weblog}</h3>
<h2 class="title"><a href="{target_url}" title="{target_title}">{title}</a></h2>
{summary}
<!-- the next line embeds the template and sets the variables -->
<p><a href="{embed="snippet/body-hp-link" the_weblog="{weblog_short_name}" the_entry_id="{entry_id}"}">Read more</a> or comment&hellip;</p>
</div>{/exp:weblog:entries}


<!-- the embedded template -->
{exp:weblog:entries weblog="{embed:the_weblog}" entry_id="{embed:the_entry_id}" dynamic="off" disable="categories|comments|member_data|pagination|trackbacks"}{title_permalink="{embed:the_weblog}/index"}{/exp:weblog:entries}

scp to my journal

// copy entries to journal

// simple snippet that shows taking a command line variable and doing a longer command that includes that variable

#!/bin/bash        
if [ -z "$1" ]; then 
    echo usage: $0 "MMM (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec)"
    exit
fi
MMM=$1
scp *.html robnugen.com:$MMM"_journal"