I've no issue with this cut
command.
wolf@linux:~$ echo ab cd ef
ab cd ef
wolf@linux:~$ echo ab cd ef | cut -d ' ' -f 1
ab
wolf@linux:~$ echo ab cd ef | cut -d ' ' -f 2
cd
However, when I try the same command with different input like this, I did not get the output as expected.
wolf@linux:~$ ip address show eth0 | grep 'inet '
inet 10.10.10.10/24 brd 10.10.10.255 scope global dynamic eth0
wolf@linux:~$ ip address show eth0 | grep 'inet ' | cut -d ' ' -f 1
wolf@linux:~$ ip address show eth0 | grep 'inet ' | cut -d ' ' -f 2
What's wrong in the second example?
awk
doesn't seems to have a problem with the same input, strange.
wolf@linux:~$ ip address show eth0 | awk '/inet / {print $1}'
inet
wolf@linux:~$ ip address show eth0 | awk '/inet / {print $2}'
10.10.10.10/24
inet
...? – steeldriver Aug 27 '20 at 14:25awk
counts the first non-blank character as part of the first field, by default. – Kusalananda Aug 27 '20 at 14:36ip
's JSON output is meant to be parsed. Along with thejq
command you can do this in shell. Eg:ip -4 -json address show vboxnet0 | jq -j '.[] | .addr_info[] | .local, "/", .prefixlen, "\n"'
– A.B Aug 27 '20 at 17:35ip -4 -br a s eth0 | awk '{print $3}'
– annahri Aug 28 '20 at 08:57ip -4 -j a ls vboxnet0 | jq -r '.[] | .addr_info[] | "\(.local)/\(.prefixlen)"'
– u1686_grawity Aug 28 '20 at 12:11