función de shell
Un enfoque un poco más detallado, pero funciona en cualquier tipo de primer y último personaje, no tiene que ser el mismo. La idea básica es que estamos tomando una variable, leyéndola carácter por carácter y agregando solo aquellos que queremos a una nueva variable
Aquí está toda esa idea formateada en una buena función
crop_string_ends() {
STR="$1"
NEWSTR=""
COUNT=0
while read -n 1 CHAR
do
COUNT=$(($COUNT+1))
if [ $COUNT -eq 1 ] || [ $COUNT -eq ${#STR} ]
then
continue
fi
NEWSTR="$NEWSTR"$CHAR
done <<<"$STR"
echo $NEWSTR
}
Y aquí está esa misma función en acción:
$> crop_string_ends "|abcdefg|"
abcdefg
$> crop_string_ends "HelloWorld"
elloWorl
Pitón
>>> mystring="|abcdefg|"
>>> print(mystring[1:-1])
abcdefg
o en la línea de comando:
$ python -c 'import sys;print sys.stdin.read()[1:-2]' <<< "|abcdefg|"
abcdefg
AWK
$ echo "|abcdefg|" | awk '{print substr($0,2,length($0)-2)}'
abcdefg
Rubí
$ ruby -ne 'print $_.split("|")[1]' <<< "|abcdefg|"
abcdefg
Sergiy Kolodyazhnyy
fuente
awk -F\| '{ print $2 }' <<<"|string|"
awk
soluciones geniales . Necesito aprenderawk
.IFS='|' read string2 <<< $string
:)"${string:1:-1}"
man bash
.