0

We got the following input:

[18:51:10] [Server thread/INFO]: Tester121 entered area ~ Wilderness - (PvP) ~

We'd like to interpret the input and define different variables.

[$time] [Server thread/INFO]: $player entered area ~ $area - (PvP) ~

The results should be:

time="18:51:10"
player="Tester121"
area="Wilderness"

I am new to stackoverflow, feel free to comment so that I can improve my how to ask skills.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • This is actually a category of task that the [grok](https://github.com/jordansissel/grok) library is built for. – Charles Duffy Mar 08 '17 at 18:05
  • Hmm. Could argue for http://stackoverflow.com/questions/19737675/shell-script-how-to-extract-string-using-regular-expressions as a duplicate, but while the answer there is great, it's a really awful question. – Charles Duffy Mar 08 '17 at 18:15
  • ...whereas http://stackoverflow.com/questions/31709705/how-to-extract-parts-of-string-in-shell-script-into-variables is a better question, but several of the answers make assumptions that don't apply to this case. – Charles Duffy Mar 08 '17 at 18:16
  • Editing questions in a way that invalidates existing answers isn't kosher here. The practice creates what is called a "chameleon question", and they're [unwelcome](https://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions). – Charles Duffy Mar 08 '17 at 18:44
  • I am sorry. But that is good to know. – Frank Stone Mar 08 '17 at 18:46
  • I post a new question for it and delete the addings here. – Frank Stone Mar 08 '17 at 18:48

1 Answers1

0

The =~ operator in [[ ]] evaluates a regex in POSIX ERE format, and assigns the resulting match groups to the array BASH_REMATCH.

re='^\[([[:digit:]:]+)\] \[Server thread/INFO\]: ([^[:space:]]+) entered area ~ ([^[:space:]]+) - [(]PvP[)] ~'
line='[18:51:10] [Server thread/INFO]: Tester121 entered area ~ Wilderness - (PvP) ~'

if [[ $line =~ $re ]]; then
  time=${BASH_REMATCH[1]}
  player=${BASH_REMATCH[2]}
  area=${BASH_REMATCH[3]}
fi
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Well if I defined the input as $input, how to test it? Thx for ur quick respond mate. – Frank Stone Mar 08 '17 at 18:18
  • Didnt saw ur edited answer, will test it :) – Frank Stone Mar 08 '17 at 18:18
  • I added something to the question. May u can help again? I am new to regex and try to learn it :) – Frank Stone Mar 08 '17 at 18:43
  • This should be a separate question. I'd suggest including in it what you tried (your own attempt at an edited regex), so folks answering it can describe how it failed. – Charles Duffy Mar 08 '17 at 18:46
  • ...once you've asked that separate question, though, feel free to link to it here. (I might be afk, though, about to head to lunch). – Charles Duffy Mar 08 '17 at 18:48
  • [http://stackoverflow.com/questions/42681138/how-to-parse-substrings-into-shell-variables-in-bash](http://stackoverflow.com/questions/42681138/how-to-parse-substrings-into-shell-variables-in-bash) – Frank Stone Mar 08 '17 at 20:23