Snippets

Víctor Goñi Sanz Bash tutorial 3 - Regular expresion and sed

Created by Víctor Goñi Sanz last modified
#!/bin/bash
# Check spell http://www.shellcheck.net/# online, or download, to test!

############################################################ Function declaration
### OPTIONS
f_template()
{
  echo -e "\n--------------------------------------------------"
  echo -e "--------------------------------------------------\n"
}
# This function, display usage info
f_display_usage()
{
  echo -e "\n--------------------------------------------------"
	echo "|  This function teach you how this script works"
	echo -e "|  Usage:\n|  $0"  # -e option allow use \n
  echo "|  ${optionHelp} = Print usage"
  echo "|  ${optionTest} = Print a test with strings before and after"
  echo "|  ${optionRegExp} = Print regular expression"
  echo "|  ${optionSetInput} = Set input path, put before processing or default path is used"
  echo "|  ${optionSetOutput} = See output path, put before processing or default path is used"
  echo "|  ${optionGenerateInput} = Generate files in input path, if you want them"
  echo "|  ${optionPSP} = Process all files from input, remove PSP and dashes"
  echo "|  ${optionAllBrackets} = Process all files from input, remove parenthesis and brackets"
  echo -e "--------------------------------------------------\n"
}

# Remove "PSP -" for all files given
f_remove_psp_at_start()
{
  echo -e "\n--------------------------------------------------"
  f_create_output
  echo "  Removing PSP - at beggining in files at path: ${inputpath}"
  counter=0
  for file in "${inputpath}"/*    # * = All items, in output path
  do
      outfile=$(f_remove_characters_only "${file##*/}" "PSP")
      outfile=$(f_remove_dash_only "${outfile}")
      echo "  Filename ${counter}: ${file##*/} --> ${outfile}"
      cp "${file}" "${outputpath}/${outfile}"
      let "counter += 1"  # Forward
  done
  echo "  ${counter} files processed! All files copied in output"
  echo -e "--------------------------------------------------\n"
}
f_remove_parenthesis_and_brackets()
{
  echo -e "\n--------------------------------------------------"
  f_create_output
  echo "  Removing brackets in files at path: ${inputpath}"
  counter=0
  for file in "${inputpath}"/*    # * = All items, in output path
  do
      filewithoutpath="${file##*/}"   # Remove full path
      filenameraw="${filewithoutpath%.*}"    # Extract string before . from variable filename
      extension="${filewithoutpath##*.}"         # Extract string after . from variable filename
      outfile=$(f_remove_spaces_at_end "${filenameraw}")
      outfile=$(f_remove_parenthesis "${outfile}")
      outfile=$(f_remove_square_brackets "${outfile}")
      outfile=$(f_remove_curly_brackets "${outfile}")
      outfile=$(f_remove_morethan2spaces "${outfile}")
      echo "  Filename ${counter}: ${file##*/} --> ${outfile}.${extension}"
      cp "${file}" "${outputpath}/${outfile}.${extension}"
      let "counter += 1"  # Forward
  done
  echo "  ${counter} files processed! All files copied in output"
  echo -e "--------------------------------------------------\n"
}
# Sed info: http://www.grymoire.com/Unix/Sed.html#uh-4
f_test_sed()
{
  inputvar="PSP - [square]   The Answer (was) is 42 (or 43) this answer is {Correct} or {or not} [works]   "
  temp=$(f_remove_spaces_at_end "${inputvar}")
  echo -e "   - f_remove_spaces_at_end\n   · ${inputvar}\n   * ${temp}"
  temp=$(f_remove_parenthesis "${inputvar}")
  echo -e "   - f_remove_parenthesis\n   · ${inputvar}\n   * ${temp}"
  temp=$(f_remove_curly_brackets "${inputvar}")
  echo -e "   - f_remove_curly_brackets\n   · ${inputvar}\n   * ${temp}"
  temp=$(  f_remove_square_brackets "${inputvar}")
  echo -e "   - f_remove_square_brackets\n   · ${inputvar}\n   * ${temp}"
  temp=$(  f_remove_morethan2spaces "${inputvar}")
  echo -e "   - f_remove_morethan2spaces\n   · ${inputvar}\n   * ${temp}"
  temp=$(f_remove_parenthesis_only "${inputvar}")
  echo -e "   - f_remove_parenthesis_only\n   · ${inputvar}\n   * ${temp}"
  temp=$(f_remove_characters_only "${inputvar}" "PSP")
  echo -e "   - f_remove_characters_only\n   · ${inputvar}\n   * ${temp}"
  temp=$(f_remove_dash_only "${inputvar}" )
  echo -e "   - f_remove_dash_only\n   · ${inputvar}\n   * ${temp}"
  temp=$(f_remove_square_brackets_only "${inputvar}")
  echo -e "   - f_remove_square_brackets_only\n   · ${inputvar}\n   * ${temp}"
  echo -e "   - Concatenate functions"
  temp=$(f_remove_parenthesis "${inputvar}")
  temp=$(f_remove_square_brackets "${temp}")
  temp=$(f_remove_curly_brackets "${temp}")
  temp=$(f_remove_characters_only "${temp}" "PSP")
  temp=$(f_remove_dash_only "${temp}")
  echo -e "   - Result: ${temp}"
  #
  echo -e "--------------------------------------------------\n"
}
f_test_regularexpresion()
{
  echo -e "\n--------------------------------------------------"
  echo "   Printing different regular expression..."
  inputvar=$(echo {A..z})
  echo "   · {A..z} = ${inputvar} "
  inputvar=$(echo {A..Z})
  echo "   · {A..Z} = ${inputvar} "
  inputvar=$(echo {a..z})
  echo "   · {A..z} = ${inputvar} "
  inputvar=$(echo {0..9})
  echo "   · {0..9} = ${inputvar} "
  inputvar=$(echo {0..9} & echo {A..z})
  echo "   · {0..9} & {A..z} = ${inputvar} "
  #
  echo -e "--------------------------------------------------\n"
}
f_generate_files()
{
  echo -e "\n--------------------------------------------------"
  f_create_input
  touch ${inputpath}/"file1.dat"
  touch ${inputpath}/"file1(1).dat"
  touch ${inputpath}/"file12(2).dat"
  touch ${inputpath}/"file12(2)(3).dat"
  touch ${inputpath}/"PSP - file7.dat"
  touch ${inputpath}/"PSP - file2 [as].dat"
  touch ${inputpath}/"PSP - file3 {asd} .dat"
  touch ${inputpath}/"file4[!]  [ao]{32}.dat"
  touch ${inputpath}/"file5[!]  [ao](32).dat"
  touch ${inputpath}/"file6[ao]{32}(ds12).dat"
  echo -e "--------------------------------------------------\n"
}
### Other functions
# Check if exist input directory and create if not
f_create_input()
{
  if [ ! -d "${inputpath}" ] # d for directory
  then
    echo "  Creating input folder..."
    mkdir ${inputpath}
  fi
}
# Check if exist output directory and create if not
f_create_output()
{
  if [ ! -d "${outputpath}" ] # d for directory
  then
    echo "  Creating output folder..."
    mkdir ${outputpath}
  fi
}
# Check if are provided correct arguments
f_evaluate_options()
{
  echo "  Evaluation options, number of arguments: ${Nargs}"
  eval set -- ${options}
  echo "  Options: ${options}"
}
# Remove parenthesis (*) and content inside!
f_remove_parenthesis()
{
  temp=$(echo "${1}" | sed s/'([^)]*)'/''/g)
  echo ${temp}
}
# Remove braces or curly brackets {*} and content inside!
f_remove_curly_brackets()
{
  temp=$(echo "${1}" | sed ':again;$!N;$!b again; s/{[^}]*}//g')
  echo ${temp}
}
# Remove square brackets [*] and content inside!
f_remove_square_brackets()
{
  temp=$(echo "${1}" | sed 's/\[[^]]*\]//g' )
  echo ${temp}
}
f_remove_morethan2spaces()
{
  temp=$(echo "${1}" | sed -r s/'\s{2,}'/' '/g )
  echo ${temp}
}
f_remove_spaces_at_end()
{
  temp=$(echo "${1}" | sed 's/[[:blank:]]*$//' )
  echo ${temp}
}
f_remove_dash_only()
{
  temp=$(echo "${1}" | sed 's/\-/ /' )
  echo ${temp}
}
f_remove_parenthesis_only()
{
  temp=$(echo "${1}" | sed 's/[()]//g' )
  echo ${temp}
}
f_remove_characters_only()
{
  temp=$(echo "${1}" | sed -r 's/\<'${2}'\>//g' )
  echo ${temp}
}
f_remove_square_brackets_only()
{
  temp=$(echo "${1}" | sed 's/\[//g;s/\]//g' )
  echo ${temp}
}
############################################################ End Function declaration

# Main program
clear   # clear terminal

# First of all, store arguments in a variable.
# Bash have variable when call an executable, $0 = CommandName, $1..$N = Arguments
# We store arguments in an array. If a function is called, you cannot access to them if not are global variables
# because they are overwrited by function arguments.
args=(${@}) # Arguments
Nargs=(${#})  # Number of arguments

# We define options, using getopt
# -o Each single character stands for an option
# -l Long name for each option
# : after an option (short or long) tells that option is required argument
# :: tells is a optional argument
# For extension take a look here: http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt
options=$(getopt -o hti:o:grpa -l help,test,input:,output:,generate,regexp,psp,all -- "${@}")
if [[ $? -ne 0 ]]
then
  echo "  Error: Detected invalid option"
fi
# -o hs:ao = h & a option, no arguments; s option with a required option;
# -l help,say:,author = same.
# Declarate variables with options to clarify
optionHelp="-h|--help"
optionSetInput="-i|--input \"path\""
optionSetOutput="-o|--output \"path\""
optionGenerateInput="-g|--generate"
optionTest="-t|--test"
optionRegExp="-r|--regexp"
optionPSP="-p|--psp"
optionAllBrackets="-a|--all"

# We define some global variables
commandName=$(basename "$0")        # Extract name from first variable
commandPath=$(pwd)                  # Get the path with command pwd
outputpath="./output"               # Output folder by default
inputpath="./input"                 # input folder by default
temp=""                             # Temporal string to process names

echo "------- My third script begins execution..."
echo "--------------------------------------------"
echo "   Executing command: ${commandName}"
echo "   At path: ${commandPath}"

# Call a function to evaluate arguments as options
f_evaluate_options

# Check all arguments
# We are going to check the all the options shifting them.
# Using shift, the positional parameters are shifted to the left by this number, N.
# Using case syntaxis, more info: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html
while [ "$1" != "" ]
do
  echo "Processing option: ${1}"
  case "$1" in
      -h|--help) f_display_usage ;;       # Call function
      -t|--test) f_test_sed ;;            # Call function
      -r|--regexp) f_test_regularexpresion ;;  # Call function
      -i|--input) inputpath=${2} ; echo "   Setting Input path: ${2}" ; shift ;; # Call function, reading next variable and shift
      -o|--output) outputpath=${2} ; echo "   Setting Output path: ${2}" ; shift ;; # Call function, reading next variable and shift
      -g|--generate) f_generate_files ;; # Call function
      -p|--psp) f_remove_psp_at_start ;;  # Call function
      -a|--all) f_remove_parenthesis_and_brackets ;;  # Call function
      *)         echo "Unknown option: $1" ; f_display_usage ; break ;;
  esac
  shift;  # Advance
done;

echo "------- My third script end now!"
echo "---------------------------------"

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.