0

I'd like to check my minecraft server for online players using /list in server console. This gives me the following input. The goal is to compare if the player is in an specific list. If he isn't, we kick him. In output.log there are listed the allowed players (each one line).

mc()
{
    screen -S mcserver -p 0 -X stuff "$1^M"
}

    if ! grep -q $player output.log; then
    echo "$player do not runs launcher."
    mc "kick $player"

We'd like to filter the following input for players to get the online players and then can check if there are in the allowed list:

[18:43:08] [pool-2-thread-528/INFO]: There are 2/100 players online.
[18:43:08] [pool-2-thread-528/INFO]: Tester1212, Me99666

The idea behind is to filter the input using awk and find them by watching for , or something like.

The final output should be (each as one line):

Tester1212
Me99666

An problem could be to filter it if only 1 player is online and there is no , .

7
  • How else would you identify that this particular row is the one that should be parsed? Is it always the second row of the output? Does it always appear after the row that says "There are n/N players online"? Commented Mar 21, 2017 at 17:50
  • 1
    I've voted to close this question because it appears to be a request for a recommendation for a tool or solution, rather than a request for assistance with your own code. This makes your question off-topic for StackOverflow. If that assessment was incorrect, and you do indeed want help writing your own code, then please add your work so far to your question and I'll retract my close vote. Commented Mar 21, 2017 at 17:50
  • Yes, the players are always directly listed after "There are /n/N players online." Commented Mar 21, 2017 at 17:52
  • @ghoti it's an output of my minecraft server. I'd like to filter the current online players. This happens by the /list server command. Commented Mar 21, 2017 at 17:54
  • 2
    You still haven't included any code in your question. You're asking for a handout, not for help fixing your code. Sure, there are people here who will provide you with that handout in exchange for a few upvotes and a checkmark, but it's not what StackOverflow is about. Commented Mar 21, 2017 at 17:55

3 Answers 3

2

Try this -

awk -F'[, ]' '/players online/ {getline; print $3RS$5}' f
Tester1212
Me99666

OR

awk -F'[, ]' '/players online/ {getline; print $3,$5}' f
Tester1212 Me99666
Sign up to request clarification or add additional context in comments.

Comments

0

If this is coming from /list on a minecraft server, then the record you are after will always be the second one. Which is helpful here.

You can use awk to parse the line like so:

 <your command> | awk -F": " 'NR==2{split($2,a,", "); for(x in a) print a[x]}'

This will:

  1. split the records by a colon and space -F": "
  2. if we are on the second row/record NR==2
  3. then split the second field (your list of names) by a comma and a space and store in array named "a": split($2,a,", ")
  4. finally iterate the array assigning each element to "x" and print the element: for(x in a) print a[x]

There's probably a more elegant way to do this in awk, but this is a good start and it solves for your problem when only one user is on your server.

1 Comment

I'll give it a try asap. Thx for ur help. (taking lunch :P)
0

You can use awk like this:

awk -F ']: ' '/players online/{getline; gsub(/, */, ORS, $2); print $2}' output.log

Tester1212
Me99666

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.