Prakhar
Prakhar

Reputation: 536

Grabbing all matching items in Shell output to list

When I run the following command on shell adb devices. It shows me a bunch of devices like

List of devices attached
10.16.95.35:5555    device
10.16.95.36:5555    offline
10.16.95.37:5555    device
10.16.95.38:5555    device
10.16.95.39:5555    device
10.16.95.40:5555    device
10.16.95.41:5555    offline
10.16.95.42:5555    device
10.16.95.43:5555    device
10.16.95.44:5555    device

I am trying to get only offline deviceIps as a list. So the output in this case I desire is 10.16.95.36,10.16.95.41

I was able to get just offline listed as through adb devices | grep offline

10.16.95.36:5555    offline
10.16.95.41:5555    offline

but i do not know how to process it further to get 10.16.95.36,10.16.95.41.

Upvotes: 1

Views: 32

Answers (2)

Paul Hodges
Paul Hodges

Reputation: 15273

Elaborating on jordanm's comment,

$: adb devices|awk -F: 'BEGIN{c=""} /offline/{printf "%s%s",c,$1;c=","} END{printf "\n"}' 
10.16.95.36,10.16.95.41

That should only get the lines you want, and format the way you asked.

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 246807

You can use cut with colon as -d delimiter to get the first -f field.

Then use paste with -s and -d

Upvotes: 2

Related Questions