Last active
July 13, 2021 20:59
-
-
Save ahaitoute/5004850 to your computer and use it in GitHub Desktop.
This script cuts between the first and last character(s).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Authors: Abdelouahed Haitoute and Mathijs Möhlmann | |
# | |
# 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 <http://www.gnu.org/licenses/>. | |
# | |
# | |
# Argument = -c character(s) or (-f character(s) and -l character(s)) | |
# | |
# Any suggestions and pull requests are welcome on GitHub! | |
# | |
usage() | |
{ | |
cat << EOF | |
usage: $0 -c character(s) or (-f character(s) and -l character(s)) | |
This script cuts between the first and last character(s). | |
OPTIONS: | |
-h Show this message | |
-c The first and last character(s) which must be cut. | |
-f The first character(s) which must be cut. This option must be used with the -l option. | |
-i Includes the character(s) on output. | |
-l The last character(s) which must be cut. This option must be used with the -f option. | |
-v Verbose | |
EOF | |
} | |
CHARACTER= | |
CHARACTER_INCLUDED=false | |
CHARACTER_INCLUDED_CMD=cat | |
FIRST_CHARACTER= | |
LAST_CHARACTER= | |
VERBOSE=false | |
while getopts "hc:f:il:v" OPTION | |
do | |
case $OPTION in | |
h) | |
usage | |
exit 1 | |
;; | |
c) | |
CHARACTER=$OPTARG | |
;; | |
f) | |
FIRST_CHARACTER=$OPTARG | |
;; | |
i) | |
CHARACTER_INCLUDED=true | |
;; | |
l) | |
LAST_CHARACTER=$OPTARG | |
;; | |
v) | |
VERBOSE=true | |
;; | |
?) | |
usage | |
exit | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
# now do something with $@ | |
if [[ -z $CHARACTER ]]; then | |
if [[ -z $FIRST_CHARACTER ]] || [[ -z $LAST_CHARACTER ]]; then | |
usage | |
echo "ERROR: First and/or last character are missing." | |
exit 1 | |
else | |
DELIMITER=($FIRST_CHARACTER $LAST_CHARACTER) | |
fi | |
else | |
DELIMITER=($CHARACTER $CHARACTER) | |
fi | |
if $CHARACTER_INCLUDED; then | |
CHARACTER_INCLUDED_CMD="sed s/^/${DELIMITER[0]}/;s/$/${DELIMITER[1]}/" | |
fi | |
if [[ -z $@ ]]; then | |
read input; INPUT=$input; | |
else | |
INPUT=$@ | |
fi | |
if $VERBOSE; then | |
echo "Remaining args: $INPUT" | |
echo "Delimiters between the quotes: First is \"${DELIMITER[0]}\" and last is \"${DELIMITER[1]}\"" | |
echo "Included_cmd: ${CHARACTER_INCLUDED_CMD}" | |
fi | |
# After a couple of tries, we think we got it. | |
# | |
# echo "$INPUT" | cut -d${DELIMITER[0]} -f 2- | rev | cut -d${DELIMITER[1]} -f 2- | rev | ${CHARACTER_INCLUDED_CMD} | |
# echo "$INPUT" | sed 's/^.*'${DELIMITER[0]}'\(.*\)'${DELIMITER[1]}'.*$/\1/g' | ${CHARACTER_INCLUDED_CMD} | |
# echo "$INPUT" | perl -pe 's|^.*?'${DELIMITER[0]}'(.*)'${DELIMITER[1]}'.*?$|\1|' | ${CHARACTER_INCLUDED_CMD} | |
TMP=${INPUT#*${DELIMITER[0]}} | |
echo ${TMP%${DELIMITER[1]}*} | ${CHARACTER_INCLUDED_CMD} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment