How to insert part of the string in to GNU parallel output

Viewed 24

I'm getting list of devices IPs using this command:

# Get all devices from ADB server
wIP=$(adb devices | tail -n +2 | grep "device" | cut -f1 | cut -f1 -d\ );

Result $wIP is:

192.168.1.10
192.168.1.20

Now I'm getting the Serial number and Name of the device with two commands:

wName=$(parallel adb -s {} shell getprop ro.product.display_name ::: "$wIP");
wSerial=$(parallel adb -s {} shell getprop ro.serialno ::: "$wIP");

Result from $wName is:

Samsung Watch4
Samsung Watch4

Result from $wSerial is:

RFAR82JDGLJ
RFAR82JPRFC

For printing I use this commands:

printf "%-22s %-22.27s %-14.12s\n" "IP" "Name" "Serial"
printf "%-22s %-22.27s %-14.14s\n" "$wIP" "$wName" "$wSerial"

Final matching is:

IP             Name                 Serial
192.168.1.10   Samsung Watch4       RFAR82JDGLJ
192.168.1.20   Samsung Watch4       RFAR82JPRFC

Question: How to get in "Name" in brackets last 4 characters from "Serial" and get result like this:

IP             Name                   Serial
192.168.1.10   Samsung Watch4 (DGLJ)  RFAR82JDGLJ
192.168.1.20   Samsung Watch4 (PRFC)  RFAR82JPRFC

Complete code:

wIP=$(adb devices | tail -n +2 | grep "device" | cut -f1 | cut -f1 -d\ );

wName=$(parallel adb -s {} shell getprop ro.product.display_name ::: "$wIP");
wSerial=$(parallel adb -s {} shell getprop ro.serialno ::: "$wIP");

printf "%-22s %-22.27s %-14.12s\n" "IP" "Name" "Serial"
printf "%-22s %-22.27s %-14.14s\n" "$wIP" "$wName" "$wSerial"
1 Answers

After thinking and trying different solutions... I found the simplest one. No need to pipe the parallel output or anything complicated. Just parse last 4 chars form wSerial output:

printf "%-22s %-22.27s %-14.14s\n" "$wIP" "$wName (${wSerial: -4})" "$wSerial"
Related