vehomzzz
vehomzzz

Reputation: 44568

Using bash extracting fields from line starting at 2nd field in for look

My current code:

for i  in {0..5}; do
       rate[${i}]="`echo $line| awk -v par=$i '{par=par+2}{print $par}'`"
    done 

Extracts each field, starting from the 2nd and puts it into array. Please advise if there is a more elegant way to re-write this. it doesn't necessarily need to be in awk.

Upvotes: 3

Views: 841

Answers (2)

perreal
perreal

Reputation: 97938

Using set to split the input and shift to remove leading 2 elements:

set $line
shift 2
rate=($@)

or eliminating shift as suggested by chepner:

set $line
rate=("${@:3}")

Upvotes: 1

twalberg
twalberg

Reputation: 62369

You could use the variable substitution operators:

linetmp=${line#* } # remove up to the first space
linetmp=${linetmp#* } remove up to the second (now first) space

Upvotes: 0

Related Questions