Search code examples
printfmultiple-columnspastecuttr

Using tr or cut skews column formatting


I'm using a script that uses curl to obtain specific array values from a configuration. Tom Fenech helped with the column formatting. I've run into another issue where the column formatting is skewed by the tr and cut commands. The command output of one array set contains square brackets surrounding the object's key. I'm using tr or cut to remove the surrounding brackets, and they do the job, however the column format is skewed. Here's my code:

# get the device groups
d_groups=`curl -H "X-Person-Token: $auth_token" -H "X-Person-Email: $auth_email" -k "$api_host/api/v1/device_groups"`
    grp_uuid=`echo $d_groups | jq '.[] | .uuid'`
    grp_name=`echo $d_groups | jq '.[] | .name'`
    grp_desc=`echo $d_groups | jq '.[] | .description'`
    grp_dev=`echo $d_groups | jq '.[] | .devices'`
        echo "========== DEVICES =========="
        paste <(printf 'DEVICE_GROUP_NAME\n%s\n' "$grp_name") <(printf 'DESCRIPTION\n%s\n' "$grp_desc") <(printf 'DEVICE_GROUP_UUID\n%s\n' "$grp_uuid") <(printf 'DEVICES\n%s\n' "$grp_dev" | cut -c2-39) | column -t
        echo ""
exit 0

I've also tried using 'tr -d "[]"' with the same results as using cut. Here are the results of the above script:

========== DEVICES ==========
DEVICE_GROUP_NAME                       DESCRIPTION     DEVICE_GROUP_UUID                       DEVICES
"Auto_API_GP1"                          "Auto_API_GP1"  "03be550b-3744-484e-88c5-d25e1ed865c0"
"Auto_API_GP2"                          "Auto_API_GP2"  "3a8e2ee4-3a59-4fba-aaf0-0d527d20fe13"  "1a7a2092-29bd-4178-88e9-1373ee5886de"
"Auto_API_GP3"                          "Auto_API_GP3"  "ba1ead89-34f5-4084-a9a2-8a681e83164d"
"81a3969c-1cc3-4f13-8602-afcb981d5295"
"8bbfd1a7-e048-4148-abeb-354763b6aef7"

What I'm expecting for results:

========== DEVICES ==========
DEVICE_GROUP_NAME  DESCRIPTION     DEVICE_GROUP_UUID                       DEVICES
"Auto_API_GP1"     "Auto_API_GP1"  "03be550b-3744-484e-88c5-d25e1ed865c0"  "1a7a2092-29bd-4178-88e9-1373ee5886de"
"Auto_API_GP2"     "Auto_API_GP2"  "3a8e2ee4-3a59-4fba-aaf0-0d527d20fe13"  "81a3969c-1cc3-4f13-8602-afcb981d5295"
"Auto_API_GP3"     "Auto_API_GP3"  "ba1ead89-34f5-4084-a9a2-8a681e83164d"  "8bbfd1a7-e048-4148-abeb-354763b6aef7"

I'm an absolute beginner at this, any insight is very much appreciated. Thanks!


Solution

  • So I stopped staring at the script long enough to get lunch and coffee and figured that the code I posted above was removing the brackets on the first output but was either entering a new line or replacing the bracket with a new line. So I simply replaced

    tr -d '[]'
    

    with

    tr -s '[]' '\n'
    

    and it now works as expected.

    I do humbly except any corrections or a more efficient way. :)