Getting certain columns in bash

前端 未结 3 742
时光取名叫无心
时光取名叫无心 2021-01-26 17:28

Suppose I have an array where each element in the array is in the following format:

someText (x1,y1,z1) (x2,y2,z2) (x3,y2,z3) KEY

What is the a

相关标签:
3条回答
  • 2021-01-26 18:25

    This is how you can get first and last element of your string in the array:

    echo "someText (x1,y1,z1) (x2,y2,z2) (x3,y2,z3) KEY" | rev | cut -d " " -f 1 | rev
    
    echo "someText (x1,y1,z1) (x2,y2,z2) (x3,y2,z3) KEY" | cut -d " " -f 1
    
    0 讨论(0)
  • 2021-01-26 18:27

    You can do something like this (where OLDARRAY is the previous array and NEWARRAY is the new one).

    NEWARRAY=()
    for I in "${!OLDARRAY[@]}"; do
        IFS=$' ' read -ra T <<< "${OLDARRAY[I]}"
        NEWARRAY[I]="${T[0]} ${T[@]:(-1)}"
    done
    

    Example output:

    > set | grep ^NEWARRAY
    NEWARRAY=([0]="someText KEY" [1]="someText2 KEY2")
    
    0 讨论(0)
  • 2021-01-26 18:29
    echo "someText (x1,y1,z1) (x2,y2,z2) (x3,y2,z3) KEY"|awk '{print $1,$NF}'
    
    0 讨论(0)
提交回复
热议问题