1

I want to save each output filepath to a variable and then grep through them to find the timestamp. I want to lable each variable by adding the nodeId from the node list I am looping through. When I try this with the following code I get an error

output1_1: command not found

nodeList=('1_1' '1_6' '2_1' '2_6')
for i in "${nodeList[@]}"
do
   output${i}=$CWD/output/abc${i}.txt
   times${i}=$(grep  -m 1 '\"path\":' $output${i}| sed 's/.*timestampUtc\"://g' | sed 's/,.*//g')
done
Rakey
  • 11
  • 3
  • 1
    Why not use associative arrays? `declare -A output times`, then `output[$i]=...` – muru Sep 12 '18 at 02:26
  • Instead of `grep -m | sed | sed`, try this (all in one) `sed` command: `sed '/\"path\":/{s/.*timestampUtc\":/\([^,]*\),.*/\1/;q};d'` – F. Hauri Sep 12 '18 at 19:00

2 Answers2

0

As @muru suggested, try

declare -A output times
nodeList=('1_1' '1_6' '2_1' '2_6')
for i in "${nodeList[@]}"; do
   output[${i}]=${PWD}/output/abc${i}.txt
   times[${i}]=$(grep  -m 1 '\"path\":' ${output[${i}]} | sed 's/.*timestampUtc\"://g' | sed 's/,.*//g')
done
JGK
  • 3,242
  • 1
  • 17
  • 22
  • I am working with bash 3 so unfortunately the terminal wont allow me to do that – Rakey Sep 12 '18 at 13:38
  • Have a look at [Create associative array in bash 3](https://stackoverflow.com/questions/11776468/create-associative-array-in-bash-3) – JGK Sep 12 '18 at 13:57
0

One way to set a variable whose name is derived from another variable is to use the -v option to 'printf'. To get the value of a variable whose name is in another variable (e.g. varname) use ${!varname}.

See Creating a string variable name from the value of another string and How can I generate new variable names on the fly in a shell script?.

Using these a possible loop body is:

output_var=output${i}
times_var=times${i}

printf -v "$output_var" '%s' "$CWD/output/abc${i}.txt"
printf -v "$times_var" '%s' "$(grep  -m 1 '\"path\":' "${!output_var}" | sed 's/.*timestampUtc\"://g' | sed 's/,.*//g')"

Note that (unless CWD is set elsewhere in your program) you probably want $PWD instead of $CWD.

pjh
  • 4,359
  • 14
  • 15