V
V
Vasily2019-03-22 14:40:58
bash
Vasily, 2019-03-22 14:40:58

Using the GNU awk utility?

Good day, the task is to clear the cache from the rediscluster, but I can’t understand why the awk utility does not behave as I expect.
Here is the output of my command without using the awk utility:

[email protected]:~$ docker run -i --rm --net host redis sh -c "redis-cli -h 192.168.100.79 -p 7000 cluster nodes | grep master"
08a7ff2732079bc7b113726352101e5302a1f5d8 192.168.101.79:7001 master - 0 1553254293379 2 connected 10923-16383
d04349ce3e46ed74e6d4e16c2911d7e33865dc55 192.168.101.79:7000 myself,master - 0 0 4 connected 5461-10922
4bb9aae34f4336f30117ef5901b68c94cd1d559b 192.168.101.79:7000 master - 0 1553254294381 1 connected 0-5460

My first goal is to take the IP and ports of the instances from the output of redis-cli, the delimiter is -F: (colon) and I write like this:
[email protected]:~$ docker run -i --rm --net host redis sh -c "redis-cli -h 192.168.100.79 -p 7000 cluster nodes | grep master | awk -F: '{print $1 $2}'"

But the problem is that the output does not change and I get the same output as without using AWK, and no matter what I write $1, $2, $3 - it doesn't matter!!
The only thing that can change the output is if I write '{print $0}, only in this case the output will show three lines with zeros:
[email protected]:~$ docker run -i --rm --net host redis sh -c "redis-cli -h 192.168.100.79 -p 7000 cluster nodes | grep master | awk -F: '{print $0}'"
0
0
0

What could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2019-03-22
@fox_12

Well so you have a string and is divided into two

$ echo "08a7ff2732079bc7b113726352101e5302a1f5d8 192.168.101.79:7001 master - 0 1553254293379 2 connected 10923-16383" | awk -F: '{print $1 $2}'
08a7ff2732079bc7b113726352101e5302a1f5d8 192.168.101.797001 master - 0 1553254293379 2 connected 10923-16383
$ echo "08a7ff2732079bc7b113726352101e5302a1f5d8 192.168.101.79:7001 master - 0 1553254293379 2 connected 10923-16383" | awk -F: '{print $1}'
08a7ff2732079bc7b113726352101e5302a1f5d8 192.168.101.79
$ echo "08a7ff2732079bc7b113726352101e5302a1f5d8 192.168.101.79:7001 master - 0 1553254293379 2 connected 10923-16383" | awk -F: '{print $2}'
7001 master - 0 1553254293379 2 connected 10923-16383

Only you again do the concatenation on the output
, maybe that's what you meant?:
$ echo "08a7ff2732079bc7b113726352101e5302a1f5d8 192.168.101.79:7001 master - 0 1553254293379 2 connected 10923-16383" | awk '{ print $2 }' 
192.168.101.79:7001

Separate host
$ echo "08a7ff2732079bc7b113726352101e5302a1f5d8 192.168.101.79:7001 master - 0 1553254293379 2 connected 10923-16383" | awk '{ print $2 }' | awk -F: '{print $1}'
192.168.101.79

Separate port
$ echo "08a7ff2732079bc7b113726352101e5302a1f5d8 192.168.101.79:7001 master - 0 1553254293379 2 connected 10923-16383" | awk '{ print $2 }' | awk -F: '{print $2}'
7001

X
xotkot, 2019-03-22
@xotkot

My first goal is to take IP and ports...

how would you like to receive them?
separately, together, through a colon, through a space, or in some other way?
my telepathy suggests that through a space, if this is really the case, then the code will look something like this:
192.168.101.79 7001
192.168.101.79 7000
192.168.101.79 7000

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question