1

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

  • Change the last command to this: echo -e "$MYIP\n" – ajgringo619 Oct 23 '19 at 23:03
  • @b0red Which do you want? Each IPv4 address on a separate line with the IPv6 addresses following, or each IPv4 AND IPv6 address on its own line? Please click edit and enhance the question to tell us. Please do not use Add Comment; instead, please use edit. – K7AAY Oct 23 '19 at 23:11
  • Hahaha. Damn. Thank you. Easy when you think outside the box :) – b0red Oct 23 '19 at 23:12
  • 1
    Your command doesn't do what you say it does. It's missing at least a $ for command substitution. Please show the actual command. And use code formatting for the output, please – muru Oct 24 '19 at 0:04
  • 1
    Probably related: New line in bash variables – steeldriver Oct 24 '19 at 1:46
1

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

| tr ' ' '\n' 

which replaces the space character with a newline.

| improve this answer | |
  • 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.

| improve this answer | |
  • Thanx. This worked fine! – b0red Oct 24 '19 at 15:24
  • You can follow up with grep to get rid of the ipv6-adresses: | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' – markgraf Oct 24 '19 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.