2

I'm trying to format the output from the following command;

MYIP=(/sbin/ifconfig $(getnic) | awk '/inet/ { print $2 } ' | sed -e s/addr://); echo $MYIP

which generates the following output;

172.30.0.1 fe80::42:c5ff:fecd:f09f 172.31.0.1 fe80::42:47ff:fe57:2f0d 172.17.0.1 fe80::42:a1ff:fe4e:2508 127.0.0.1 ::1 fe80::2887:fcff:fe19:e54b fe80::bc99:3aff:fe46:dcab fe80::b090:4cff:fecb:3dc4 fe80::9c0f:6dff:fe6b:6c71 fe80::808f:5fff:febb:2669 fe80::f851:23ff:fee4:c87a fe80::4c97:24ff:fe04:ea02 fe80::4cde:e2ff:feb7:4784 fe80::c4fc:4aff:fe67:1863 192.168.1.134 fe80::d1f1:947f:9370:74ca

How do I make it look nicer, just with each ipadress on a separate line?

Edit: clarification; for now I just want all ip's on a separate line, one each. Would be noce to strip out the v6 numbers, but htatäs really not important. Would be a bonux though.

I've tried with tr, sed, awk but I guess I'm doing something wrong or using the wrong command. My thought was just to add another pipe at the end that does some kind of word wrangling magic.

EDIT: This is the complete function;

function myip() {
    MY_IP=$(/sbin/ifconfig $(getnic) | awk '/inet / { print $2 } ' | sed -e s/addr://| sort)
    echo -e ${MY_IP:-"Not connected"}
}

But it still won't print linebreaks. Working on that. Thanx for all input

b0red
  • 73

2 Answers2

1

Add this to the end of the command string you now have:

| tr ' ' '\n' 

which replaces the space character with a newline.

K7AAY
  • 3,816
  • 4
  • 23
  • 39
  • tried that, but that doesn't work for me somehow. – b0red Oct 23 '19 at 23:54
  • Please grab the source file, upload it to a site like pastebin.com and put its URL in your question, and I will take a whack at it later (I hope), so someone with brains will (...braiiiiins.....) – K7AAY Oct 23 '19 at 23:56
  • It works now. Thanxfor all help – b0red Oct 24 '19 at 15:24
1

Replace the echo-statement with

printf '%s\n' ${MY_IP:-"Not connected"}

and you should be good.

printf will evaluate the format-string '%s\n' for all its args and thus print a newline after each string you feed it.

markgraf
  • 2,860