Parsing OSX UNIX command results which print in multiple lines

from the CLI on a Mac, if you type

networksetup -listallnetworkservices

then you get results in a multi-line paragraph that look something like this:

networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
Wi-Fi
Display Ethernet
Bluetooth DUN
Thunderbolt Ethernet
Display FireWire
Bluetooth PAN
Thunderbolt Bridge

I need to capture each of the network services reported and add that to either a variable or an array.

Problem #1 is that first sentence. I don't wish to capture it but DO wish to capture everything else.

Problem #2 is that cut -d, cut -c and awk '{ print $x }' are all failing because they only appear to scan results form the first line, not the others that follow.

Not sure if this is a job for sed or perl, but not super good with either of those commands and their syntax. Open to yer thoughts, oh wise ones.

Yours Truly,
The Mac Dweeb

Not sure I understand. You say awk doesn't recognize lines? What's the awk record separator you're using? What's the result of

networksetup -listallnetworkservices | awk '{print NR, NF}'
1 Like
networksetup -listallnetworkservices | awk 'NR>1'
1 Like

@RudiC - I'm not familiar with either NF or NR. I'm using something like

networksetup -listallnetworkservices | awk '{print $11-$20}'

and it's returning a lot of zeros. Your example only returns numbers as well:

1 10
2 1
3 2
4 2
5 1
6 1
7 2

I'm looking for the alphanumeric results but at least we now know the bnumber of spaces as delimiters.... :slight_smile:

@Aia - Your code DID return alphanumeric, but omitted the line items with no spaces in their name like "Wi-Fi" or "Ethernet".

---------- Post updated at 01:19 PM ---------- Previous update was at 01:06 PM ----------

Houston: we found a solution.

networksetup -listallnetworkservices | awk 'NR>1'
Wi-Fi
Display Ethernet
Bluetooth DUN
Thunderbolt Ethernet
Display FireWire
Bluetooth PAN
Thunderbolt Bridge

Thank you both for your help!

Sorry I meant NR>1 and somehow I interpolated an F instead of a R.
NF means Number of Fields
NR means Number of Records (normally lines)
Glad you found the same solution. I corrected it in the previous post.

Appreciate the follow up. The issue now is a bit more devious: setting an array with these values doesn't know how to break up the resulting text! Perhaps another problem for another thread, perhaps not.

Here's the original code:

networksetup -listallnetworkservices | awk 'NR>1'
Wi-Fi
Display Ethernet
Bluetooth DUN
Thunderbolt Ethernet
Display FireWire
Bluetooth PAN
Thunderbolt Bridge

Now, if I set a variable and then print those results to that variable, I get a series of words that aren't parsed by LINE. First, the variable:

services=`networksetup -listallnetworkservices | awk 'NR>1'`

Then showing the results:

echo $services
Wi-Fi Display Ethernet Bluetooth DUN Thunderbolt Ethernet Display FireWire Bluetooth PAN Thunderbolt Bridge

All those spaces essentially defeat the initial reason I needed this list in the first place! I need individual entries for each line, added into the array: Wi-Fi, Display Ethernet, Bluetooth DUN, Thunderbolt Ethernet, etc.

Meh.

If your shell supports it:

IFS=$'\n' device=($(networksetup -listallnetworkservices | awk 'NR>1'))

Now you can access each elements of the array:

for d in ${device[@]}; do echo "$d"; done
Bluetooth DUN
Thunderbolt Ethernet
Display Ethernet
Display FireWire
Wi-Fi
Bluetooth PAN
Thunderbolt Bridge

How many elements?

echo ${#device[@]}
7

had no idea about IFS but will certainly dig into that now, thank you! the code is working on my mac and i'm loving it. appreciate it.

---------- Post updated 12-02-15 at 09:31 AM ---------- Previous update was 12-01-15 at 03:43 PM ----------

had no idea about IFS but will certainly dig into that now, thank you! the code is working on my mac and i'm loving it. appreciate it.