P
P
PlayBoyXXX2022-01-07 01:56:08
linux
PlayBoyXXX, 2022-01-07 01:56:08

What is the problem with output?

Hello. I can't figure out what I'm doing wrong. I need to output data in this form:
Nickname IP

for((integer = 1; integer <= ${user_total}; integer++))
  do
  user_nick=$(occtl --json show users | grep 'Username' | awk -F ":" '{print $2}' | grep -E -o "([A-Z,a-z,0-9]{1,10})" | grep -v ',')
  user_IP=$(occtl --json show users | grep 'Remote IP' | awk -F ":" '{print $2}' | grep -oE '\b[0-9]{1,3}(\.[0-9]{1,3}){3}\b')
  user_list_all=${user_list_all}"Пользователь: "${user_nick}" IP: "${user_IP}""
  done
  echo -e "${user_list_all}"

But the output is like this:

spoiler
User: Tlwrlx
Kesha1
Arken1
Yklym2
Yklym3 IP: 62.212.248.81
95.47.57.56
62.212.248.81
62.212.248.21
62.212.248.21

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
PlayBoyXXX, 2022-01-08
@PlayBoyXXX

sed -n "${integer}p"

S
Sergey Pankov, 2022-01-07
@trapwalker

For greater clarity, it would be worthwhile to take an example of the command output
occtl --json show users
so that the responders do not have to set up a VPN server and connect test users.
It is immediately clear that you are requesting the result in json format, and then parsing it with grapples and awk, which is extremely pointless and merciless. Why not use jq for this purpose instead of trying to hammer a screw.
The problem you cited is related to the fact that, probably, the output in the form of json occurs without a guarantee of the order of the keys, and your way of getting data from it is very barbaric.
And the problems in this approach will not be limited to this, because the list of users can change between iterations and even between getting the name and ip.
It would be necessary to get all the raw data once, and then pull out the right one from them.

K
ky0, 2022-01-07
@ky0

For parsing jason, using awk these days is pretty sub-optimal and tedious. Smoke jq .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question