#!/bin/bash
#Ziel des Skriptes wird sein die ID zu extrahieren hier nur als Consolentest:
root@~: eingang=”RECENT [1] time 1496763560, type 8001, id 134750, value 21.5547″
root@~: teil1=${eingang##*id}
root@~: echo ${teil1%%,*}
134750
ENDE
Soweit so gut-> funktioniert,
Nur bin ich der Meinung dass das noch schlanker geht, nur mir fällt einfach nicht mehr ein wie?
So einfach verschachtelt leider nicht, weil ich da irgendwas vergesse nur was?
hab schon einige Try-Error-Versuche mit Backticks Klammen Anführungszeichen hinter mir.
root@~: echo ${${eingang##*id}%%,*}
bash: ${“${eingang##*id}”%%,*}: Falsche Variablenersetzung.
Danke.
Reiche doch deinen “eingang” in einer Pipe an awk weiter
echo “$eingang” | awk -F, ‘{print $3}’ | awk ‘{print $2}’
mit awk könnte es gehen, stimmt, aber Ressourcen schonender dürfte es nicht sein.
Bin immer noch hartnäckig am ausprobieren,
denn ich will ja auch was lernen :)
[root@comp]# echo ${`${eingang##*id}`%%,*}
bash: ${`${eingang##*id}`%%,*}: Falsche Variablenersetzung.
[root@comp]# echo ${‘${eingang##*id}’%%,*}
bash: ${‘${eingang##*id}’%%,*}: Falsche Variablenersetzung.
[root@comp]# eval echo ${‘${eingang##*id}’%%,*}
bash: ${‘${eingang##*id}’%%,*}: Falsche Variablenersetzung.
[root@comp]# eval echo ${`${eingang##*id}`%%,*}
bash: ${`${eingang##*id}`%%,*}: Falsche Variablenersetzung.
[root@comp]# eval echo ${${eingang##*id}%%,*}
bash: ${${eingang##*id}%%,*}: Falsche Variablenersetzung.
[root@comp]# eval echo ${“${eingang##*id}”%%,*}
bash: ${“${eingang##*id}”%%,*}: Falsche Variablenersetzung.
[root@comp]# eval echo ${~${eingang##*id}%%,*}
bash: ${~${eingang##*id}%%,*}: Falsche Variablenersetzung.
[root@comp]# eval echo ${(${eingang##*id)}%%,*}
bash: ${(${eingang##*id)}%%,*}: Falsche Variablenersetzung.
[root@comp]# eval echo ${(${eingang##*id})%%,*}
bash: ${(${eingang##*id})%%,*}: Falsche Variablenersetzung.
[root@comp]# eval echo ${((${eingang##*id}))%%,*}
bash: ${((${eingang##*id}))%%,*}: Falsche Variablenersetzung.
[root@comp]# echo $’${eingang##*id}’%%,*
${eingang##*id}%%,*
[root@comp]# echo “$’${eingang##*id}’%%,*”
$’ 134750, value 21.5547’%%,*
[root@comp]# echo “${‘${eingang##*id}’}%%,*”
bash: ${‘${eingang##*id}’}%%,*: Falsche Variablenersetzung.
[root@comp]# echo “$’${eingang##*id}’%%,*”
$’ 134750, value 21.5547’%%,*
[root@comp]# eval echo “${‘${eingang##*id}’}%%,*”
bash: ${‘${eingang##*id}’}%%,*: Falsche Variablenersetzung.
[root@comp]# echo “${‘${eingang##*id}’}%%,*”
bash: ${‘${eingang##*id}’}%%,*: Falsche Variablenersetzung.
[root@comp]# echo “$’${eingang##*id}’%%,*”
$’ 134750, value 21.5547’%%,*
[root@comp]# echo “$`${eingang##*id}`%%,*”
bash: 134750,: Kommando nicht gefunden.
$%%,*
[root@comp]# echo “${`${eingang##*id}`%%,* }”
bash: ${`${eingang##*id}`%%,* }: Falsche Variablenersetzung.
[root@comp]# eval echo “${`${eingang##*id}`%%,* }”
bash: ${`${eingang##*id}`%%,* }: Falsche Variablenersetzung.
hallo,
das Zerlegen von “RECENT [1] time 1496763560, type 8001, id 134750, value 21.5547”
mittels ${##*} etc, mit zwei Schrtten ist die eleganteste Lösung, die Du richtig erraten hast. Alles andere kann nur schlimmer werden.
In bash gilt noch [[ $eingang =~ pattern ]] und daraus ${BASH_REMATCH[3]} oder so, ist aber nicht portiebar zu /bin/dash oder /bin/rsh. Warung vor versteckten \tabs!
Beispiele:
[[ $eingang =~ id\ ([0-9]+) ]] && echo “${BASH_REMATCH[1]}”
[[ $eingang =~ id[[:blank:]]([0-9]+) ]] && echo “${BASH_REMATCH[1]}” # for \tabs inbetween